Updated April 10, 2023
Overview of Bash Scripting
Bash is the abbreviation of Bourne-again shell. UNIX shell runs the program in a command line interpreter so that the computer program has various dialects in the language. The language has many commands in the text which is a mix of different commands. The normal operations in any scripting language are file manipulation, program execution, and text printing. These scripts are mainly used for administrative tasks in the system. Bash has invocation features and the look can be customized in the scripting. Also, they lend a helping hand in installing complex programs. The files are saved as .bashrc.
How Does Bash Scripting Works?
Following are the steps to write and execute a bash script successfully:
- Open any text editor (For ex vi or nano) from the command line. This can simply be done by typing vi or nano and pressing enter.
- Put the following line at the beginning: #!/bin/bash. This is required to declare the interpreter. Here #! it is called shebang or hashbang and the remaining part is the path to the interpreter. You can get the path to your interpreter by executing the command: which bash
- Now put all the commands you want to execute.
- Save the file with a name and .sh extension.
- Make the file executable with the following command: chmod +x <file_name>
- Run the script with the following command: ./<file_name>
In case you need to pass some arguments you can pass them in the following way:
./<file_name> arg1 arg2 arg2
Example
Let’s write and execute a bash script that would read marks obtained by various students in a subject (Say English) and display the average marks, maximum marks (and by which students) and minimum marks (and by which students). It will also list out the students who have failed in the subject.
Given a csv file whose each row consists of a student’s name and marks obtained. Let’s assume that the minimum marks (out of 100) to pass in the subject is 40.
Script:
#!/bin/bash
# Declaring all the variables
SUM=0
COUNT=0
MAX_MARKS=0
MIN_MARKS=100
MAX_MARKS_LIST=()
MIN_MARKS_LIST=()
FAIL_LIST=()
input="marks.csv"
# Set "," as the field limiter using $IFS
# and then read line by line using while and read
while IFS=',' read -r name marks
do
if [ $marks -gt $MAX_MARKS ]; then
MAX_MARKS=$marks
fi
if [ $marks -lt $MIN_MARKS ]; then
MIN_MARKS=$marks
fi
if [ $marks -lt 40 ]; then
FAIL_LIST+=($name )
fi
SUM=$(( SUM + marks))
COUNT=$(( COUNT + 1 ))
done < "$input"
input="marks.csv"
while IFS=',' read -r name marks
do
if [ $marks -eq $MAX_MARKS ]; then
MAX_MARKS_LIST+=($name)
fi
if [ $marks -eq $MIN_MARKS ]; then
MIN_MARKS_LIST+=($name)
fi
done < "$input"
echo "The highest marks obtained is:" $MAX_MARKS
echo
echo "The students who have obtained the highest marks are: " ${MAX_MARKS_LIST[@]}
echo
echo "The lowest marks obtained is:" $MIN_MARKS
echo
echo "The students who have obtained the lowest marks are:" ${MIN_MARKS_LIST[@]}
echo
echo "The students who have failed are:" ${FAIL_LIST[@]}
echo
echo "The average marks obtained is:" $((SUM / COUNT))
We have put the above script inside a file called my_script.sh.
We have our input inside marks.csv file.
Let’s check its content:
Now let’s try to run the script:
We are getting this error because we haven’t made it executable yet. Let’s set proper permissions:
Let’s try to run it again:
Bingo! We got the desired output.
Advantages of Bash Scripting
- It can automate the frequently performed operations.
- Can run a sequence of commands in a go.
- Quite easy to use.
- It is portable.
Conclusion
In this article, we have seen how we can execute a set of shell commands in one go. We can use Crontab in Linux/UNIX systems to schedule the run of a bash script at the desired time which would reduce manual efforts to large extents.
Recommended Articles
We hope that this EDUCBA information on “What is Bash Scripting?” was beneficial to you. You can view EDUCBA’s recommended articles for more information.