The term “shell” encompasses any software program that offers a command-line interface to interact with an operating system. Among various types of shells, “Bash” is a popular one extensively utilized in Unix/Linux systems.
A bash script is a file that ends with the “.sh” extension and contains a series of commands and logic controls. When executed by the bash program, the commands in the script are executed line by line. It’s important to note that the file extension “.sh” is commonly used as a naming convention for bash scripts, but it is not mandatory.
The bash script can be created and edited by using popular text editors such as vi, nano, Notepad, Sublime Text, or Visual Studio Code.
Be careful !!!
When working with a Windows editor or copying code from the internet, it is important to check for the presence of \r
characters (CR) in the script. In the diagram below, the tesh.sh
script does not function correctly due to the presence of these characters, while the test2.sh
script works perfectly despite having the same syntax. The presence of \r
characters, also known as carriage return characters, can cause issues when running scripts on Unix-based systems, as the carriage return may interfere with the interpretation of commands. It is advisable to remove these characters from the script before execution.
Removing \r
characters can be done effortlessly by utilizing the replace function in Windows Note++ text editor, as demonstrated in the diagram below:
Bash scripting is primarily designed for command-line operations and automating system tasks. Compared to languages like Python, Bash scripting relies more on symbols rather than meaningful syntax. In my observation, as scripts grow in size and complexity, involving intricate logic and data processing, Bash scripting can become increasingly intricate and difficult to grasp.
Most commonly used bash commands
- awk: extract specific fields or columns from input data.
- cat: Concatenate and print the contents of a file.
- cd: Change the directory to a different location.
- chmod: Change the permissions of a file or directory.
- clear: Clear the terminal screen.
- cp: Copy a file or directory.
- date: Displays the current date.
- df: Display the amount of disk space available.
- echo: Print text to the terminal.
- grep: Search for a pattern in a file or input streams.
- history: Show a list of previously executed commands.
- hostname: Show the name of the host.
- ifconfig: Show and set network device.
- login: Log into the linux as a new user.
- logout: Log off from linux.
- ls: List the contents of the current directory.
- mkdir: Create a new directory.
- mv: Move or rename a file or directory.
- nano: “sudo nano filename” open the file by text editor “nano”
- passwd: Change password of a user.
- ping: “Ping an IP address” to test the connectivity of a network.
- ps: Display information about running processes.
- pwd: Displays the present working directory.
- reboot: Reboot linux.
- rm: Remove a file or directory.
- sed: text stream editing such as searching, replacing, inserting, and deleting text.
- shutdown: Shutdown your machine.
- sleep: Halt all currently running bash scripts and puts the system to sleep. Argument is number in seconds.
- sudo: Run a command with administrative privileges.
- touch: Create a new empty file.
- wget: “wget hyperlink” to retrieve content from the internet.
- whoami: Display the username currently in use.
Executing the bash script
To make a script file executable, grant the permission to the file, such as “abc.sh” utilizing the following command to assign user execution rights.
chmod u+x abc.sh
Subsequently, script file can be executed using either of the following commands:
bash abc.sh
sh abc.sh
./abc.sh
When executing a script file, it is possible to pass multiple arguments to the script.
bash abc.sh argu1, argu2
Scripts coding summary
First line
#!/bin/bash
Tell the shell to execute it via bash shell. It contains an absolute path to the bash interpreter. The bash shell path can be found by using the command: which bash
Comment
start with a #
Variable
No data types, can store numeric values, characters and string.
$ is required to access an existing variable’s value.
Variable name starts with a letter or an underscore (_), case-sensitive, no space and no special characters allowed
fruit="apple"
drink="$fruit"
cups=5
numberofdrink=$cups
Input
read the user input and store it in a variable username
read username
Reading from a file named input.txt
while read line
do
echo $line
done < input.txt
Output
Print to the terminal
echo “Hello, World!”
To redirect and overwrite all the text displayed on the terminal, generated by commands, to a file called “output.txt”:
echo “Hello, World!” > output.txt
To redirect and append all the text displayed on the terminal, generated by commands, to a file called “output.txt”:
echo “Hello, World!” >> output.txt
Command-line argument
When arguments are provided when script is executed, the variable “$1” in the script represents the first argument passed, “$2” represents the second argument passed, and so on.
echo "Hello, $1!"
To obtain the number of arguments, the following method can be used within the script: [# means the number, $ means arguments]
echo $#
Logic
eq: equal
ne: not equal
gt: greater than
lt: less than
ge: greater or euqal
le:less or equal
&& or -a: AND
|| or -o: OR
!: NOT
if/then/else
number=10
if [ $number -gt 10 ]; then
echo "The number is greater than 10."
elif [ $number -eq 10 ]; then
echo "The number is equal to 10."
else
echo "The number is less than 10."
fi
While loop
num=1
while [[ $num -le 10 ]] ; do
echo "$num"
(( num += 1))
done
For loop
for num in {1..5}
do
echo $num
done
Case statement
food="meat"
case $food in
"fish")
echo "I am eating fish."
;;
"fruit")
echo "I am eating fruit."
;;
"meat")
echo "I am eating meat."
;;
*)
echo "I am not eating now."
;;
esac
The asterisk “*” represents the default case
sed scripts
‘sed
‘ is a powerful command-line tool for stream editing. It is designed to perform various operations on text streams, such as searching, replacing, inserting, and deleting text. Here are Some common examples that demonstrate the usage of sed
scripts are shown below:
i) Search for first occurrence of the word “apple” in file.txt and replace it with “orange”. Without -i option to perform an in-place edit, the modified output will be only displayed on the console and do not modify the original file.
sed -i 's/apple/orange/' file.txt
ii) Replace all words “apple” by the word “orange” using /g option and save the modified content to a new file named “new_file.txt”. The original file “file.txt” will remain unchanged.
sed 's/apple/orange/g' file.txt > new_file.txt
iii) Insert a new line “This is a new line” before line number 2 in file.txt
sed -i '2i This is a new line' file.txt
iv) Search any lines with word “orange” and delete the whole lines
sed -i '/orange/d' file.txt
v) Delete line 3 from “file.txt” and save the modified content (without line 3) to a new file named “new_file.txt”. The original file “file.txt” will remain unchanged.
sed '3d' file.txt > new_file.txt
vi) Find all digits in the file.txt and substitute with the character ‘X’.
Detailed explanation: The s
stands for “substitute”. Within the forward slashes (/
), we specify the pattern to be searched for, which is [0-9]
in this case, matching any digit. Outside the forward slashes, we specify the replacement, which is the letter “X”. The g
at the end of the expression stands for “global” and is used to replace all occurrences of the pattern on each line rather than just the first occurrence.
sed -i 's/[0-9]/X/g' file.txt
vii) Modify the input stream by replacing “apple” with “orange”. This allows you to perform text manipulation on-the-fly without the need for intermediate files.
echo "I am eating an apple." | sed 's/apple/orange/'
Running the command will produce the following output:
I am eating an orange.
Inner (functions in script) and outer script
#!/bin/bash
# Outer script
echo "This is the outer script."
# Inner script
inner_script() {
echo "This is the inner script."
echo "Inner script completed."
}
# Calling the inner script
inner_script
echo "Outer script completed."
script nesting
Content of script1
echo "Hello, $1!"
Content of script2 ( make sure no spaces surrounding the equal sign (=
) )
firstScript="./script1"
name="George"
echo $(bash "$firstScript" "$name")
# ./script2
# Hello, George!