rm ~/data.tar.bz2
if [ "$?" != 0 ]; then
echo "ERROR: Could not delete file"
exit 1
fi
$
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:
miércoles, 24 de junio de 2009
The kill that revives
Let's say your working on a remote console and send a process to sleep (CTRL-Z), all of the sudden your connection freezes and you loose the ability to interact with the slept process and bring it back to life. Don't connect again to kill it and restart it. Connect again to revive it with kill
:)
$ kill -SIGCONT pid
$
:)
martes, 23 de junio de 2009
disown ...
For the bash lovers:
It happens to all of us ...
You launch a command that is taking too long, you can't logout 'cause you'll lose your session and all the running hours...
what to do??
Now INIT will take care of your son, and you are free to leave ...
(see "man disown", a bash built-in).
It happens to all of us ...
You launch a command that is taking too long, you can't logout 'cause you'll lose your session and all the running hours...
what to do??
$ ./myprocess.bash
^Z
[1]+ Stopped ./myprocess.bash
$ bg
$ disown %1
$ logout
$
Now INIT will take care of your son, and you are free to leave ...
(see "man disown", a bash built-in).
Adding en_US.utf8 locale to an Ubuntu
sudo localedef –no-archive -i en_US -c -f UTF-8 en_US.UTF-8
locale -a
C
en_US.utf8
POSIX
For the next time you log into the server the following two lines have to vi added to /etc/environment
LANGUAGE=”en_US.utf8″
LANG=”en_US.utf8″
locale -a
C
en_US.utf8
POSIX
For the next time you log into the server the following two lines have to vi added to /etc/environment
LANGUAGE=”en_US.utf8″
LANG=”en_US.utf8″
viernes, 19 de junio de 2009
double/single quotes in bash
Single Quotes (' '): Preserve the literal value for each character within the quotes. For example,
Double Quotes (" "): Preserve the literal value of all characters within the quotes. Rules Exception are: ‘$’, ‘`’ and‘\’ (shell expansion). For example,
Back Quotes or Back Sticks(``):Command Substitution (idem $() )
For example,
$ string="Hello World"
$ echo $string
$ echo '$string "string"'
$
Double Quotes (" "): Preserve the literal value of all characters within the quotes. Rules Exception are: ‘$’, ‘`’ and‘\’ (shell expansion). For example,
string="Hello World"
echo $string
echo "My first script: $string, \"string\", "string""
$
Back Quotes or Back Sticks(``):Command Substitution (idem $() )
For example,
echo `date`, $(date)
$
jueves, 18 de junio de 2009
Adding Google projection to your PostGIS
INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 900913, 'spatialreference.org', 900913, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs', 'PROJCS["unnamed",GEOGCS["unnamed ellipse",DATUM["unknown",SPHEROID["unnamed",6378137,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Mercator_2SP"],PARAMETER["standard_parallel_1",0],PARAMETER["central_meridian",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["Meter",1],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"]]');
Files being accessed by a process
$ ls -l /proc/pid_number/fd
Try with the pid of your firefox process and you'll see all the sqlite files where firefox stores places, cookies, downloads, permissions, etc.. All the jar files, certificates, extensions and more.
Try:
$ echo ".dump" | sqlite3 /path_to_firefox_profile/formhistory.sqlite
:P
Request Layer with another SLD from OpenLayers
Reports is the layer name in the map and SLD is the sld name
layer=Map.getLayersByName('Reports')[0];
layer.params['STYLES']='SLD';
later.redraw();
Kill'em all >:|
$ kill -9 -1
$
Kill all processes owned by the user except the shell from where it's being launched, it's a nice cleanup shortcut.
Code Highlighting
Now you can post your snippets codes with a nice highlight.
Example:
To use it, just write your code inside of <pre> or <textarea>
with tags name="code" and class="language" where language could be:
If you are looking for a console style, use class="console", and <blink>_</blink>
for your cursor :)
Example:
from datetime import datetime
print datetime.now()
To use it, just write your code inside of <pre> or <textarea>
with tags name="code" and class="language" where language could be:
- Javascript (js)
- Bash (bash)
- Python (python)
- Sql (sql)
- C++ (cpp)
- Java (java)
- Php (php)
If you are looking for a console style, use class="console", and <blink>_</blink>
for your cursor :)
miércoles, 17 de junio de 2009
Expanding strings in shell
$ a="my string"
$
Simple quote doesn't expand the variable:
$ echo 'This is $a'
This is $a
$
Double quote does expand the variable:
$ echo "This is $a"
This is my string
$
list processes with open sockets
lsof -i -r30 -n +c0
-r30: list every 30 secs
-n: do not resolve names
-i: sockets
+c0: do NOT truncate chars
Days between 2 unix epoch
#!/bin/bash
# Calculate "days diff" (unix epoch)
function daysdiff {
begin=${1:-0}
end=${2:-0}
let Sec=$end-$begin
if ((Sec < 0)); then
echo $((Sec/-86400))
else
echo $((Sec/86400));
fi
return 0
}
Mount a directory through ssh
# Mount as read only directory from
# remote host (username@host:/dirnamehost) to
# local host (dirnamelocalhost)
sshfs -o ro username@host:/dirnamehost dirnamelocalhost
# remember that the user must belong to the fuse group
# before doing the sshfs
usermod -a -G fuse username
# remote host (username@host:/dirnamehost) to
# local host (dirnamelocalhost)
sshfs -o ro username@host:/dirnamehost dirnamelocalhost
# remember that the user must belong to the fuse group
# before doing the sshfs
usermod -a -G fuse username
Working with date in shell
If you don't know what time is the unix time 1245257034
Or in UTC
To know what is the current unix time in your machine
Or in UTC
$ date -d @1245257034 -u
Wed Jun 17 16:43:54 UTC 2009
To know what is the current unix time in your machine
$ date +%s
1245257175
Getting last part of a string in shell script
# If we have the string
string="my string.hello.world"
# to get the last part considering the field separator = "." execute:
echo ${string##*.}
string="my string.hello.world"
# to get the last part considering the field separator = "." execute:
echo ${string##*.}
Suscribirse a:
Entradas (Atom)