Edit Article

  • 359 views
  • 7 Editors

  • Updated

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.

Ad

EditSteps

  1. 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.
    Ad
  2. 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.
  3. 3
    Create 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="" and choice=0.
  4. 4
    Create 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.
  5. 5
    Type the name and parentheses on the first line after the variables. It should look like: menuList () .
  6. 6
    Type an opening bracket on another line by itself {.
  7. 7
    Type the command to clear the screen on a line by itself. It should look like this: clear.
  8. 8
    Type 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".
  9. 9
    Type a closing bracket on another line by itself }.
  10. 10
    Create a function to run the ls command.
  11. 11
    Type the name and parentheses on the first line after the variables. You should add runLS ().
  12. 12
    Type a opening bracket on another line by itself {.
  13. 13
    Type the command to run the ls. ls -la $directory where $directory is the one specified by the menu choice.
  14. 14
    Type a closing bracket on another line by itself }..
  15. 15
    Create the loop and if statements to run your functions.
  16. 16
    Create the while statement with the word while followed by a conditional statement. while [ $choice -ne 4]. 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.
  17. 17
    Continue the while statement with the do keyword. Type in do.
  18. 18
    Next 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.
  19. 19
    Save and exit the file by typing: Esc, :x.
  20. 20
    Run chmod to give permissions to the file to be executable. Type in: chmod u+x FunLoop.sh.
  21. 21
    This 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

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!

Can you tell us about
e-commerce?
Yes
No
e-commerce
how to start an online business
Can you tell us about
dehumidifiers?
Yes
No
dehumidifiers
different ways to use a dehumidifier
Can you tell us about
business operations?
Yes
No
business operations
how to retain customers
Can you tell us about
trailers?
Yes
No
trailers
how to trailer a motorcycle
Thanks for helping! Please tell us everything you know about
...
Tell us everything you know here. Remember, more detail is better.
Tips
Provide Details.
Please be as detailed as possible in your explanation. Don't worry about formatting! We'll take care of it. For example:
Don't say: Eat more fats.
Do say: Add fats with some nutritional value to the foods you already eat. Try olive oil, butter, avocado, and mayonnaise.

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

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.

Did this article help you?
Yes No

Become
an Author!

Write an Article