if exit_status
then
no_error/true commands
fi
if exit_status
then
no_error/true commands
else
error/false commands
fi
test –flag file
while exit_status
do
commands
done
until exit_status
do
commands
done
for variable in wordlist
do
commands
done
case string in
expression_1)
commands_1
;;
expression_2)
commands_2
;;
*)
default_commands
;;
esac
|
|
|
|
|
| Choice |
if exit_status
then
true_commands
else
false_commands
fi
|
case string in
exp_1)
commands_1
;;
exp_2)
commands_2
;;
*)
default_cmds
;;
esac
|
|
| Loops |
while exit_status do commands done |
until exit_status do commands done |
for var in list do commands done |
trap "commands" signal_list
$ cat > lsl
ls –l
<ctrl-d>
chmod +x lsl
PATH PS1 CWD UID
$ echo Hello, world Hello, world $ echo UID $ UID $ echo $UID 1000 $ echo The value of UID is $UID The value of UID is 1000
$ PS1="What next?" What next? Echo Prompt is now $PS1 Prompt is now What next? What next?
$ myvar="pre" $ echo Value of myvar has been $myvar viously set Value of myvar has been pre viously set
$ echo Value of anyvar has been $anyvar viously set Value of anyvar has been viously set
$ echo Value of myvar has been $myvarvariously set Value of myvar has been set
$ myvar="pre"
$ echo Value of my var has been ${myvar}viously set
Value of myvar has been previously set
$ UID=2000
$ PATH=/bin:/usr/bin:$HOME/bin:. $ export PATH
$ PATH=$PATH:$HOME/bin $ export PATH
$ varval PATH=/bin:/usr/bin:$HOME/bin:. PS1=$ PWD=/home/mark/scripts
$ cp crucial.txt safe.txt
$ lsl /bin
ls –l $1
$ ls –l /bin
echo –n "Enter some text: " read text echo You typed $text
echo –n "Enter some text: " read first second restofinput echo First word: $first echo Second word: $second echo Rest of input: $restofinput
/home/mark/bin $ currentdir=`pwd` $ echo $currentdir /home/mark/bin
$ cd /notexit sh: /noexist: No such file or directory $ echo $? 1 $ echo $? 0
S cat > nargs echo $# <ctrl-d> $ chmod +x nargs $ nargs one two three 3
$ cat > showargs echo $* <ctrl-d> $ chmod +x showargs $ showargs one two three one two three
# Usage: exists username if grep "^$1:" /etc/passwd > /dev/null 2> /dev/null then echo The user $1 exists exit 0 else echo The user $1 does not exist exit 1 fi
if exists mark then commands fi
if test –e $1 then echo The file $1 exists else echo The file $1 does not exist fi
/bin:/usr/bin:/home/mark/bin:.
$ chkpath /usr/bin 0 $ chkpath /bad_dir 1
while test –e $1 do sleep 2 done echo File $1 does not exist
until grep "^scs:" /etc/passwd > /dev/null 2> /dev/null do sleep 5 done
$ doubleln <congrats.txt Congratulations on your choice of the Armstrong and Mason virus security product. If installed with care, it will give years of trouble-free protection. $ ls –l | doubleln -rw-r--r-- 1 mark 1000 92365 Dec 3 20:37 bugs.txt -rw-r--r-- 1 mark 1000 152 Dec 3 20:37 congrats.txt $
for i in 1 2 3 4 5 do echo Value of 1 is $1 done
Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4 Value of i is 5
The following example will display all the command line arguments, each on a separate line:
for i in $* do echo $i done
case $# in
1)
cat >> $1
;;
2)
cat >> $1 < $2
;;
*)
echo Usage: append out_file [in_file]
;;
esac
exit 0
$ fsize am_virus *.txt am_virus 132991 bugs.txt 92365 congrats.txt 152
trap "" 2 3
trap "rm /tmp/tmp$$; exit 0" 2 3 touch /tmp/tmp$$ trap 2 3
echo PATH=$PATH echo PS1=$PS1 echo PWD=$PWD echo UID=$UID
echo $PATH | grep "$1" > /dev/null echo $?
echo $PATH | grep "$1" > /dev/null exit $?
if chkpath 1 then exit 1 else PATH=$PATH:$1 exit 0 fi
$ . addpath
while read line do echo $line echo done
if test –z $1 then echo Usage: chkpath pathname exit –1 fi
# chkpath
case $# in
1)
echo $PATH | grep "$1" > /dev/null
exit $?
;;
*)
echo Usage: chkpath pathname
exit -1
;;
esac
# addpath
case $# i
1)
if chkpath $1
then
exit 1
else
PATH=$PATH:$1
exit 0
fi
;;
*)
echo Usage: . addpath pathname
exit –1
;;
esac
for i in $* do echo –n $i echo –n " " echo `wc –c < $i` done