Bash Shell Scripting
Bash Shell Scripting
[username@host ~]$
You can enter any command after the $ sign and see the output
on the terminal.
Generally, commands follow this syntax:
which bash
#!/bin/bash
echo "Today is " `date`
echo -e "\n you path has the following files and folders: "
ls $the_path
Script to print contents of a user supplied directory
Let's take a deeper look at the script line by line. I am displaying
the same script again, but this time with line numbers.
1 #!/bin/bash
2 echo "Today is " `date`
3
4 echo -e "\nenter the path to directory"
5 read the_path
6
7 echo -e "\n you path has the following files and folders: "
8 ls $the_path
• Line #1: The shebang (#!/bin/bash) points toward the bash
shell path.
• Line #2: The echo command is displaying the current date
and time on the terminal. Note that the date is in backticks.
• Line #4: We want the user to enter a valid path.
• Line #5: The read command reads the input and stores it in
the variable the_path.
• line #8: The ls command takes the variable with the stored
path and displays the current files and folders.
Executing the bash script
To make the script executable, assign execution rights to your
user using this command:
• chmod modifies the ownership of a file for the current user :u.
• +x adds the execution rights to the current user. This means
that the user who is the owner can now run the script.
• run_all.sh is the file we wish to run.
You can run the script using any of the mentioned methods:
• sh run_all.sh
• bash run_all.sh
• ./run_all.sh
Let's see it running in action 🚀
Bash Scripting Basics
Comments in bash scripting
Comments start with a # in bash scripting. This means that any
line that begins with a # is a comment and will be ignored by the
interpreter.
Comments are very helpful in documenting the code, and it is a
good practice to add them to help others understand the code.
name
count
_var
myVar
MY_VAR
And here are some examples of invalid variable names:
echo -e "\nyour path has the following files and folders: "
ls $the_path
2. Reading from a file
This code reads each line from a file named input.txt and prints
it to the terminal. We'll study while loops later in this article.
while read line
do
echo $line
done < input.txt
3. Command line arguments
2. Writing to a file:
For loop
The for loop, just like the while loop, allows you to execute
statements a specific number of times. Each loop differs in its
syntax and usage.
In the example below, the loop will iterate 5 times.
#!/bin/bash
for i in {1..5}
do
echo $i
done
For loop that iterates 5 times.
Case statements
In Bash, case statements are used to compare a given value
against a list of patterns and execute a block of code based on
the first pattern that matches. The syntax for a case statement
in Bash is as follows:
case expression in
pattern1)
# code to execute if expression matches pattern1
;;
pattern2)
# code to execute if expression matches pattern2
;;
pattern3)
# code to execute if expression matches pattern3
;;
*)
# code to execute if none of the above patterns match expression
;;
esac
Case statements syntax
Here, "expression" is the value that we want to compare, and
"pattern1", "pattern2", "pattern3", and so on are the patterns
that we want to compare it against.
fruit="apple"
case $fruit in
"apple")
echo "This is a red fruit."
;;
"banana")
echo "This is a yellow fruit."
;;
"orange")
echo "This is an orange fruit."
;;
*)
echo "Unknown fruit."
;;
esac
Example of case statement
In this example, since the value of "fruit" is "apple", the first
pattern matches, and the block of code that echoes "This is a red
fruit." is executed. If the value of "fruit" were instead "banana",
the second pattern would match and the block of code that
echoes "This is a yellow fruit." would execute, and so on. If the
value of "fruit" does not match any of the specified patterns, the
default case is executed, which echoes "Unknown fruit."
*/5 * * * */5 * * * *
* Run a script every 5 minutes /path/to/script.sh
Using crontab
The crontab utility is used to add and edit the cron jobs.
crontab -l lists the already scheduled scripts for a particular
user.
You can add and edit the cron through crontab -e.
You can read more about corn jobs in my other article here.
How to Debug and Troubleshoot Bash Scripts
Debugging and troubleshooting are essential skills for any Bash
scripter. While Bash scripts can be incredibly powerful, they can
also be prone to errors and unexpected behavior. In this section,
we will discuss some tips and techniques for debugging and
troubleshooting Bash scripts.
if [ $? -ne 0 ]; then
echo "Error occurred."
fi
Use echo statements
Another useful technique for debugging Bash scripts is to
insert echo statements throughout your code. This can help you
identify where errors are occurring and what values are being
passed to variables.
#!/bin/bash