Shell scripting - goodies
Jump to navigation
Jump to search
Input
Input from scripts/outside
$$ = The PID number of the process executing the shell. $? = Exit status variable. $0 = The name of the command you used to call a program. $1 = The first argument on the command line. $2 = The second argument on the command line. $n = The nth argument on the command line. $* = All the arguments on the command line. $# The number of command line arguments.
Conditions
IF
Checks available (if [ -<check> ])
-b file = True if the file exists and is block special file. -c file = True if the file exists and is character special file. -d file = True if the file exists and is a directory. -e file = True if the file exists. -f file = True if the file exists and is a regular file -g file = True if the file exists and the set-group-id bit is set. -k file = True if the files' "sticky" bit is set. -L file = True if the file exists and is a symbolic link. -p file = True if the file exists and is a named pipe. -r file = True if the file exists and is readable. -s file = True if the file exists and its size is greater than zero. -s file = True if the file exists and is a socket. -t fd = True if the file descriptor is opened on a terminal. -u file = True if the file exists and its set-user-id bit is set. -w file = True if the file exists and is writable. -x file = True if the file exists and is executable. -O file = True if the file exists and is owned by the effective user id. -G file = True if the file exists and is owned by the effective group id. file1 –nt file2 = True if file1 is newer, by modification date, than file2. file1 ot file2 = True if file1 is older than file2. file1 ef file2 = True if file1 and file2 have the same device and inode numbers. -z string = True if the length of the string is 0. -n string = True if the length of the string is non-zero. string1 = string2 = True if the strings are equal. string1 != string2 = True if the strings are not equal. !expr = True if the expr evaluates to false. expr1 –a expr2 = True if both expr1 and expr2 are true. expr1 –o expr2 = True is either expr1 or expr2 is true.
If (string and numeric check)
(1) s1 = s2 (2) s1 != s2 (3) s1 < s2 (4) s1 > s2 (5) -n s1 (6) -z s1 (1) s1 matches s2 (2) s1 does not match s2 (3) __TO-DO__ (4) __TO-DO__ (5) s1 is not null (contains one or more characters) (6) s1 is null #!/bin/bash S1='string' S2='String' if [ $S1=$S2 ]; then echo "S1('$S1') is not equal to S2('$S2')" fi if [ $S1=$S1 ]; then echo "S1('$S1') is equal to S1('$S1')" fi 11.4 Arithmetic relational operators -lt (<) -gt (>) -le (<=) -ge (>=) -eq (==) -ne (!=)
Loop
For
for NAME [in LIST ]; do COMMANDS; done EX: for i in `cat list`; do cp "$i" "$i".bak ; done