Time your shell script operations

This is a note to self (so I don’t forgot about it) – it is not rocket science 😉

If you need to check how long it takes to run part of your bash script you can use the use PS4 trick, as explained partially here, but it is a bit messy and I don’t like to do the math by hand 😛

In order to test the performance bottleneck of one of my shell scripts I draft a small set of functions.
One to start the clock ticking, another to check how long it passed since the last mark and another to reset the clock.

You can download/copy into a file of your choice and then source it inside your script. The output is the nanoseconds passed between operations. If you make (any) use of it please let me know. I don’t think it will blow up your computer, but you’re at your own risk 😉

#######################################
# Bruno Lucas – v1.0 – 2012/10/22 #
#######################################
TIME_TICKER=0

echoerr() { echo -e “$@” 1>&2; }

function time_start()
{

TIME_TICKER=`date “+%s%N”`

}

#You can pass a message delimited by single quoted
function time_mark()
{

CURR=`date +%s%N`
DIFF=`echo “$CURR – $TIME_TICKER” | bc`
echoerr “$DIFF\t$1”
TIME_TICKER=$CURR

}

function time_reset()
{

TIME_TICKER=0

}

To use:

source timer.sh

time_start

some_op_of_mine 1 2 3 4

time_mark ‘After x’

and you’ll get something like ‘13789137   After x’ telling the elapsed time.

PS: The echoerr function was stolen from here, it echos the content to STDERR, instead of STDOUT.

PS1: Copy/paste from HTML can sometimes come with errors, always check the source code prior to execution.

 

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux, Tech. Tags: , , , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Pub.

Grabbing magnet links from EZTV

To get all the magnetic links from a show that was posted on EZTV just use the following command:

wget eztv.it/show -O – | grep magnet | cut -f6 -d ‘”‘  > show.magnet.link.newer.first.txt

to sort by the order that the show was broadcast use the tac command:

wget eztv.it/show -O – | grep magnet | cut -f6 -d ‘”‘  | tac > show.magnet.link.chronological.order.txt

You want to trough-in after the grep magnet , a grep 720 if you’re looking for HD version or a grep -v 720 if want to make sure just to get SD versions. A grep -i S04E would filter Season 4 only episodes.

Notice that the argument for the cut is ‘ ” ‘ (that is, single quote, normal quote, single quote, not grave accent `) – HTML sometimes messes this up …

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Pub.

Primeiro de Agosto


Xutos e Pontapés – 1 de Agosto

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in My Life. No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Pub.

Salvar stream/programa do site da RTP

Por vezes quero ver offline (longe do browser e na TV via XBMC) algum programa da RTP. Para tal dei um olhinho ao HTML da página e pedi ajuda à minha amiga bash.

#!/bin/bash

URL=”$1″
PAGE=`wget -c “$URL” -O -`
START=`echo “$PAGE” | grep ‘addVariable(“streamer”‘ | cut -f4 -d'”‘`
END=`echo “$PAGE” | grep ‘addVariable(“file”‘ | cut -f4 -d'”‘ `
FILE=`echo $START/$END`
OUT=`basename $FILE`
mplayer “$FILE” -dumpstream -dumpfile “$OUT”

Para além das coisas normais de Linux (e Unix) usa o mplayer para salvar a stream. Podem salvar o código bnm ficheiro, dar-lhe permissões de execução e invocar com o URL da página da RTP em questão.
Funciona com os programas que testei 😛 (Jan 2012) – como se sabe estas coisas vão funcionado até que alguém do lado de lá se lembre de mudar a página 🙂

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux, Tutorial. Tags: , , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Bash, Ficheiros e Extensões

Alguns truques de bash úteis (e post para eu me lembrar deles):

FICH=”/home/bruno/abc.txt”

FICH_SEM_DIR=`basename $FICH`
echo “O ficheiro sem o caminho absoluto e’ $FICH_SEM_DIR “#abc.txt

NOME_FICH=${FICH_SEM_DIR%.*}
echo “O ficheiro sem o caminho absoluto e sem extensao e’ $NOME_FICH ” #abc

EXTENSAO_DO_FICH=${FICH_SEM_DIR#*.}
echo “A extensao do ficheiro e’ $EXTENSAO_DO_FICH “#txt

#se soubermos a priori a extensão do ficheiro é mais simples

FICH_SEM_DIR_SEM_EXTENSAO=`basename $FICH .txt`
echo “O ficheiro sem o caminho absoluto e sem extensao e’ $FICH_SEM_DIR_SEM_EXTENSAO “#abc

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Detach a running process – Linux

Sometimes I start a job remotely and only after I remember that I may need to log-off, but the process must continue to run afterwards. This can be done with command disown. On the same shell where your process is running type Ctrl+Z, to pause the program, you’ll see the job id, something like [1], you need it for the disown command, now restart the job in background with the command bg and you’re ready to issue the disown command:

disown -h %1

you can also use the pid1 of the process (use jobs -l on that same shell and you’ll get a longer number that is the pid of the process you want to detach):

disown -h pid

after that you can close the shell with exit or Ctrl+D.

If you start your program with nohup2 or screen you don’t have to worry about log-off and running processes.

This command can also be used in Unix (such as BSD/MacOS X/etc.) but their might be implementation differences.

1 After some interaction on Google+ with CNF we found out that the pid is accepted but it is not documented. I’ve changed the post to reflect a more appropriated use of the disown command (according to the manual).

2 disown -h is is basically a nohup a posteriori.

Solution found on serverFault.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , . 2 Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Download Youtube Playlist with youtube-dl

To download a full playlist from Youtube with youtube-dl do the following:

youtube-dl.py -A -t -c “http://www.youtube.com/p/1234567890ABCDEF”

-A makes youtube-dl auto-number the files downloaded in order (0001, 0002, …)

-t uses the title page, otherwise the file name will be non-understandable

-c continues in case of failure

In case your playlist url is something like: http://www.youtube.com/playlist?list=1234567890ABCDEF transform it into the format: http://www.youtube.com/p/1234567890ABCDEF otherwise you’ll get an error.

One can specific also the quality of the video by using the -fmt FMT or –max-quality=FMT where FMT is number as can be seen here, 34 is 360p, 35 480p, 37 is 1080p …

youtube-dl can be installed in your Linux distro using the normal package managers or you can download it by hand to your favorite directory from here. On this directory executing youtube-dl -U makes the script get auto-updated to the latest version – important because sometimes Youtube changes things that make elder versions of the script not work.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux, Tech. Tags: , , . 1 Comment »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Saber detalhes da RAM instalada a partir da linha de comandos

Já mais do que uma vez que precisei de saber detalhes sobre a memória RAM que tenho instalada no sistema (velocidade, tipo, tamanho, etc.), mas não me apetece/posso abrir a caixa do PC.
Em Linux podem usar o comando dmidecode para obter estas e muitas outras informações do vosso hardware. A sequência de comandos seguinte mostra somente a informação (relevante) sobre a memória RAM instalada no computador.

sudo dmidecode | grep -A20 ‘Memory Device’ | grep -v ‘Range’ |grep ‘Speed:\|Part Number\|Size:\|Type:’

No sistema onde estou a testar isto obtenho como resultado:

Size: 2048 MB
 Type: DDR2
 Speed: 800 MHz (1.2 ns)
 Part Number: EBEXXXX8AXXX-XX-X
 Size: No Module Installed
 Type: DDR2
 Speed: Unknown
 Part Number:

Nota:O dmidecode pode estar fora do vosso PATH, só acessível ao root ou não estar instalado no sistema.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...