Mostrando entradas con la etiqueta bash. Mostrar todas las entradas
Mostrando entradas con la etiqueta bash. Mostrar todas las entradas

viernes, 18 de febrero de 2011

Procesando archivos en pararelo en bash con "xargs"

function process_file {
file=$1
do_something_with $file
}
export -f process_file


find PATH -type f | xargs --max-args=1 --max-procs=NUM -I{} bash -c process_file \{\}
or a shorter version
find PATH -type f | xargs -n1 -PNUM -I{} bash -c process_file \{\}



Where NUM is the number of parallel jobs and PATH where to start looking for the files.


source

martes, 20 de octubre de 2009

Get list tables from postgres database

If we need to iterate over all tables of a certain schema, we can get the list of schema, table, type and user using psql:


$ psql -qAtF, -c "\dt da_schema.*"
da_schema,segments,table,jacen
da_schema,speeds,table,jacen
$ _


You can also use "*" in the schema name, for example:

$ psql -qAtF, -c "\dt da_schema*.*"
da_schema,segments,table,jacen
da_schema,speeds,table,jacen
da_schema_2,labels,table,jacen
da_schema_test,positions,table,jacen
$ _

martes, 22 de septiembre de 2009

Changing execution attributes on svn

Sometimes a script is added to a SVN repository without execution permissions. After that, a simple chmod doesn't work for SVN.

This is what can be done to change the attributes of the file:


$ svn propset svn:executable on da_scripto.sh
$ _

viernes, 11 de septiembre de 2009

Attach a file to a mail on shell

In the old times we were able to attach a file using the mail command. Now, we can do the same thing using mutt:

$ echo "Mail body" | mutt -s "This is a mail with attachments" -a ~/mi_file.txt -a ~/mi_picture.png anne@octop.us
$ _


We can also use some pre-written file to use as the email body:

$ mutt -s "Mail with body from a file" anne@octop.us < ~/mail_body.txt
$ _

lunes, 27 de julio de 2009

Get IP address of a host

If you need to get the IP address of a host, but you don't want to parse the output of ifconfig, you can use hostname:

$ hostname -i
192.168.1.160
$ _

jueves, 25 de junio de 2009

Error handling in Bash

To check if a command was successfully run in our bash script, we have to check the value of its output, which is stored in the $? variable. If its value equals to 0, then the command was successful, otherwise, an error was thrown:



rm ~/data.tar.bz2
if [ "$?" != 0 ]; then
echo "ERROR: Could not delete file"
exit 1
fi

$ _