1 / 15

If..then..elif

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”

ronna
Download Presentation

If..then..elif

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. If..then..elif • Template: • if test-command; then • commands • elif test-command; then • commands • … • else • commands • fi

  2. Day 14 Looping

  3. 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

  4. And • Test with -a • Example: • if[ “$firstname” = “Jeff” –a / • “$lastname” = “Wilson” ]; then • echo “You are my hero!” • fi

  5. Or • Test with –o • Example: • if[ “$name” = “Jeff” –o / • “$name” = “Jeffrey” ]; then • echo “You are my hero!” • fi

  6. Loop! • Template: • forloop-indexin argument-list • do • echo “do stuff” • done

  7. Example • for fruit in apple orange pear • do • echo “$fruit” • done

  8. Whoson Script • Find out which of your friends are on. • Automatically write them a greeting message.

  9. While Loop • Template: • while test-command • do • commands • done

  10. Example • number=0 • while [ “$number” –lt 10 ] • do • echo –n “$number” • let number=“$number”+1 • done

  11. Until • Another type of loop • This one ALWAYS runs at least once

  12. Until Template • until test-command • do • commands • done

  13. Until Example • name=duh • echo “Guess the name!” • until [ “$name” = “bob” ] • do • echo -n “guess: “ • read name • done • echo “You got it!”

  14. Break • Allows you to jump out of a loop (even if the loop isn’t finished)

  15. 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

More Related