MLUG Bash Scripting Workshop 25/04/08 | ||
---|---|---|
Prev | Index | Next |
I couldn't let the opportunity pass without telling you about the use of functions in Bash scripts.
I use functions to organise my scripts into sections which makes it easier to:
Look at functions in a script as a bunch of smaller scripts all under one shebang. Each function is given a name and after all functions are listed the function names as well as other commands are listed in the order they are to be run. The functions can be listed in any order but the main part or the script must come after the functions and flow top down just as in a script without functions.
#!/bin/bash # basic layout of a script with functions do_this() { run some commands play with variables } do_that() { run some commands play with variables } do_the_other_thing() { run some commands play with variables } # with all the functions listed the script's # main section would begin which may consist # of Bash builtins, and other comands as well # as the functions listed above echo "Hello World" do_that do_this do_the_other_thing # end of script
When the script is going to be long or incorporate more than, say, four involved steps, I will write some if not all of them out as scripts. This allows me to test them before they go into the final script and once in, there's a bit more order to the script.
If you had stopped to look, you will have seen that example07 is made up of 21 scripts, one runs the other 20. Example07 could have been put together as a single script but it would have been much more difficult for me to keep track of it as I wrote it. It would have been better convert the 20 scripts to functions and I thought about doing that. However in the end I decided the easiest thing to do was to run them all with while.
I have, however, changed things around a bit in example06 so that you can see how that script would look with everything written into functions.
#!/bin/bash # functions07, was example07 get_response() { clear echo -e "\n\t\t\tWhat does `whoami` want to do?\n" echo -e "\t\t\tMake a selection from the following by" echo -e "\t\t\ttyping in the corresponding number then" echo -e "\t\t\thitting enter. \c" sleep 1 echo -e "\n\n\t\t\t1 Do nothing" echo -e "\t\t\t2 Get the time and date" echo -e "\t\t\t3 Do something silly" echo -e "\t\t\t\t\c" read response } response_one() # function if response is 1 { echo -e "\n\n\t\t\tLooks like we're feeling lazy today :^)\n\n\n" sleep 5 } response_two() { echo -e "\n\n\t\t\t`date`" sleep 4 clear } response_three() { clear silly=0 while [ $silly -lt 10 ]; do echo -e "\n\n\n\t\t\c" sleep 1 echo -e "`whoami` is a darn nice person, I \c" echo -e "don't care what anybody says! \c" sleep 1 clear let silly=$silly+1 done sleep 2 } bad_response() { clear echo -e "\n\n\t\t\tYou did not respond correctly!!" sleep 2 echo -e "\n\t\tYou are lucky I didn't put you in an infinit loop!" sleep 2 echo -e "\n\t\tYou will now have to wait 1 minute before you get" echo -e "\t\tget your command prompt back \c" sleep 10 echo -e "\n\n\n\n" } # Functions go before the main part of the script # Bash will run commands in functions when the function is # called by name in the script # The main part of this function follows: get_response case $response in 1) response_one ;; 2) response_two ;; 3) response_three ;; *) bad_response ;; esac #end of script