Shell Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Shell Programming

Description:

The conditional construct is: if command; then. command. fi. For example, if tty -s; then ... This is very useful for constructing temporary files. tmp=/tmp/cal0 ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 27
Provided by: kaustav
Category:

less

Transcript and Presenter's Notes

Title: Shell Programming


1
Shell Programming
  • Kaustav Ghoshal
  • kghoshal_at_tamu.edu

2
The Basics
  • What is a shell ?
  • Why use a shell script?
  • Creating a Script
  • Replace find . -name file print by a command
    sfind file
  • Create a shell script
  • vi sfind
  • cat sfind
  • find . -name 1 -print
  • chmod x sfind
  • cd /sbin
  • sfind init
  • ./sfind /etc/password

3
The Basics (contd..)
  • The use of the !
  • Comments
  • Argument Checking
  • if -ne 3 then
  • echo 1gt2 Usage 0 19 Oct 91
  • exit 127
  • fi
  • This script requires three arguments and works
    accordingly.

4
Exit Status
  • Exit status
  • is the year out of range for me?
  • if year -lt 1901 -o year -gt 2099 then
  • echo 1gt2 Year \"year\" out of range
  • exit 127
  • fi
  • etc...
  • All done, exit ok
  • exit 0
  • A non-zero exit status indicates an error
    condition of some sort while a zero exit status
    indicates things worked as expected.

5
Exit Status
  • Using exit status
  • Exit codes are important for those who use your
    code. Many constructs test on the exit status of
    a command.
  • The conditional construct is
  • if command then
  • command
  • fi
  • For example,
  • if tty -s then
  • echo Enter text end with \D
  • fi

6
File Descriptors
  • Stdin, Stdout, Stderr
  • Standard input, output, and error are file
    descriptors 0, 1, and 2. Each has a particular
    role and should be
  • used accordingly
  • is the year out of range for me?
  • if year -lt 1901 -o year -gt 2099 then
  • echo 1gt2 Year \"year\" out of my range
  • exit 127
  • fi
  • etc...
  • ok, you have the number of days since Jan 1,
    ...

7
File Descriptors
  • case expr days 7 in
  • 0) echo Mon
  • 1) echo Tue
  • etc...
  • Error messages should appear on stderr not on
    stdout! Output should appear on stdout.

8
The quotes
  • Quotes
  • Single Quotes
  • The result is one word
  • grep gid /etc/group awk -F print 1
  • Double Quotes
  • Within double quotes you have variable
    subsitution.
  • if ! "parent" then
  • parentpeople/group/user
  • fi
  • Back Quotes
  • Back quotes mean run the command and substitute
    the output.
  • TODAY(set \date\ echo 1)

9
Language Constructs
  • Language Constructs
  • For loop iteration
  • Substitute values for variable and perform task
  • for variable in word ...
  • do
  • command
  • done
  • For example
  • for i in cat LOGS
  • do
  • mv i i.TODAY
  • cp /dev/null i
  • chmod 664 i
  • done
  • Alternatively you may see
  • for variable in word ... do command done

10
Language Constructs
  • Case
  • Switch to statements depending on pattern match
  • case word in
  • pattern pattern ... )
  • command ...
  • esac

11
Language Constructs
  • For example
  • case "year" in
  • 0-90-9)
  • year19year
  • yearsexpr year - 1901
  • 0-90-90-90-9)
  • yearsexpr year - 1901
  • )
  • echo 1gt2 Year \"year\" out of range ...
  • exit 127
  • esac

12
Language Constructs
  • Pattern Matching in case Statements
  • ?) Matches a string with exactly one character
    like a, 3,!
  • ?) Matches a string with one or more characters
    (a non-empty string).
  • yYyYeEsS)
  • Matches y, Y or yes, YES, YeS, etc. The means
    "or."
  • //0-9)
  • Matches a file pathname, like /xxx/yyy/somedir/fil
    e2, that starts with a slash, contains at least
    one more slash, and ends with a digit.
  • 'What now?')
  • Matches the pattern What now?
  • "msgs")
  • Matches the contents of the msgs variable

13
Language Constructs
  • Conditional Execution
  • Test exit status of command and branch
  • if command
  • then
  • command
  • else
  • command
  • fi
  • For example
  • if -ne 3 then
  • echo 1gt2 Usage 0 19 Oct 91
  • exit 127
  • fi
  • Alternatively you may see
  • if command then command else command fi

14
Language Constructs
  • While/Until Iteration
  • Repeat task while command returns good exit
    status.
  • while until command
  • do
  • command
  • done
  • For example
  • for each argument mentioned, purge that
    directory
  • while -ge 1 do
  • rm 1
  • shift
  • done
  • Alternatively you may see
  • while command do command done

