notes useful for shell

Creation: 19 juin 2013
Mise à jour:
Version: 1.0
Author: Jean-Louis Bicquelet-Salaün
Location: http://jlbicquelet.free.fr
Copyright: (c) 2013 Jean-Louis BICQUELET

tests

-eq numeric EQuality if [ "$x" -eq "4" ]
-ne numeric inquality if [ "$x" -ne "4" ]
-lt Less Than if [ "$x" -lt "4" ]
-le Less than or Equal if [ "$x" -le "4" ]
-gt Greater Than if [ "$x" -gt "4" ]
-ge Greater than or Equal if [ "$test" -ge "4" ]
-z String is zero length if [ -z "$test" ]
-n String is not zero length if [ -n "$test" ]
= String equality (cf. -eq) if [ "$test" = "bar" ]
-nt Newer Than if [ "$file1" -nt "$file2" ]
-ot Older Than if [ "$file1" -ot "$file2" ]
-e does the file exist if [ -e /etc/hosts ]
-d is a Directory if [ -d /bin ]
-f is a File if [ -f /bin/ls ]
-L is a link if [ -f /opt/oracle ]
-s is the file not empty if [-s /tmp/test]
-r is a Readable file if [ -r /bin/ls ]
-w is a Writable file if [ -w /bin/ls ]
-x is an eXecutable file if [ -x /bin/ls ]

examples:

if test 1 -lt 7
then echo "It's normal."
fi

if test 1 -gt 7 then echo "it's absurde." fi

if test -e ~/.profile then echo "~/.profile exist." else echo "~/.profile doesn't exist." fi

if test -d ~/.profile then echo "~/.profile is a directory." else echo "~/.profile isn't a directory." fi

if test -f ~/.profile then echo "~/.profile is a filer." else echo "~/.profile isn't a file." fi

if test ~/.exrc -nt ~/.profile then "~/.exrcis newer than ~/.profile." fi

conjonctions

&& Logical AND if [ "$test" -ge "0" ] && [ "$test" -le "9"]
|| Logical OR if [ "$test" -lt "0" ] || [ "$test" -gt "9" ] (not in Bourne shell)
! Logical NOT if [ "$test" != "bar" ]

examples:

[ -f test ] && echo "file test exist."

[ -f test ] || echo "file test doesn't exist."

[ ! -f test ] && echo "file test doesn't exist."

if [ -f test ] then echo "Le fichier test existe." else continue fi

if [ -f test ] then continue else echo "file test doesn't exist." fi

if [ ! -f test ] then echo "file test doesn't exist." else continue fi

variables spéciales

$$ PID of current shell echo "my PID = $$"
$! PID of last background command ls & echo "PID of ls = $!"
$? exit status of last command ls echo "ls returned code $?"
$0 Name of current command (as called) echo "I am $0"
$1 Name of current command’s first parameter echo "My first argument is $1"
$9 Name of current command’s ninth parameter echo "My ninth argument is $9"
$@ All of current command’s parameters (preserving whitespace and quoting) echo "My arguments are $@"
$ All of current command’s parameters (not preserving whitespace and quoting) echo "My arguments are $"

functions

Function definition function myfunc() { echo hello }

time and date

date +"%d/%m/%Y" 20/06/2013
date +"%d/%m/%y" 20/06/13
date +"%D" 06/20/13
date "+%a %D %T" Thu 06/20/13 09:42:28
date +"%A" Thursday
date +"%a" Thu
date +"%B" June
date +"%B" June
date +"%r" 09:19:32 AM
date +"%p AM
date +"%u" 4
date +"%T" 09:23:48
date +"%H:%M" 09:24
date +"%H%M" 0924
date +"%H:%M:%S" 09:25:04
date +"%H%M%S" 092530
date +"%I" 09
date +"%W" 24
date +"%j" 171

notes:

  • for %u number is from 1 (monday) to 7 (sunday)
  • for %p value is AM or PM
  • for %W week number
  • for %j day of the year number

logs files

To generate log files

logname=`date +%Y%m%d%H%M%S`.log 20130620144817.log