How to Bash Shell Script Functions and Loops
One Methods:Example
Bash shell scripting is a CLI (Command Line Interface) language that is powerful and versatile. The easiest way to reduce the amount of code you have to write in any program is by coming up with ways to reuse code. Loops and functions are two ways to accomplish this. As in any language, the more of the syntax you know (or where to find the syntax), the better you will be at writing the code. The code that is provided in this article is a runnable program that will execute on UNIX, Linux or the Mac OS X or higher. This article will start in Linux at the command prompt.
EditSteps
-
1Go to Linux and open a terminal window. Type the following command at the command prompt:
vi FunLoop.sh
. This will create the file you are going to use as a script file and open it for editing.Ad -
2Enter the shell statement on the first line of the file. Every shell script needs this to tell it what shell to run in. Type
#!/bin/bash
. -
3Create the variables you will need for this script. You will need one to capture the choice entered by the user and one to set the directory based on the user's choice. Type
directory=""
andchoice=0
. -
4Create your first function. It is a straightforward code and it has a name that is case sensitive. Name it
menuList
. Everything between the opening and closing brackets is any code you wish it to be. -
5Type the name and parentheses on the first line after the variables. It should look like:
menuList ()
. -
6Type an opening bracket on another line by itself
{
. -
7Type the command to clear the screen on a line by itself. It should look like this:
clear
. -
8Type the echo statements that make up the menu.
-
echo "1. Run ls on my home directory."
. -
echo "2. Run ls on the root directory."
. -
echo "3. Run ls on the bin directory."
. -
echo "4. Exit program"
.
-
-
9Type a closing bracket on another line by itself
}
. -
10Create a function to run the
ls
command. -
11Type the name and parentheses on the first line after the variables. You should add
runLS ()
. -
12Type a opening bracket on another line by itself
{
. -
13Type the command to run the ls.
ls -la $directory
where $directory is the one specified by the menu choice. -
14Type a closing bracket on another line by itself
}
.. -
15Create the loop and if statements to run your functions.
-
16Create the while statement with the word while followed by a conditional statement.
while [ $choice -ne 4]
. Where$choice
is the input from the user and4
is the menu item to tell the program to close,-ne
is a conditional operator used in bash shell scripting for number evaluation. -
17Continue the while statement with the do keyword. Type in
do
. -
18Next comes the code you wish to run every time the while loop runs. You have four menu items so you will need four
if
statements and a catch all for values that don't meet the menu items' requirements. Write the if statement as follows:if [ $choice -eq 1 ]
then
$directory="~/"
runLS
elif [ $choice -eq 2 ]
then
$directory="/"
runLS
elif [ $choice -eq 3 ]
then
$directory="/bin"
runLS
elif [ $choice -eq 4 ]
then
clear
echo "Thank you for using list files"
sleep 3
exit
else
echo "Invalid choice, please try again"
fi
-
done
.
-
19Save and exit the file by typing: Esc,
:x
. -
20Run chmod to give permissions to the file to be executable. Type in:
chmod u+x FunLoop.sh
. -
21This is a simple program to show how to create a function and some loops to reduce repeating code.
- run the program by typing
./FunLoop.sh
- run the program by typing
EditExample
Sample code that you can use:
!/bin/bash. directory="" choice=0 menuList () { clear echo "1. Run ls on my home directory." echo "2. Run ls on the root directory." echo "3. Run ls on the bin directory." echo "4. Exit program" } runLS () { ls -la $directory sleep 2 } while [ $choice -ne 4 ] do menuList read -p "Enter your choice: " choice if [ $choice -eq 1 ] then $directory="~/" runLS elif [ $choice -eq 2 ] then $directory="/" runLS elif [ $choice -eq 3 ] then $directory="/bin" runLS elif [ $choice -eq 4 ] then clear echo "Thank you for using list files" sleep 3 exit else echo "Invalid choice, please try again" fi done
We could really use your help!
e-commerce?

dehumidifiers?

business operations?

trailers?

EditVideo
EditWarnings
- Incorrect scripts can have negative effects on your system. It is important to be careful when manipulating files on a computer.
EditThings You'll Need
- Computer with Linux, UNIX or OS X on it
EditSources and Citations
- List of programming languages by type: http://en.wikipedia.org/wiki/List_of_programming_languages_by_type
- BASH Programming - Introduction HOW-TO: http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html
- Advanced Bash-Scripting Guide: http://tldp.org/LDP/abs/html/
- Professor Norman Han, Algonquin College, Ottawa, ON
Article Info
Categories: Programming
Recent edits by: Chris R, Flickety, Hailey Girges
Thanks to all authors for creating a page that has been read 359 times.