15
Variables
  • Variables
  • Variables are sequences of letters, digits, or
    underscores beginning with a letter or
    underscore. To get the contents of a variable you
    must prepend the name with a .
  • Numeric variables (eg. like 1, etc.) are
    positional variables for argument communication.
  • Variable Assignment
  • Assign a value to a variable by variablevalue.
    For example
  • PATH/usr/ucb/usr/bin/bin export PATH
  • Or TODAY(set \date\ echo 1)

16
variables
  • Referencing Variables
  • Use variable (or, if necessary, variable) to
    reference the value.
  • if "USER" ! "root" then
  • PATHHOME/binPATH
  • else
  • PATH/etc/usr/etcPATH
  • fi
  • The braces are required for concatenation
    constructs.
  • p_01 ?The value of the variable "p_01".
  • p_01 ?The value of the variable "p" with "_01"
    pasted onto the end.

17
Export
  • Exporting Variables
  • Variables are not exported to children unless
    explicitly marked.
  • We MUST have a DISPLAY environment variable
  • if "DISPLAY" "" then
  • if tty -s then
  • echo "DISPLAY (hostname0.0)? \c"
  • read DISPLAY
  • fi
  • if "DISPLAY" "" then
  • DISPLAYhostname0.0
  • fi
  • export DISPLAY
  • fi

18
Variable reference
  • Conditional Reference
  • Operator Explanation
  • var-default If var is not set or is empty,
    use default instead.
  • vardefault If var is not set or is empty,
    set it to default and use that value.
  • varinstead If var is set and is not empty,
    use instead. Otherwise, use nothing (null
    string).
  • var?message If var is set and is not empty,
    use its value. Otherwise, print message, if any,
    and exit from the shell. If message is missing,
    print a default message (which depends on your
    shell).

19
Variable reference
  • Conditional Reference (example)
  • !/bin/sh
  • if cp "1" "1.bak"
  • then VISUAL-/usr/ucb/vi "1
  • exit
  • USE STATUS FROM EDITOR
  • else echo "basename 0 quitting can't make
    backup?" 1gt2
  • exit 1
  • fi

20
Speacial variables
  • Special Variables
  • Current process id. This is very useful for
    constructing temporary files.
  • tmp/tmp/cal0
  • trap "rm -f tmp /tmp/cal1 /tmp/cal2"
  • trap exit 1 2 13 15
  • /usr/lib/calprog gttmp
  • ?
  • The exit status of the last command.
  • command
  • Run target file if no errors and ...
  • if ? -eq 0
  • then
  • etc...
  • fi

21
functions
  • Functions
  • Syntax is
  • name ()
  • commands
  • For example
  • purge()
  • if ! -d 1 then
  • echo 1 No such directory 1gt2
  • return
  • fi
  • Within a function the positional parmeters 0,
    1, etc. are the arguments to the function (not
    the arguments to the script).
  • Within a function use return instead of exit.

22
More useful stuff
  • Test
  • if test expression then
  • etc...
  • if expression then
  • etc...
  • Useful expressions are
  • test -w, -r, -x, -d, ... filename
  • test n1 -eq, -ne, -gt, ... n2
  • test s1 , ! s2
  • test cond1 -o, -a cond2
  • Binary or binary and use ! for unary negation.
  • For example
  • if year -lt 1901 -o year -gt 2099 then
  • echo 1gt2 Year \"year\" out of range
  • fi

23
String matching
  • String matching
  • while -ge 1 do
  • case 1 in
  • -c) rateecho 1 cut -c3-
  • -c) shift rate1
  • -p) prefixecho 1 cut -c3-
  • -p) shift prefix1
  • -) echo Usage exit 1
  • ) disks break
  • esac
  • shift
  • done

24
Traps
  • Trapping Exits Caused by Interrupts
  • stat1
  • temp/tmp/zpg
  • trap 'rm -f temp exit stat' 0
  • trap 'echo "basename 0Quitting early." 1gt2'
    1 2 15
  • Signals
  • 0 EXIT exit command
  • 1 HUP When session disconnected
  • 2 INT Interrupt - often CTRL-c
  • 3 QUIT Quit - often CTRL-\
  • 15 TERM From kill command

25
Miscellaneous Stuff
  • Reading from the Keyboard
  • echo -n "Enter first and last name " read fn ln
  • The character
  • while do commands done
  • if something then else commands fi

26
Mathematical expressions
  • Expr
  • Syntax expr arg1 operator arg2 operator arg3
    ...
  • Arithmetic Operators like - /
  • Relational Operators like ! gt gt lt lt
  • Logical operators like
Write a Comment
User Comments (0)
About PowerShow.com