Shell Scripting in Linux for Automation
In this article, we will learn about shell scripting in Linux and we will automate a few stuff with the help of shell scripting or we can it bash scripting as well.
So we will write a script to automate the below points.
Take input from the user and print the data.
Automate File and Folder creation and add text to the file.
Automate Conditional Scripting.
Backup Using Script, Manual and Automation.
1-Take input from the user and print the data.
Here we will write a script that will simply print the value at run time and by passing arguments as well.
# First I have created a directory called Scripting and I am in to this directory and we will create all the scripts in this directory only.
so lets create our first file as shown below.
ubuntu@ip-172-31-82-230:~/Scripting$ vim user_input.sh
#!/bin/bash
# This is the starting point for any scripting language which tells program to run this script.
echo "Welcome to Scripting Training"
echo "Please Enter Your Name"
#(Here we have used variable name , and whatever user will input whhen promt, same will be stored in name variable)
read name
echo "Welcome $name in DevOps Training"
echo "Enter Your Rollno"
read rollno
echo "Your Roll no is $rollno"
Now type :wq and hit enter to save and close.
# This is executable file hence we have to provide executable permission with chmod command
ubuntu@ip-172-31-82-230:~/Scripting$ chmod 700 user_input.s
ubuntu@ip-172-31-82-230:~/Scripting$ ./user_input.sh --- to run the script
Welcome to Scripting Training
Please Enter Your Name
Mohan
Welcome Mohan in DevOps Training
Enter Your Rollno
12
Your Roll no is 12
ubuntu@ip-172-31-82-230:~/Scripting$
Now we will see how we can pass the value in arguments.
#!/bin/bash
echo "Welcome to Scripting Training $1" -- Here $1 means it will store the first argument as first value.
Lets run the script.
ubuntu@ip-172-31-82-230:~/Scripting$ ./user_input.sh Mohan
Welcome to Scripting Training Mohan
ubuntu@ip-172-31-82-230:~/Scripting$
2-Automate File and Folder creation and add text to the file.
Here we will create a file and folder and text to it automatically.
ubuntu@ip-172-31-82-230:~/Scripting$ vim create_file_and_folder.sh
#!/bin/bash
mkdir DevOps
cd /home/ubuntu/Scripting/DevOps
touch test.txt
echo "This is demo file for file and folder creation automatically" > test.txt
# what this script will do, this will ceate a directory called DevOps and then it will get into this directory, after that it will create a blank file test.txt and in the final line, it will add the data to the test.txt file.
Lets run and see the results.
ubuntu@ip-172-31-82-230:~/Scripting$ chmod 700 create_file_and_folder.sh
ubuntu@ip-172-31-82-230:~/Scripting$ ./create_file_and_folder.sh
ubuntu@ip-172-31-82-230:~/Scripting$ ls
DevOps create_file_and_folder.sh user_input.sh
ubuntu@ip-172-31-82-230:~/Scripting$ cd DevOps/
ubuntu@ip-172-31-82-230:~/Scripting/DevOps$ ls
create_file_and_folder.sh test.txt
ubuntu@ip-172-31-82-230:~/Scripting/DevOps$ cat test.txt
This is demo file for file and folder creation automatically
ubuntu@ip-172-31-82-230:~/Scripting/DevOps
3- Automate Conditional Scripting.
Here we will write a conditional script to find out the file with the name of the file.
ubuntu@ip-172-31-82-230:~/Scripting$ vim Conditional.sh
#!/bin/bash
echo "Enter the file name"
read filename
echo "checking if $filename exists..."
if [ -f $filename ]
then
echo "Yes $filename file exists"
else
echo " $filename file does not exists"
fi ---this is the ending of if condition.
# Lets run the script.
ubuntu@ip-172-31-82-230:~/Scripting$ ./Conditional.sh
Enter the file name
test
checking if test exists...
test file does not exists
ubuntu@ip-172-31-82-230:~/Scripting$ ./Conditional.sh
Enter the file name
user_input.sh
checking if user_input.sh exists...
Yes user_input.sh file exists
ubuntu@ip-172-31-82-230:~/Scripting$
Now we will check the same thing by passing value as an argument.
#!/bin/bash
filename=$1 ---- here we are using $1 to store the first value in argument.
echo "checking if $filename exists..."
if [ -f $filename ]
then
echo "Yes $filename file exists"
else
echo " $filename file does not exists"
fi
# Lets run the script.
ubuntu@ip-172-31-82-230:~/Scripting$ ./Conditional.sh test
checking if test exists...
test file does not exists
ubuntu@ip-172-31-82-230:~/Scripting$ ./Conditional.sh user_input.sh
checking if user_input.sh exists...
Yes user_input.sh file exists
ubuntu@ip-172-31-82-230:~/Scripting$
4-Backup Using Script, Manual and Automation.
This is the most important section of this tutorial, here we will write a script to take backups manually and automatically with the help of cronjob. we will create one more directory and the backup file will be stored in this directory only.
ubuntu@ip-172-31-82-230:~/Scripting$ mkdir auto_backup
ubuntu@ip-172-31-82-230:~/Scripting$ vim backup.sh
#!/bin/bash
#this is the source which we want to take backup.
src=/home/ubuntu/Scripting
#this is destination where we want to keep our backup file.
tgt=/home/ubuntu/Scripting/auto_backup
#here we are using filename as variable and storing the name in date format with particular argument.
filename=$(date | xargs | awk '{print $3"-"$2"-"$6}')
# tar is use to compress the file like we use winzip in windows.
tar -czvf $tgt/$filename.tar.gz $src
# this is the success mesage.
echo "backup complete"
ubuntu@ip-172-31-82-230:~/Scripting$ chmod 700 backup.sh
ubuntu@ip-172-31-82-230:~/Scripting$ ./backup.sh
tar: Removing leading `/' from member names
/home/ubuntu/Scripting/
/home/ubuntu/Scripting/backup.sh
/home/ubuntu/Scripting/DevOps/
/home/ubuntu/Scripting/DevOps/test.txt
/home/ubuntu/Scripting/DevOps/create_file_and_folder.sh
/home/ubuntu/Scripting/create_file_and_folder.sh
/home/ubuntu/Scripting/auto_backup/
/home/ubuntu/Scripting/user_input.sh
/home/ubuntu/Scripting/Conditional.sh
backup complete
ubuntu@ip-172-31-82-230:~/Scripting$ ls
Conditional.sh DevOps auto_backup backup.sh create_file_and_folder.sh user_input.sh
ubuntu@ip-172-31-82-230:~/Scripting$ cd auto_backup/
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$ ls
30-Apr-2023.tar.gz this is our backup file.
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$
Now let's schedule this job which will perform this backup automatically, so let's delete the backup file first.
ubuntu@ip-172-31-82-230:~/Scripting$ cd auto_backup/
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$ ls
30-Apr-2023.tar.gz backup.sh
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$ rm 30-Apr-2023.tar.gz
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$ ls
backup.sh
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$
#run the below command
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$ crontab -e
crontab: installing new crontab
# now add the below line of code, this will run every one minute and it will create a backup file.
*/1 * * * * . /home/ubuntu/Scripting/backup.sh
Wait for 1 minute and then check.
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup$ ls
30-Apr-2023.tar.gz backup.sh
ubuntu@ip-172-31-82-230:~/Scripting/auto_backup
As we can see that the backup is coompleted.
I hope this article was a little bit helpful to understand how scripting work, if you want more article on this topic as well as on cloud and DevOps then you may follow me on hashnode and Linkedin as well.
Thank you so much for your time.