What is a Bash script?
A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ourselves on the command line .
These scripts can be used to automate the repeatable tasks by generating the script files which contains commands in sequence of execution.
Bash is a name of the unix shell, which was also distributed as the shell for the GNU operating system and as the default shell on most Linux distros.
echo $SHELL
/usr/bin/bash
This command checks the executing shell and gives the path for the same.
Create Bash file to write Bash script
nano bash.sh
ctrl + X to exit
Y to confirm
Press enter
It is convention to give files that are Bash scripts an extension of .sh.
A variable named STRING is initialised to"TUTORIAL"
and in the second line the string is printed and variable is accessed by using $
bash bash.sh
Entering this command will execute the bash file and the string
"Welcome to the tutorial is printed"
Passing variables as parameters
#! /usr/bin/bash #this line referred to as shebang
echo "Linux Distros are $1 , $2 and #3
bash bash.sh Ubuntu Fedora Kali
The bash file will take parameters entered in the command to refer in the string
Run commands using script file
#! /usr/bin/bash
echo $(whoami)
Executing the bash file gives you the username
Sending the user input to the script file
#! /usr/bin/bash
echo "what is your name"
read name
echo "$name welcome to the server"
Conditions in scripts
#! /usr/bin/bash
echo "what is your name"
read name
if [ $name]; then
echo "$name welcome to the server"
else
echo "Doesn't sound anything to me"
fi # fi is used to end the loop
The input from user is used to check , if user entered the name then if condition is executed otherwise, else condition is executed
Aliasing of commands
Aliasing happens whenever one variable's value is assigned to another variable
In order to save the time and reduce the effort of writing the complex command everytime the programmar need it.
This method helps you assign such complex commandsto simple variables and can be executed with the help of these variables
ifconfig | grep broadcast | awk 'print{$2}'
ifconfig command include setting the IP address and netmask of a network interface and disabling or enabling an interface.
| This straight line is a pipeline used to attach two commands and execute them one after other in a sequence
grep command searches for lines that contain strings that match a pattern
grep broadcast helps to filter the string broadcast in the executed part of the ifconfig command
Basic function of awk is to search files for units of text
awk 'print{$2}' prints the second word from the line obtained and obtains the ipaddress
alias whatismyipaddress = "echo $(ifconfig | grep broadcast | awk 'print{$2}')"
Now, ipaddress can be directly accessed from the whereismyipaddress variable directly
Aliasing hides the complexity behind the simple variables
Conclusion
Explore more about the bash scripting as it will help you to automate tasks,
You can refer to the bash script cheatsheet here -https://devhints.io/bash
and subscribe to newsletter for more such helpful Blogs