Shell Scripting Tutorial — Basic Concepts of Shell Scripts

Er Ravindra Pawadia
3 min readNov 7, 2020

Normally shells are interactive. That means shell accept command from you (via keyboard) and execute them. But if you execute a sequence of commands ( you can store this sequence of command into text file) and instruct the shell to execute this text file instead of entering the commands one by one. This is known as shell script.

Shell script is not compiled before executing. It is executed with the help of an interpreter. An interpreter is a shell program. There are many shells (i.e. interpreters) available in Unix like Operating Systems. These are as follows,

Note : /etc/shells file will display list of installed shells.

Difference between programming & scripting

Programs are compiled and then executed whereas scripts are not compiled, these are directly executed by the interpreter.

When to write shell scripts?

You can apply shell scripting on wide variety of system and database administration tasks.

Necessity is the mother of invention. Initially shell scripts will be manual tasks which will be done on a regular basis automatically.

Tasks which are done rarely enough that their method or even their need may be forgotten.

  • Periodic business related reports(daily/weekly/monthly/quarterly/yearly)
  • Offsite backups
  • Purging old data

Some tasks can be performed manually but may be automate by scripting.

  • Checking for database locks
  • Killing runaway processes

These are few tasks which would not be possible without a programming language.

  • Getting OS information (performance stats, disk usage, etc.) into the database.
  • High Frequency System Monitoring.

Executing First Shell Script

To create any program you can use your favorite editor in Linux OS. Here we’ll call hello.sh as our first shell scripts.

$ vim hello.sh

Insert the following shell command in the above file,

#!/bin/bash echo "Hello World!"

Save and exit the editor and then run the program.

$ ./hello.sh [ press Enter] bash: hello.sh: cannot execute - Permission denied

What is the problem here and how can we fix it?

The problem is, you are not having execute permission on file so let us give execute permission on this file first,

$ chmod u+x hello.sh

Once the problem above is fixed, find the path of your script.Suppose in this case the full path of hello.sh is /home/mahesh/hello.sh now execute the script as follows.

$ /home/ravi/hello.sh [Enter] Hello World!

#!/bin/bash — This first line indicates which interpreter to use when running this script.

The shebang is a special comment. Since it is a comment it doesn’t execute when the script is run. before the script run, the shell calling the script will check for the #! pattern. If found it will invoke the script using that particular interpreter. If no #! is found most shells use the current shell to run the script.

Although shells are installed in different locations on different systems you may have to alter the #! shebang line. For instance, the bash shell is in /bin/bash, /usr/bin/bash or /usr/local/bin/bash. Setting the shell explicitly like this ensures that the script will be run with the same interpreter regardless of who executes it.

Read Also : Top 10 Shell Scripting Interview Questions with Answers

Originally published at https://thecodecloud.in on November 7, 2020.

--

--

Er Ravindra Pawadia

Hi Guys, This is Ravi. I am AWS and Oracle Certified Solution Architect Associate. I love to write technical blogs on my blogging site https://thecodecloud.in .