How to Write Functions and Loops in Bash

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

21 Steps 2 min read Advanced

Step-by-Step Guide

  1. Step 1: Go 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.
  2. Step 2: Enter 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. , 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. ,
  3. Step 3: Create the variables you will need for this script.

  4. Step 4: Create your first function.

  5. Step 5: Type the name and parentheses on the first line after the variables.

  6. Step 6: Type an opening bracket on another line by itself {.

  7. Step 7: Type the command to clear the screen on a line by itself.

  8. Step 8: Type the echo statements that make up the menu.

  9. Step 9: Type a closing bracket on another line by itself }.

  10. Step 10: Create a function to run the ls command.

  11. Step 11: Type the name and parentheses on the first line after the variables.

  12. Step 12: Type an opening bracket on another line by itself {.

  13. Step 13: Type the command to run the ls.

  14. Step 14: Type a closing bracket on another line by itself }.

  15. Step 15: Create the loop and if statements to run your functions.

  16. Step 16: Create the while statement with the word while followed by a conditional statement.

  17. Step 17: Continue the while statement with the do keyword.

  18. Step 18: Next comes the code you wish to run every time the while loop runs.

  19. Step 19: Save and exit the file by typing: Esc

  20. Step 20: Run chmod to give permissions to the file to be executable.

  21. Step 21: Run the program by typing ./FunLoop.sh.

Detailed Guide

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

About the Author

H

Heather Stokes

Specializes in breaking down complex lifestyle topics into simple steps.

58 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: