#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # variable: assignment, default value, unset #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> x=123 # no space allowed at both side of '=' prompt> echo $x # retrieve the value of x. 123 prompt> y=$x # x's value is assigned to y prompt> echo $y 123 prompt> y=${x-456} # if x is defined, its value is assigned to y # else 456 is assigned to y prompt> echo $y 123 prompt> unset x # Now x is removed prompt> y=${x-456} # the same statement but with different result prompt> echo $y 456 prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # three kinds of quote #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> y='$x'; echo $y # single quote: do nothing $x prompt> y="$y + 1"; echo $y # double quote: substitute var's value 456 + 1 # the type of the value is string prompt> z=`ls`; echo $z # back quote: execute a command ..... something like the result of executing `ls' prompt> z=$(ls); echo $z # another form for executing a command #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # environment variables #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> env # show the envirionment variables of this shell PATH=.... PWD=... ....... prompt> set # show all the variables (env vars are included) y=.... # y is a local var PATH=... # PATH is an env var PWD=.... # ..... ..... prompt> HELLO=aloha prompt> env | grep HELLO # nothing produced since HELLO is not an env var prompt> set | grep HELLO # try to show values of all variables HELLO=aloha # HELLO and its value are shown prompt> export HELLO # HELLO is now an env var prompt> env | grep HELLO HELLO=aloha prompt> echo $$ # show the pid of current shell 2012 prompt> bash # create a child which is also a shell prompt> echo $$ # Now the tty is occupied by the child shell 2046 prompt> env | grep HELLO # show the env var of this (child) shell HELLO=aloha # env vars of child are copied from parent prompt> HELLO=ohio # change the value prompt> env | grep HELLO HELLO=ohio prompt> exit # child shell terminated, return to parent shell prompt> echo $$ 2012 prompt> env | grep HELLO # show the env vars of this (parent) shell HELLO=aloha # parent's env vars are not affected by the child prompt> # # When a user logins, a shell is created. When the user issues a command, the # shell will create a child to execute. Since the envrionment variables of the # child (the command) are copied from parent (the login shell), the user can # set the value of an environment variable and it will be consulted by the # command. In this way, environment variables can be seen as implicit arguments. # prompt> env | grep PAGER # no env var named PAGER prompt> man ls # `man' will use his default pager: more ...... prompt> export PAGER=less # bash's way to assign and export simultaneously prompt> man ls # `man' now will use 'less' as the pager ..... prompt> PAGER=more man ls # do not change the env of current shell # but invoke the "man" with the changed enviroment #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # argument #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> cat show-arg #!/bin/bash # beware of the first two characters #! echo $# # $# : the number of arguments echo $* # $* : the list of argument (often used in for loop) echo $0 # the cmd itself echo $1 # the first argument echo $2 # the second argument prompt> bash show-arg 1 2 3 4 4 1 2 3 4 show-arg # the command 1 2 prompt> ./show-arg 1 2 3 4 bash: ./show-arg: bad interpreter: Permission denied prompt> chmod u+x show-arg prompt> ./show-arg 1 2 3 4 4 1 2 3 4 ./show-arg # the command 1 2 prompt> ./show-arg 1 1 1 ./show-arg # the command 1 # there is no second argument. prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # exit status of a process # success --> 0, true # failure --> >0, false #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> grep klim /etc/passwd # success --> return 0 klim:x:........... prompt> echo $? # show the exit status of the 0 # just terminated process prompt> grep oak /etc/passwd # failure --> return 1 prompt> echo $? 1 prompt> cat check-user #!/bin/bash if grep $1 /etc/passwd > /dev/null # chech if $1 existed then # run a command and treat its echo $1 is ok # return value as boolean else # success -> 0, true echo $1 is bad # failure -> 1, false fi prompt> ./check-user klim klim is ok prompt> ./check-user oak oak is bad prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # the `test' command #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> test -f eval-ex1.sh; echo $? # Is "eval-ex1.sh" a file? 0 # Yes. prompt> test -d eval-ex1.sh; echo $? # Is "eval-ex1.sh" a directory? 1 # NO. prompt> x=ab prompt> test $x = "ab" ; echo $? # Does x equal to "ab"? 0 # Yes. prompt> test $x < "abc"; echo $? # Is x less than "abc"? bash: abc: No such file or directory # Oh, '<' is a special char 1 prompt> test $x '<' "abc"; echo $? # ask again. 0 # Yes. prompt> [ $x = "ab" ]; echo $? # [ ... ] is a syntax sugar for 'test' 0 prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # if statement #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> if [ $x = "ab" ]; then echo equal; else echo no; fi equal prompt> x=cd prompt> if [ $x = "ab" ]; then echo equal; else echo no; fi no prompt> # the previous command is equal to if [ $x = "ab" ] if [ $x = "ab" ]; then then echo equal echo equal or else else echo no echo no fi fi # Whenever you meet a ';' in syntax, it may be replaced by a newline. prompt> help if if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi ........ #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # for loop #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> for i in 1 2 3; do > echo $i # the '>' is the prompt for continuation > done # After entering 'done', the 'for' is completed. 1 # the output is ... 2 3 prompt> for i in 1 2 3; do echo $i; done # the same as above but in one line 1 2 3 prompt> ls *.asm sicboot.asm sicldr.asm sictest.asm prompt> for f in `ls *.asm`; do echo $f; done # using the output of sicboot.asm # a command as the sicldr.asm # index list sictest.asm prompt> cat show-for-arg for arg in $*; do # using the argument echo $arg # list as the index list done prompt> bash show-for-arg hello i am klim hello i am klim prompt> for((i=0; i<10; i++)); do echo $i; done #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # the 'expr' command #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> x=1 prompt> x=`expr $x + 3`; echo $x 4 prompt> x=`expr $x * 2`; echo $x # '*' is special expr: syntax error prompt> x=1 prompt> x=`expr $x '*' 2`; echo $x 2 prompt> x=$((x % 2)); echo $x 0 prompt> x=$((x + 2)); echo $x 2 prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # while loop #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> x=a prompt> while [ $x '<' "aaaa" ]; do echo $x; x="${x}a"; done a # string comparison #string concatenation aa aaa prompt> x=1 prompt> while [ $x -lt 3 ]; do echo $x; x=`expr $x + 1`; done 1 # numeric comarison 2 prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # the 'read' command and the '<<' #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> read a # read from stdin, assign to a 1 prompt> echo $a 1 prompt> read a b # read from stdin, assign to a and b 1 2 3 prompt> echo $a 1 prompt> echo $b # 3 fields, 2 vars 2 3 prompt> read a b c # 2 fields, 3 vars 1 2 prompt> echo $a 1 prompt> echo $b 2 prompt> echo $c prompt> while read x; do echo "X = $x"; done # When nothing to read --> false klim X = klim milk X = milk prompt> cat show-while-read while read name score; do echo "hi $name, your score is $score" done << END-SCORE # << : the here document klim 101 milk 10 END-SCORE prompt> sh show-while-read hi klim, your score is 101 hi milk, your score is 10 prompt> prompt> cat show-usage cat << END-USAGE Be carefule! A cute dog inside ! END-USAGE prompt> sh show-usage Be carefule! A cute dog inside ! prompt> z='a b c e' prompt> read x y z <<< $z prompt> echo x x prompt> echo $x a prompt> echo $y b prompt> echo $z c e prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # function #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> cat k-func.sh foo() { echo $# echo $* } foo a b c d prompt> ./k-func.sh 4 a b c d prompt> #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # bulitin commands: . cd set unset ...... #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- prompt> cmd # fork another process to run cmd prompt> (cmd) # execute cmd in another shell prompt> . another-sh-script # execute another-sh-script in current shell