UNIX / Linux Shell Scripting Tutorial

Unix / Linux Bourne / Bash shell scripting book Buy this book as a 75-page PDF document.
Read the sample. All the content, available offline, with examples, no adverts, and convenient to print.

UNIX/Linux Shell CheatSheet : free A4 PDF (52Kb) | PNG (90Kb)

Unix / Linux Shell Scripting Tutorial

8. Test 1. Intro 2. Philosophy 3. A First Script 4. Variables - Part I 5. Wildcards 6. Escape Characters 7. Loops 8. Test 9. Case 10. Variables - Part II 11. Variables - Part III 12. External Programs 13. Functions 14. Hints and Tips 15. Quick Reference 16. Interactive 17. Exercises 10. Variables - Part II

9. Case

The case statement saves going through a whole set of if .. then .. else statements. Its syntax is really quite simple:
talk.sh
#!/bin/sh

echo "Please talk to me ..."
while :
do
  read INPUT_STRING
  case $INPUT_STRING in
	hello)
		echo "Hello yourself!"
		;;
	bye)
		echo "See you again!"
		break
		;;
	*)
		echo "Sorry, I don't understand"
		;;
  esac
done
echo 
echo "That's all folks!"

Okay, so it's not the best conversationalist in the world; it's only an example!

Try running it and check how it works...

$ ./talk.sh
Please talk to me ...
hello
Hello yourself!
what do you think of politics?
Sorry, I don't understand
bye
See you again!

That's all folks!
$
The syntax is quite simple:
The case line itself is always of the same format, and it means that we are testing the value of the variable INPUT_STRING.

The options we understand are then listed and followed by a right bracket, as hello) and bye).
This means that if INPUT_STRING matches hello then that section of code is executed, up to the double semicolon.
If INPUT_STRING matches bye then the goodbye message is printed and the loop exits. Note that if we wanted to exit the script completely then we would use the command exit instead of break.
The third option here, the *), is the default catch-all condition; it is not required, but is often useful for debugging purposes even if we think we know what values the test variable will have.

The whole case statement is ended with esac (case backwards!) then we end the while loop with a done.

That's about as complicated as case conditions get, but they can be a very useful and powerful tool. They are often used to parse the parameters passed to a shell script, amongst other uses.


8. Test 1. Intro 2. Philosophy 3. A First Script 4. Variables - Part I 5. Wildcards 6. Escape Characters 7. Loops 8. Test 9. Case 10. Variables - Part II 11. Variables - Part III 12. External Programs 13. Functions 14. Hints and Tips 15. Quick Reference 16. Interactive 17. Exercises 10. Variables - Part II
Suggested books: Recommended Reading
StumbleUponStumbleUpon | del.icio.usdel.icio.us

Site Links



1. Intro
2. Philosophy
3. A First Script
4. Variables - Part I
5. Wildcards
6. Escape Characters
7. Loops
8. Test
9. Case
10. Variables - Part II
11. Variables - Part III
12. External Programs
13. Functions
14. Hints and Tips
15. Quick Reference
16. Interactive
17. Exercises
18. Forum
19. Recommended Books




My blog has tips about how to use Unix and Linux commands XML



contact