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.;
, Every shell script needs this to tell it what shell to run in. Type #!/bin/bash.
 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="" and choice=0.
 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.
 It should look like: menuList().
, It should look like this: 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."




,,, You should add runLS().
, ls -la $directory where $directory is the one specified by the menu choice.
,, while . Where $choice is the input from the user and 4 is the menu item to tell the program to close, -ne is a conditional operator used in bash shell scripting for number evaluation.
 Type in do.
 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 then
$directory="~/"
runLS
elif then
$directory="/"
runLS
elif then
$directory="/bin"
runLS
elif then
clear
echo "Thank you for using list files"
sleep 3
exit
else
echo "Invalid choice, please try again"
fi
done




,, Type in: chmod u+x FunLoop.sh.

Go to Linux and open a terminal window.
Enter the shell statement on the first line of the file.
Create the variables you will need for this script.
Create your first function.
Type the name and parentheses on the first line after the variables.
Type an opening bracket on another line by itself {.

,
Type the command to clear the screen on a line by itself.
Type the echo statements that make up the menu.
Type a closing bracket on another line by itself }.

,
Create a function to run the ls command.

,
Type the name and parentheses on the first line after the variables.
Type an opening bracket on another line by itself {.

,
Type the command to run the ls.
Type a closing bracket on another line by itself }.

,
Create the loop and if statements to run your functions.
,
Create the while statement with the word while followed by a conditional statement.
Continue the while statement with the do keyword.
Next comes the code you wish to run every time the while loop runs.
Save and exit the file by typing: Esc, :x.

,
Run chmod to give permissions to the file to be executable.
Run the program by typing ./FunLoop.sh.
{"src": "wikihow-all_test", "title": "How to Write Functions and Loops in Bash"}