What is Bash scripting and how does it work?
Bash, short for Bourne-Again Shell, is a Unix shell and a command language interpreter. It interprets a shell scripting language and executes the commands.
Bash scripting can be used to automate linux tasks ( automate means instead of executing individual commands to perform a specific task , simply run the bash file which can automate the same process ).
How to create a bash file?
- to create a bash file , type the command given below ->
nano filename.sh
- bash script to perform auto update of installed linux packages.
#! /bin/bash
echo "Do you want to upgrade" //asking user choice
echo "1. yes"
echo "2. no"
read n
if(($n==1))
then
sudo apt-get upgrade -y // command to update
elif(($n==2))
then
exit
else
echo "Wrong choice entered"
fi
How to run a bash file ?
- In order to run a bash file , firstly add executable permissions to bash file. To make a bash file executable follow the command given below
chmod +x filename.sh
- After adding executable permissions , run the bash file follow the below command
./filename.sh
Bash Script to install and configure git
- Bash Script to automate the process of installation of git and configure it.
#!/bin/bash
if ! [ -x "$(command -v git)" ];then
echo "Git not found in your system"
echo "--------------------"
echo "--------------------"
echo '###Installing Git..'
sudo apt-get install git -y
echo "---------------------"
echo '###Congigure Git..'
echo "Enter the Username for Git:";
read GITUSER;
git config --global user.name "${GITUSER}"
echo "Enter the Email for Git:";
read GITEMAIL;
git config --global user.email "${GITEMAIL}"
echo "-------------------------------------"
echo "-------------------------------------"
echo 'Git has been configured!'
git config --list
else
echo "Git is already installed in your system"
fi
ย