380 likes | 483 Views
Variables in UNIX. How to run a shell program. Shell program can be run from: terminal prompt executable files which has the shell program Any file can be executable by changing the permission chmod + file(s)
E N D
How to run a shell program • Shell program can be run from: • terminal prompt • executable files which has the shell program • Any file can be executable by changing the permission chmod + file(s) • The executable file may have many lines, where every line has a correct shell program
How to run a shell program(continue.) • If any of the lines (shell program) has error (i.e. syntax), this will not prevent other lines from being executed • Whenever the shell executes the special chracter # at the start of a word, it takes whatever characters follow the # to the end of the line as comment
How to run a shell program(continue.) $ who | wc -l 11 $ cat > nu who | wc -l $ chmod +x nu $ ls -l nu -rwxr-xr-x 1 abuzneid 534 12 Oct 11 00:41 nu $ nu 11 $ nu >nufile $ cat nufile 11 $
How to run a shell program(continue.) $ cat > userstate #This is userstate script echo The current date and time is: date #To print the date echo echo The number of users on the system is: who | wc -l echo echo Your current working directory is: pwd $ chmod +x userstate $ userstate The current date and time is: Thu Oct 11 00:52:46 EDT 2001 The number of users on the system is: 11 Your current working directory is: /home/abuzneid/UNIX $
Variables • A shell variable begins with alphabetic or underscore "_" character, and is followed by zero or more alphabetic or underscore characters • variable=value • Must not be space before or after "=" • Example: my_bin=/usr/local/bin • Shell does not have the concept of data types
Displaying the value of variables • If a valid variable name followed the $, then the shell takes this as an indication that the value stored inside that variable is to be substituted of that point • Variables can be assigned to: • numbers • alphanumeric chracters/strings • other variables • utilities or programs • null values • file names
Displaying the value of variables(continue.) $ num=10 $ dir=/usr/local/bin $ echo $num 10 $ echo num num $ echo $dir /usr/local/bin $ echo dir dir $ echo $dir $num /usr/local/bin 10 $
Displaying the value of variables(continue.) arguments echo 10 arguments echo /usr/local/bin 10
Displaying the value of variables(continue.) $ my_UNIX_dir=/home/abuzneid/UNIX $ $my_UNIX_dir /home/abuzneid/UNIX: cannot execute $ command=who $ $command am i abuzneid pts/7 Oct 11 01:25 (216.87.102.220) $ nu='who | wc -l' $ $nu bgeorge pts/16 Oct 5 15:01 (216.87.102.204) msiles pts/3 Sep 27 17:44 (216.87.102.201) abakshi pts/7 Oct 11 01:25 (216.87.102.220) tphilip pts/11 Oct 2 14:10 (AC8C6085.ipt.aol.com) abuzneid pts/9 Oct 10 19:29 (avicenna.102.87.216.in-addr.arpa) $
Displaying the value of variables(continue.) $ value1=10 $ value2=value1 $ echo $value2 value1 $ value2=$value1 $ echo $value2 10 $
The null value • To set a variable to null • variable= • variable="" (double quote) • variable='‘ (single quote) • variable=" " with a space between the quotes is not a null variable (space is a character)
The null value (continue.) $ echo $null_variable $ echo :$null_variable: :: $ nosuch= $ echo :$nosuch: :: $
File name substitution as variables $ ls Documents foo mail personal TEST test $ x=* $ echo $x Documents TEST foo mail personal test $
File name substitution as variables(continue.) $x * args ison memos phonebook . . . students arguments echo
The$(variable) construct • To delimit the end of the variable name, enclose the entire name (but not the leading dollar sign) in a pair of curly braces • Example: ${filename} • Example: to rename a file from "myfile" to "myfileX"
The single Quote • When the shell sees the first single quote, it ignores any otherwise special characters that follow until it sees the closing quote $ cat phonebook Edward John 336-1454 Alice Stuart 334-1219 Sony Bell 332-3364 Robert Micheal 326-0569 $ grep Alice phonebook Alice Stuart 334-1219 $ grep Sony phonebook Sony Bell 332-3364 $ grep Sony Bell phonebook grep: can't open Bell phonebook:Sony Bell 332-3364 $ grep 'Sony Bell' phonebook Sony Bell 332-3364 $
The single Quote(continue.) • grep Chen Tao phonebook • grep 'Chen Tao' phonebook arguments Chen Tao phonebook echo arguments Chen Tao phonebook echo
The single Quote(continue.) • Single quotes preserves all white spaces $ echo one Two Three one Two Three $ echo 'one Two three' one Two three $ echo 'How are you doing, > today' How are you doing, today $
The single Quote (continue.) • Quotes are needed when assigning values containing white space or special characters to shell variables $ msg='I love New York' $ echo $msg I love New York $
The single Quote (continue.) • Shell does file substitution after variable name substitution even in between single quotes $ msg='* are my files' $ echo $msg Documents TEST foo list mail personal test are my files $
The double quote • Three characters are not ignored inside double quotes: • Dollar sign • Back quotes • Backslashes • File names substitution (*,?) is not done inside double quotes
The double quote(continue.) $ X=* $ echo $X Documents TEST foo list mail personal test $ echo '$X' $X $ echo "$X" * $
The double quote(continue.) $ address="169 University Ave > Bridgeport, CT 06601" $ echo $address 169 University Ave Bridgeport, CT 06601 $ echo "$address" 169 University Ave Bridgeport, CT 06601 $
The double quote (continue.) • Double quotes can be used to hide single quotes from the shell, and vice versa $ X="Hello, 'he said'" $ echo $X Hello, 'he said' $ Y='"he was here",Tom said' $ echo $Y "he was here",Tom said $
The Backslash • Equivalent to placing single quotes around a single character $ echo > syntax error: `newline or ;' unexpected $ echo \> > $ X=* $ echo \$X $X $ echo \\ \ $
The$(variable) construct $ myfile=foo $ mv $myfile $myfileX mv: Insufficient arguments (1) Usage: mv [-f] [-i] f1 f2 mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 $ mv $myfile ${myfile}X $
Passing arguments • Whenever you execute a shell program, the shell automatically stores the first argument in the special shell variable #1, the second argument in the variable #2, and so on. [$1,$2..$9] • These special variables are known as positional parameters
Passing arguments (continue.) $ cat > ison who | grep $1 $ who bgeorge pts/16 Oct 5 15:01 (216.87.102.204) abuzneid pts/17 Oct 11 01:17 (216.87.102.224) abuzneid pts/7 Oct 11 01:25 (216.87.102.220) Steve pts/9 Oct 10 19:29 (avicenna.102.87.216.in-addr.arpa) $ chmod +x ison $ ison abuzneid abuzneid pts/17 Oct 11 01:17 (216.87.102.224) abuzneid pts/7 Oct 11 01:25 (216.87.102.220) $ ison bgeorge bgeorge pts/16 Oct 5 15:01 (216.87.102.204) $
The $# variable • $# is set to the number of arguments that were typed on the command line $ cat >args echo $# arguments passed echo arg1=:$1: arg2=:$2: arg3=:$3: $ chmod +x args $ args a b c 3 arguments passed arg1=:a: arg2=:b: arg3=:c: $ args a b 2 arguments passed arg1=:a: arg2=:b: arg3=:: $ args 'ls' 1 arguments passed arg1=:ls: arg2=:: arg3=:: $
The $* variable • $* references all of the arguments passed to the program $ cat >args2 echo $# arguments passed echo they are :$*: $ chmod +x args2 $ args2 a b c 3 arguments passed they are :a b c: $ args2 * 10 arguments passed they are :Documents TEST args args2 fooX ison list mail personal test: $
The shift command • Maximum number of variables can be referenced by the shell is 9. Shell accepts single digit following the dollar sign • The shift command allows you to effectively left shift your positional parameters • I. e. after shift is executed, whatever was previously stored inside $2 will be assigned to $1, and whatever was stored in $3 will be assigned to $2, and so on.
The shift command (continue.) • The old value of $1 will be irretrievably lost • Whenever shift is executed, $# is also automatically decremented by one
The shift command (continue.) • Naturally, you should first save the value of $1 before shift, if the value is needed later in the program arg1=$1 shift arg10=$9 • The shift command is very useful when processing a variable number of arguments
References • UNIX SHELLS BY EXAMPLE BY ELLIE QUIGLEY • UNIX FOR PROGRAMMERS AND USERS BY G. GLASS AND K ABLES • UNIX SHELL PROGRAMMING BY S. KOCHAN AND P. WOOD