1 / 13

More Shell Programming

More Shell Programming. Learning Objectives: To learn the usage of environment (shell) variables in shell programming To understand the handling of command line arguments To illustrate more useful commands in shell programming using examples. More Shell Programming. Table of Content

rock
Télécharger la présentation

More Shell Programming

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 Shell Programming Learning Objectives: To learn the usage of environment (shell) variables in shell programming To understand the handling of command line arguments To illustrate more useful commands in shell programming using examples

  2. More Shell Programming Table of Content • Keyword Shell Variables • Keyword Example • Read-only Shell Variable • Command Line Argument • Command for handling arguments - shift • Command for handling arguments - set • Special Variable - $$ • Looping using for • Looping using for – More Examples • Return Status Example • << Examples

  3. Keyword Shell Variables • The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where the shell looks for executables USER Your login name SHELL The name of the shell you are running PWD The current working directory PRINTER Can be loaded with your default printer

  4. Keyword Example $ cat env #!/bin/sh echo "Hi $USER!" echo "Your home is: $HOME" echo "Your path is: $PATH" echo "Your current directory is: $PWD" echo "Your shell is: $SHELL" echo "Your printer is: $PRINTER" $ env Hi qyang! Your home is: /homes/qyang Your path is:/usr/bin:.:.:/homes/horner/Unix/bin:... Your current directory is: /homes/qyang/111 Your shell is: /bin/csh Your printer is: csl3

  5. Read only Shell Variables • Read Only means you cannot change it; just read its value • $0 is the program name (the filename the user typed to invoke the shell script): $ cat print1 #!/bin/sh echo "This script is called $0" $ print1 This script is called print1 $ ./print1 This script is called ./print1 $ ~/111/print1 This script is called /homes/qyang/111/print1

  6. Command Line Arguments – (1) • The command line arguments that you call a script with are stored in variables $1, $2, ..., $9. $ cat args1 #!/bin/sh echo "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9" $ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 • With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

  7. Command Line Arguments – (2) • Example: How to write a command to swap two files? $ cat swap #!/bin/sh mv $1 /tmp/$1 mv $2 $1 mv /tmp/$1 $2 $ cat it1 contents of file1 $ cat it2 contents of file2 $ swap it1 it2 $ cat it1 contents of file2 $ cat it2 contents of file1 $

  8. Command Line Arguments – (3) • $* lists all the command line args: $ cat args2 #!/bin/sh echo "The args are $*" $ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 • $# contains the number of args: $ cat args3 #!/bin/sh echo "The number of args is $#" $ args3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The number of args is 10

  9. Looping using for – Example 1 • The for statement executes a loop once for each of a list of possibilities: $ cat printall #!/bin/sh for file in * do if [ -f $file ] then echo "Print $file [y/n]? " read resp if [ $resp = "y" ] then lpr -Pcll3 $file fi fi done $ printall Print letter1 [y/n]? y Print names [y/n]? n

  10. Looping using for - Example 2 • The in clause of the for statement accepts as many parameters as you wish in many forms: $ for i in 1 2 3 ; do echo $i ; done 1 2 3 $ for pid in `ps -a | tail +2 | cut -c1-6 | sort` > do > kill -9 $pid > done kill: permission denied kill: permission denied kill: permission denied (you will then be logout!)

  11. Looping using for – Example 3 • We can make use of the find command to generate a list of files in a sub-directory tree for processing: #!/bin/sh if [ $# = 0 ] then echo "usage: treeproc <pattern> [<command>]"; exit fi pattern="$1"; shift if [ $1 ] ; then cmd=$*; else cmd=echo; fi echo "Running \"$cmd\" for $pattern in dir: `pwd`" for file in `find . -name "$pattern" -print` do $cmd $file done • The shift command promotes each command line argument by one (e.g., the value in $2 moves to $1, $3 moves to $2, etc.) $ find . -name ‘letter*’ -print ./secret/letter1 ./secret/letter2

  12. Looping using for – Example 3 testing $ ls -R.: names letter0 secret/ ./secret: letter1 letter2 $ treeproc usage: treeproc <pattern> [<command>] $ treeproc "*1" Running "echo" for *1 in dir: /home/kwchiu ./secret/letter1 $ treeproc "letter*" rm -i Running "rm -i" for letter* in dir: /home/kwchiu rm: remove ./letter0 (yes/no)? rm: remove ./secrete/letter1 (yes/no)? rm: remove ./secrete/letter2 (yes/no)? $ find . -name "letter*" -exec rm -i {} \; (same as the above) {} substitutes each filename \ denotes end of the exec command

  13. Example 4: Here documents << • Documents can be embedded in a shell script with << with shell variable processing (e.g., mail merge). w | tail +3 | ( while read user tty login idle other do cmd=`echo $other | tr -s '[0-9]' ' '` if [ `echo $idle | grep -c day` -ge 1 ] then mail $user << EOF Dear $user, May I remind you that your session has been idle for $idle. The running program was: $cmd Regards, SysAdmin. EOF fi done ) You may use any words – EOF is just an example 1:20pm up 41 day(s), 3:36, 5 users, load average: 0. User tty login@ idle JCPU PCPU what wfung pts/2 Thu10pm 10:58 15 14 telnet wfung pts/4 Sat12am 11:23 1 telnet dominic pts/3 Fri10am 2days -bash

More Related