Miscellaneous: Shell scripts

(last edit: 2000-11-22) #!/bin/sh lets the kernel know that the Bourne Shell will execute the lines in the program (you can ofcourse specify any shell you want to use) Debugging a shell script user@host:~#sh -x [shell script] This wil display each command and it's output. Command substitution: Is done using backquotes currentdir = `pwd` Will have the value of the current working dir currentdir = pwd Will have the value "pwd" If statements if [ expression ] then else fi The spaces in [ expression ] must be there!!! If not you get a syntax error As expression you can use: int1 -eq int2 Equal int1 -ne int2 Not equal int1 -gt int2 Greater than int1 -ge int2 Greater than or equal to int1 -lt int2 Less than int1 -le int2 Less than or equal to string1 = string2 Equal string1 != string2 Not equal string String is not null -z string True if the length of string is zero. -n string True if the length of string is non-zero. -d filename True if dir exists -f filename True if exists as non dir -s filename True if contains at least one character expr1 -a expr2 And expr1 -o expr2 Or \( expr \) Grouping expressions example: if [ "$answer" = "Y" -o "$answer" = "y" ] Splitting strings By default the IFS variable is set to a whitespace, you can reset this ofcourse.. #!/bin/sh input="this:is:a:test" printf "\n\n" IFS=: for section in $input do printf "$section \n" done printf "\n\n"

Click here to go back to the index.