1 / 42

More Variables

More Variables. Parameter substitution. Parameter substitution (continue.). The $0 variable. Shell stores the name of the program inside the special variable $0. $ cat foo $0 is running!! $ foo foo: cannot fork: no swap space $. The set Command. The set Command (continue.).

avel
Download Presentation

More Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Variables

  2. Parameter substitution

  3. Parameter substitution (continue.)

  4. The $0 variable • Shell stores the name of the program inside the special variable $0 $ cat foo $0 is running!! $ foo foo: cannot fork: no swap space $

  5. The set Command

  6. The set Command (continue.)

  7. The set Command (continue.)

  8. The –x option • Turns on trace mode in the current shell • Traced commands are preceded by plus (+) sign, but variable assignments are not • Subshells can be traced by running the shell with the –x option followed by the name of the program to be executed sh -x program • To turn off trace mode: set +x • Any number of set -x and set +x can be inserted inside the program to turn trace on and off as desired

  9. The –x option (continue.) $ x=* $ set -x set command trace option $ echo $* + echo set command trace option set command trace option $ mysl=ls mysl=ls $ ls | wc -l + ls + wc -l 32 $

  10. set with no Arguments • set with no arguments gives an alphabetized list of all the variables whether they are local or exported $ set . . . WD=/home/sbenayed SHELL=/bin/csh TCAT=/usr/lib/lp/postscript/dpost TERM=vt100 TZ=US/Eastern UNIX=/home/abuzneid/UNIX USER=sbenayed mysl=ls x=*

  11. Using set to reassign Positional Parameters $ set a b c d + set a b c d $ echo $1:$2:$3:$4 + echo a:b:c:d a:b:c:d $ echo $# + echo 4 4 $ echo $* + echo a b c d a b c d $ for arg; do echo $arg; done syntax error: `;' unexpected $

  12. Using set to reassign Positional Parameters (continue.) # # Count words on a line # read line set $line echo $# $ parse + parse I love New York city 6 $

  13. The -- option • If the arguments passed to set has (-) character, it will connect it as an option • If there is white space arguments, a list of the variables in the current shell will be displayed • If – option is used • set will interpret any subsequent arguments as option • prevents set from displaying all variables in the current shell

  14. Shell Program: parse • Counts all the words on standard input • To view the source code of parse click here $ parse < $HOME/.profile 2 $ wc -w < $HOME/.profile 12 $

  15. The IFS Variable • Internal Field Separator • Shell uses IFS when • passing input from the read command • output from command substitution (back-quotating) • performing variable substitution • The actual characters that are stored in IFS where 040 is whit espace, 011 is tab character and 012 the new line character (the second 012 comes from echo) $ echo "$IFS" | od -b 0000000 040 011 012 012 0000004

  16. The IFS Variable (continue.) • IFS can be changed to any character $ read line This is a line $ echo "$line" This is a line $ IFS=" > " $ read line set it to just a line This is a line $ echo "$line" This is a line leading space preserved $

  17. The IFS Variable (continue.) • Shell doesn't use the IFS when performing variable assignment • Changing the IFS is often done in conjunction with execution of the set command $ var=x:y:z $ echo "$var" x:y:z $ line="Micro Logic Corp.:BOX 174:Hachensack, NJ 07602" $ IFS=: $ set $line $ echo $# 3 $ for field; do echo $field; done syntax error: `;' unexpected

  18. The readonly Command • Specify variables when values cannot be subsequently changed • To get a list of readonly variables • readonly variables attribute is not passed down to subshells $ MYPATH=/usr/bin:: $ readonly MYPATH $ MYPATH=/usr/local/bin:: MYPATH: is read only $ $ readonly readonly MYPATH

  19. The readonly Command (continue.) • Once a variable has been made readonly in a shell, there is no way to undo it

  20. The unset Command • Removes the definition of a variable from the environment or a function • Cannot unset: • readonly variables • IFS, MAIL, CHECK, PATH, PS1, PS2 $ v=100 $ echo $v 100 $ unset v $ echo $v removes v from the environment $

  21. The eval Command • Format: eval command_line • eval makes the shell scan the command line twice before executing it $ pipe="|" $ ls $pipe wc -l |: No such file or directory wc: No such file or directory -l: No such file or directory $ eval ls $pipe wc -l 45 $

  22. The wait Command • Format: wait process_id • If process_id is omitted, then the shell waits for all child process to complete execution $ sort data >sorted_data & 2517 $ ls addi function1.sql monitor3 procedure1.sql varfile6 args greetings mycp rem charis2 mail var memos creation1.sql personal varfile3 $ wait 2517 $

  23. The $! Variable • Shell stores the process_id of the last command executed in the background in $! variable program1 & pid1=$: … program2 & pid2=$! … wait $pid1 * waits for program1 to finish … wait $pid2 * waits for program2 to finish

  24. The trap Command • Format: trap command signals where commands is one or more commands that will be executed whenever any of the signals specified by signals is received • The commands that are specified to trap must be enclosed in quotes if they contain more than one command

  25. The trap Command (continue.)

  26. The trap Command (continue.)

  27. The trap Command (continue.) • Shell scans the command line at: • the line when trap gets executed, and • when one of the listed signals is received • If commands are put inside double quotes, variables substitution will occur when trap is executed • If commands are put inside single quotes, variables substitution will occur when one of the signals is received $ trap "rm $HOME/foo$$; exit" 2 $ trap "rm $HOME/foo$$; exit" 21 $ trap `rm $HOME/foo$$; exit` 21

  28. trap with no Arguments • trap with no arguments displays changed traps $ trap 'echo logged off at `date` >>$HOME/logoffs' 0 $ trap 0: echo logged off at `date` >>$HOME/logoffs $ egypt%

  29. Ignoring signals

  30. Redirect to standard error • Format: command >& file-descriptor redirects the standard output for command to file_descriptor • file_descriptor could be • 0 (standard input) • 1 (standard output) • 2 (standard error) • echo "Error.. ." >&2 writes an error to standard error

  31. Redirect to standard error (continue.) • command >file 2>>file is identical to command >file 2>&1 where standard output s redirected to file and standard error is redirected to standard output

  32. <&- and >&- • >&- closes standard output • <&- closes standard input • ls>&- will display nothing because standard output is closed

  33. In_line Input Redirection –Here document (continue.) • ln_lineInput reads the input from the same file

  34. In_line Input Redirection –Here document $ wc -l <<KEY > a > aa > aaa > aaaa > KEY 4 $

  35. Functions • Format: name() { command; ... Coomand;} • Arguments listed after the function on the command line are assigned to the positional parameters $1, $2… • functions can not be passed to subshells • Changes made to the current directory or to variables remain after the function has completed execution • unset command is used to remove a definition of a function

  36. Functions (continue.) $ cd UNIX $ db () { > PATH=$PATH:/$HOME/UNIX > PS1="`who am i`" > cd /; > } $ db sbenayed pts/2 Feb 1 08:13 (1Cust229.tnt28.nyc3.da.uu.net)

  37. The return command • Format: return n • n: returns the status of the function • function returns the exit status of last command executed in the function if: • n is omitted • the function does not return command at the end • return status is equivalent to the exit status which its value can be accessed through the shell variable $?

  38. The type command • type tells what is the comman: • function • shell program • shell buit_in command • standard UNIX command

  39. The type command (continue.) $ mywho () { who am i; } $ type ls ls is /bin/ls $ type cat cat is /bin/cat $ type telnet telnet is /bin/telnet $ type mywho mywho is a function mywho(){ who am i } $

  40. The restricted shellrsh • rsh restricts user from certain actions that the standard shell allows • rsh disallows: • change directory • change PATH or SHELL variables • specify a path to a command • redirect output (> and >>) • exec programs

  41. The ulimit Command • Format: ulimit size • This command tells the shell to set the maximum size of files that can be written by child processes to sizeblocks $ ulimit unlimited $ ulimit 1000 $ ulimit 1000 $

  42. 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

More Related