Various scripting examples. Lists, Loops, functions, statements and more!
Used to Specify which interpreter will run the script. This will be the very 1st line. This line will start with a #! and be followed by the path to the bash interpreter. Doing this will ensure the correct interpreter is used to execute the script. Without this line, the script will be teated as just a text file. However, the Bash interpreter is not always installed in the same location. In this case the shebang can also point to the Bash environment variable, which will then point to the interpreter. Examples shown below.
If the script is being run locally on the machine that created it, the following command can be use to find the path to the Bash interpreter. After finding the path, add it to the shebang/hashbang at the top of the script.
Screenshot
Find path to interpreter
Command | Purpose |
---|---|
#! | shebang / hashbang |
/usr/bin/bash | path to interpreter |
Use the following example to specify the intended interpreter by the environment variable. This can be useful if the path to the interpreter is unknown.
Screenshot
Using environment variable
Command | Purpose |
---|---|
#! | shebang / hashbang |
/usr/bin/env bash | use environment variable |
Collect and store input from the user.
The following example uses a user input to get information from the user. Ater this information has been collected, it will be stored in a variable called any_value and shown to the user.
#!/usr/bin/bash # user input echo -n "Enter any value: " read any_value # print out entered value echo "value entered: $any_value" # exit this script exit
Command | Purpose |
---|---|
echo -n |
print the following to the screen and keep prompt on same line |
read any_value | store user input in any_value |
echo "value entered: $any_value" | print value of any_value to the screen |
exit | exit script |
Screenshot
User input example
The following is an example scipt using a if/else statement. The user will first be asked to select a mode. If read is selected the message "mode was set to read" will be shown. If any other value is entered, the message "mode was not set to read" will be shown.
#!/usr/bin/bash # user input echo -n "choose mode (read/write): " read mode # if / else if [ $mode == read ]; then echo "mode was set to read" else echo "mode was not set to read" fi # exit this script exit
Screenshot
if/else statement example
The following is an example scipt using a else/if statement. The user will first be asked to select a mode. If read is selected the message "mode was set to read" will be shown. If write is selected the message "mode was set to write" will be shown. If any other value is entered, the message "invalid value" will be shown.
#!/usr/bin/bash # user input echo -n "choose mode (read/write): " read mode # else / if if [ $mode == read ]; then echo "mode was set to read" elif [ $mode == write ]; then echo "mode was set to write" else echo "invalid value" fi # exit this script exit
Screenshot
else/if statement example
The following is an example scipt using a if/and statement. The user will first be asked to provide a username and password. If the username is root and the password is password, they will be presented with a welcome message. If the username entered is root and the password is not password, they will get a message saying the password is invalid. Finally... if the user is not root, they will be denied access and threatened.
#!/usr/bin/bash echo "" echo -n "Username: " read username echo -n "Password: " read password echo "" # if / and if [ $username == "root" ] && [ $password == "password" ]; then echo "Welcome $username" elif [ $username == "root" ] && [ $password != "password" ]; then echo "password entered is not valid" else echo "only root has access" echo "this indicent will be reported" fi # exit script exit
Screenshot
if/and statement example
The following is an example scipt using a if/or statement. The user will first be asked to provide a username and password. If the username is root or tux and the password is password, they will be presented with a welcome message. If the username entered is root or tux and the password is not password, they will get a message saying the password is invalid. Finally... if the user is not root or tux, they will be denied access and threatened.
#!/usr/bin/bash echo "" echo -n "Username: " read username echo -n "Password: " read password echo "" # if / or if [ $username == 'root' ] || [ $username == 'tux' ] && [ $password == "password" ]; then echo "Welcome $username" elif [ $username == 'root' ] || [ $username == 'tux' ] && [ $password != "password" ]; then echo "password entered is not valid" else echo "only root and tux have access" echo "this incident will be reported" fi # exit script exit
Screenshot
if/and statement example
Collect and store input from the user.
The following code snippets are examples of for loops
Specified Range (iteration not shown)
Complete a task for a predetermined amount of iterations. In the example below... the sentence "just some text" will be printed out to the screen 10 times.
#!/usr/bin/bash for i in $(seq 10); do echo "just some text" sleep 1 done # exit script exit
Specified Range (iterations shown)
Similar to the example above, the following code snippet will complete a task for a predetermined amount of iterations. The sentence "just some text" will be printed out to the screen 10 times. Showing each iteration can be done by adding the $i to the end of the echo command. This would be the variable created in the for loop representing each iteration. In this context, the "i" can be considered as an item or iteration. Any other character or name can also be used, as the "i" is only representative
#!/usr/bin/bash for i in $(seq 10); do echo "just some text:" $i sleep 1 done # exit script exit
Specified Iterations (iterations shown)
In this example, we can define where the iteration will start and where it will end. Each iteration will be shown, starting at 1 and ending at 10.
#!/usr/bin/bash for i in {1..10}; do echo "just some text:" $i sleep 1 done # exit script exit
Specified Iterations (iterations shown 5-10)
Similar to the example above, we can specify where the iterations will start and when they will end. In this example the 1st iteration starts at 5 and ends at 10. Resulting in 6 iterations.
#!/usr/bin/bash for i in {5..10}; do echo "just some text:" $i sleep 1 done # exit script exit
Specified Iterations (iterations shown 1, 4, 7 and 10)
In addition to specifying where the iterations will start and end, we can also specify the size of each step. In the example below, the iterations will start at 1, end at 10 and will have a step count of 3. Resulting in 4 iterations (1,4,7 and 10)
#!/usr/bin/bash for i in {1..10..3}; do echo "just some text:" $i sleep 1 done # exit script exit
Arrays (show contents)
For loops can also be used to iterated through an array. The items in said array can be anything from strings to functions and even other arrays. In the example below, a list of employees will be printed out to the screen from an array of employees. ${Employees[@] expands the array to include all elements.
#!/usr/bin/bash Employees=("John" "Alice" "Tux" "May" "Jessie" "Jade" "Louis" "Sara" "Donald") echo "" echo "Employees" echo "---------" for i in ${Employees[@]}; do echo "$i" sleep 1 done # exit script exit
The following is an example script using a while loop. These are used to execute a block of code repeatedly while a specified condition remains true. It evaluates the condition before each iteration based on changing conditions within the program.
While Loop (Basic Example)
In the example below, the while loop will execute its commands while the "valid" condition remains true or when the iteration count reaches 5. After which, the script will then exit. Each iteration will display it's value on the screen until it reaches 5 and sleep for 1 second. Resulting in the numbers 1 through 5 being printed on the screen.
#!/usr/bin/bash count=1 while true; do echo currnet count: $count if [ $count -eq 5 ]; then break fi ((count++)) sleep 1 done # exit script exit
Screenshot
basic while loop
While Loop (with user input)
In the example below, the while loop will execute its commands while the value of "guess" is not equal to 5 (the magic number) At each iteration, the user will be prompted to enter a number. When the magic number has been guessed correctly the number will be shown along with some positive feedback. After which, the program will exit.
#!/usr/bin/bash magic_number=5 guess=0 echo "" echo "Guess the magic number (1-10)" echo "" while [ $guess != $magic_number ]; do echo -n "Enter Number => " read guess done echo "" echo "You guessed correctly!" echo "The magic number is $magic_number" # exit script exit
Screenshot
while loop with user input