170 likes | 375 Views
if then flowchart. if test command. false. true. then commands. fi. Simple if statement. if [ $# –gt 9 ] then echo more than 9 arguments were entered echo “Use the shift command to see args 10-$#” fi. if then else flowchart. if test command. true. false. then commands.
E N D
if then flowchart if test command false true then commands fi Loops and Decisions Flowcharts
Simple if statement if [ $# –gt 9 ] then echo more than 9 arguments were entered echo “Use the shift command to see args 10-$#” fi Loops and Decisions Flowcharts
if then else flowchart if test command true false then commands else commands fi Loops and Decisions Flowcharts
The full if statement • echo “Please enter filename to be backed up” • read fname • if [ -f $fname ] • then cp $fname ${fname}.bak • else echo filename $fname DOES NOT exist • fi Loops and Decisions Flowcharts
The elif flowchart if test-command true false elif test-command false true then commands then commands else commands fi Loops and Decisions Flowcharts
The if - elif statement • day=`date +"%A" • if [ $day = Monday ] ; then echo "Today is Monday" • elif [ $day = Tuesday ] ; then echo "Today is Tuesday" • elif [$day = Wednesday ] ; then echo "Today is Wednesday " • elif [ $day = Thursday ] ; then echo "Today is Thursday" • elif [ $day = Friday ] ; then echo "Today is Friday " • elif [ $day = Saturday ] ; then echo "Today is Saturday " • else echo "Today is Sunday” • fi Loops and Decisions Flowcharts
The case structure flowchart case test-string = pattern-1? true commands-1 false test-string = pattern-2? true commands-2 false test-string = pattern-3? true commands-3 false esac Loops and Decisions Flowcharts
The case structure 1 day=`date +”%A” case $day in M*) echo "Today is Monday“ ;; Tu*) echo "Today is Tuesday" ;; W*) echo "Today is Wednesday " ;; Th*) echo "Today is Thursday" ;; F*) echo "Today is Friday " ;; Sa*) echo "Today is Saturday " ;; Su*) echo "Today is Sunday” ;; esac Loops and Decisions Flowcharts
The case structure 2 cat <<++ Main Menu 1. Display Current Directory 2. Print Today's Date x. Exit Main Manu Please enter your selection ++ (Note: script continued on Next slide) Loops and Decisions Flowcharts
The case structure 2 - Continued • (Note: script continued from Previous slide) • read selection • case $selection in • 1) echo "Your Present Working Directory is: " • pwd ;; • 2) echo "Today's date and time is: " • date ;; • [xX]) leave=yes ;; • *) echo "Invalid choice, please try again" ;; • esac Loops and Decisions Flowcharts
The while loop flowchart while test-command done false true do commands Loops and Decisions Flowcharts
The until loop flowchart until test-command done true false do commands Loops and Decisions Flowcharts
leave=no while [ $leave = no ] do cat <<++ Main Menu 1. The break and continue commands 2. Print Today's Date x. Exit Main Manu Please enter your selection ++ read selection Note: Script continues next slide The while structure Loops and Decisions Flowcharts
Note: Script continues next slide case $selection in [ $USER = mohammed ] && continue Note: && means if command1 executes then do command2 [ $USER = mohammed -a $HOSTNAME = phobos ] && break echo This message NOT printed for user mohammed ;; 2) echo "Today's date and time is: " date ;; [xX]) leave=yes ;; *) echo "Invalid choice, please try again” ;; esac done echo Loop is done The while structure Loops and Decisions Flowcharts
for in - flowchart Assign next argument in argument-list to loop-index do commands is there another argument in the argument-list? yes no done Loops and Decisions Flowcharts
The for loop • echo “Enter file names to be copied: \c”; read fnames • for file in $fnames • do • cp $file ../${file}.bak • if [ $? –ne 0 ] • then echo “$file does not exist, Continue copying? Y/N” • read stop • if [ $stop != Y ] • then break • else echo copied file $file • fi • fi • done Loops and Decisions Flowcharts