150 likes | 304 Views
If..then..elif. Template: if test-command; then commands elif test-command; then commands … else commands fi. Day 14. Looping. Example. if [ “$name” = “Jeff” ]; then echo “Hey Jeff” elif [ “$name” = “Bob” ]; then echo “Hey Bob”
E N D
If..then..elif • Template: • if test-command; then • commands • elif test-command; then • commands • … • else • commands • fi
Day 14 Looping
Example • if [ “$name” = “Jeff” ]; then • echo “Hey Jeff” • elif [ “$name” = “Bob” ]; then • echo “Hey Bob” • elif [ “$name” = “Bill” ]; then • echo “Hey Bill” • else • echo “I dont know you” • fi
And • Test with -a • Example: • if[ “$firstname” = “Jeff” –a / • “$lastname” = “Wilson” ]; then • echo “You are my hero!” • fi
Or • Test with –o • Example: • if[ “$name” = “Jeff” –o / • “$name” = “Jeffrey” ]; then • echo “You are my hero!” • fi
Loop! • Template: • forloop-indexin argument-list • do • echo “do stuff” • done
Example • for fruit in apple orange pear • do • echo “$fruit” • done
Whoson Script • Find out which of your friends are on. • Automatically write them a greeting message.
While Loop • Template: • while test-command • do • commands • done
Example • number=0 • while [ “$number” –lt 10 ] • do • echo –n “$number” • let number=“$number”+1 • done
Until • Another type of loop • This one ALWAYS runs at least once
Until Template • until test-command • do • commands • done
Until Example • name=duh • echo “Guess the name!” • until [ “$name” = “bob” ] • do • echo -n “guess: “ • read name • done • echo “You got it!”
Break • Allows you to jump out of a loop (even if the loop isn’t finished)
Break Example • ans=“y” • while [ “bob” = “bob” ] • do • echo “Want to stop looping? (y or n)” • read ans • if [ $ans = “y” ]; then • break • fi • done