Shell script from Zero to Hero

Shell script from Zero to Hero

·

4 min read

What is Shell/bash script

Shell scripting is used for multiple purposes, such as you can execute shell commands, and multiple commands together to achieve a particular task, customize administrative tasks, and perform task automation etc.

It is very important for a Linux user that he/she should know the basic shell commands before diving into shell scripting.

Let's start with a very basic command in to print a Hello World by the shell scripting.

$ nano hello.sh // will create a new file with name hello.sh
#!/bin/bash // it notify to shell that this is a shell script 
echo "Hello World"  // basic command to print a message

// Save the file just pressing CTRL+Z then press Y ENTER

Now, Let's try to run the script.

$ ./hello.sh
// But you are getting error as below.
-bash: ./hello.sh: Permission denied
It means this file does not have execute permission

How to give execute the permission to file hello.sh

$ chmod +x hello.sh

Now run hello.sh again

./hello.sh
Hello World

Yayyyyyy....

Cool!!! we have moved our baby step ahead...

Let me give a hack here..

I have a file in which I want to print a line from the new line and remove the \ from the string and print it.

#!/bin/bash

echo "Hello World"

echo "Printing test new line "
echo -n "printing text without new line"
echo -e "\nRemoving \t backslash \t characters\n"

Now run the hello.sh

./hello.sh
Hello World
Printing test new line 
printing text without new line
Removing      backslash      characters

Tip 1 : How to comment in a shell script, easy just put # in front of the line.

Now, Let's jump to a little bit advanced level

Use of While loop

Let's create a sh file with name while.sh

$ nano while.sh

Now jump to the file editor and make an easy program on a while loop.

#!/bin/bash

val=true
count=1
while [ $val  ]
do 

echo $count
if  [ $count -eq 5  ];
then 
break
fi
((count++))
done

Run while.sh

./while.sh
1
2
3
4
5

Similarly, let's use for loop.

Create a for.sh file and paste below loop example.

nano for.sh
for ((count=10; count>0; count--))
do 
echo  -n  "$count "
done
printf "\n"

Now run for.sh

./for.sh
10 9 8 7 6 5 4 3 2 1

Let me introduce you to "read" command which is used to take input from the user.

Let's create a shell script for the same.

#create read.sh by the below command
nano read.sh
#paste below code into the file
#!/bin/bash

echo "Enter your Name"

read name

echo "Welcom $name to the DevOps World"

Here is the output.

 ./read.sh
Enter your Name
Saif
Welcom Saif to the DevOps World

If statement in shell script sxample

#create if.sh file
nano if.sh
#!/bin/bash
s=10
if [ $s -lt 10 ]
then 
echo "This is one digit number"
else
echo "This is two digit number"
fi

Now run if.sh

$ ./if.sh
$ This is two digit number

Create a function in shell script.

nano fun.sh

#!/bin/bash
function f1()
{
echo "Hello Worl, this Function F1"
}
f1

Run fun.sh

./fun.sh
Hello Worl, this Function F1

Now let's do one small example of the cron job.

Crone Job

I want to turn on my laptop tomorrow at 8:55 AM automatically.

nano morning.sh
#!/bin/bash
sudo rtcwake -m no -l -t $(date +%s -d 'tomorrow 9:00')

In order to turn on your laptop at a specific time using a shell script, you can use the rtcwake command, which allows you to set a specific wake-up time for your computer.

This script uses the rtcwake command with the following options:

  • -m no tells the command to not put the computer into a sleep mode before waking it up.

  • -l specifies that the hardware clock should be used for the wake-up time.

  • -t specifies the wake-up time in seconds since the Unix epoch. The $(date +%s -d 'tomorrow 9:00') command generates the wake-up time as the number of seconds since the Unix epoch for tomorrow at 9:00 AM.

Note that in order to run the rtcwake command, you will need to have root access, which is why the script uses sudo.

To schedule this script to run at a specific time, you can use the cron system scheduler. For example, to run the script every day at 8:55 AM, you can add the following line to your crontab file (crontab -e to edit it):

crontab -e
55 8 * * * /path/to/morning.sh

Your computer will be turn on tomorrow at 8:55 AM.