cs 497c introduction to unix lecture 33 shell programming
Download
Skip this Video
Download Presentation
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming

Loading in 2 Seconds...

play fullscreen
1 / 20

CS 497C – Introduction to UNIX Lecture 33: - Shell Programming - PowerPoint PPT Presentation


  • 113 Views
  • Uploaded on

CS 497C – Introduction to UNIX Lecture 33: - Shell Programming. Chin-Chih Chang [email protected] The if Conditional. The if statement takes two-way decisions depending on the fulfillment of a certain condition. There are three forms of the if statement:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS 497C – Introduction to UNIX Lecture 33: - Shell Programming' - walda


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the if conditional
The if Conditional
  • The if statement takes two-way decisions depending on the fulfillment of a certain condition.
  • There are three forms of the if statement:
    • if condition then commands else commands fi
    • if condition then commands fi
    • if condition then commands

elif condition then commands else commands fi

$ emp3.sh firewall

the if conditional1
The if Conditional

$ emp3.sh mail

$ emp3a.sh firewall

$ emp3a.sh mail

  • We saw two forms of the if conditional – if-then-fi and if-then-else-fi. There’s a third form – if-then-elif-then-else-fi.

$ cronfind.sh “find */”

test and companions of if
Test and []: Companions of if
  • When you utilize if to evaluate expressions, the test statement is often used as its control command.
  • test ueses certain operators to evaluate the condition on its right and returns either a true or false exit status, test works in three ways:
    • Compare two numbers.
    • Compare two strings or a single one for null value.
    • Checks a file’s attributes.
test and companions of if1
Test and []: Companions of if
  • Test doesn’t display any output but simply returns a value that sets the parameter $?.
  • The numerical comparison operators used by test are:
    • -eq: equal to
    • -ne: not equal to
    • -gt: greater than
    • -ge: greater than or equal to
    • -lt: less than
    • -le: less than or equal to
test and companions of if2
Test and []: Companions of if

$ cat arg_number_check.sh

#!/bin/sh

if test $# -ne 3 ; then

echo "You didn\'t enter three arguments"

else

echo "You entered the right number"

fi

  • The test statement has a shorthand – a pair of rectangular brackets.
test and companions of if3
Test and []: Companions of if
  • The following two forms are equivalent:

test $x –eq $y

[$x –eq $y]

  • Test can be used to compare strings with the following operations:
    • s1 = s2: true if s1 = s2
    • s1 != s2: true if s1 is not equal to s2
    • stg: true if stg is assigned and not null
    • -n stg: true if stg is not a null string
test and companions of if4
Test and []: Companions of if
    • -z stg: true if stg is a null string
    • s1 == s2: true if s1 = s2 (korn and bash only)
  • The compile.sh script is used to compile the last modified c or java programs.
  • test can be used to test various file attributes (Page 554 Table 18.4):
    • -f fname: fname exists and is a regular file.
    • -f fname: finame exists and is readable.
    • -w fname: fname exists and is writable.
test and companions of if5
Test and []: Companions of if

#!/bin/sh

if [ $# -eq 1 ] ; then

if [ $1 = "j" ] ; then

file=`ls -t *.java | head -1`

javac $file

elif [ $1 = "c" ] ; then

file=`ls -t *.c | head -1`

cc $file && a.out

else

echo "Invalid file type"

fi

else

echo "Usage: $0 file_type\nValid file types are c and j"

fi

test and companions of if6
Test and []: Companions of if
    • -x fname: fname exists and is executable.
    • -d fname: finame exists and is a directory.
  • The ! negates a test, so [! –w file] negates [-w file].
  • Check the file filetest.sh.

$ filetest.sh emp3.lst

$ filetest.sh emp.lst

the case conditional
The case Conditional
  • case is a compact string-matching construct and is closed with esac.
  • It uses the shell’s wild cards to match multiple patterns in egrep-style.
  • The * when used as the last option matches everything not matched by the previous options. The wild cards match strings and not files.
  • case is specially suitable for matching the filename $0.
the case conditional1
The case Conditional
  • Here is its syntax:

case expression in

pattern1) commands1;;

pattern2) commands2;;

….

esac

  • case first matches expression with pattern1. If the match succeeds, then it executes commands1. Otherwise, then go to pattern2.
the case conditional2
The case Conditional

#!/bin/sh

tput clear

echo "\n 1. Find files modified in last 24 hours\n 2. The free disk space\n 3. Space consumed by this user\n 4. Exit\n\n SELECTION: \c"

read choice

case $choice in

1) find $HOME -mtime -1 -print ;;

2) df ;;

3) du -s $HOME ;;

4) exit ;;

*) echo "Invalid option"

esac

expr computation and string handling
Expr: Computation and String Handling
  • expr is used for integer computation and string manipulation. It is used with the Bourne shell for incrementing the value of a variable.
  • This command combines two functions in one:
    • Performs arithmetic operations on integers.
    • Manipulates strings.
expr computation and string handling1
Expr: Computation and String Handling
  • The + (add), - (subtract), * (multiply), / (divide), and % (divide and truncate the decimal portion).

$ x=3; y=5; expr $x + $y;

  • expr uses regular expressions to extract a substring, locate the position of a character, and evaluate the length of a string.
  • Korn and bash shells don’t need expr.
  • sleep specifies the number of seconds for which the shell will pause.
while and until looping
While and until: Looping
  • The while loop executes its body as long as the control command returns a true value.
  • It is used in scripts that repeatedly increment the value of a variable or provide multiple chances to a user.
  • You can set up an infinite loop using true as the control command.
  • The until loop complements while.
while and until looping1
While and until: Looping
  • The syntax of the while command is:

while condition is true

do

commands

done

while [! –r invoice.lst]

do

sleep 60

done

while and until looping2
While and until: Looping

until [–r invoice.lst]

do

sleep 60

done

  • for works with each element of a list at a time. The list can be generated by variables, wild cards, positional parameters and command substitution.
for looping with a list
for: Looping with a List
  • The syntax of this construct is as follows:

for variable in list; do

commands

done

for file in chap*; do

cp $file ${file}.bak

echo $file copied to $file.bak

done

for looping with a list1
for: Looping with a List

for file in *.c; do

cc -o ${file}.o $file

done

  • All loops use the keywords do and done.
  • The break statement terminates a loop, while continue starts the next iteration.
ad