what are shell scripts in linux and why you need them

A Shell Script is nothing but a text file which contains a series of commands that can be executed by the shell or the underlying operating system. Some of the commands that you enter in the command line might actually be shell scripts that execute a set of other lower level commands. Also many software in Linux use shell scripts as startup scripts.

The shell script file should have the execute permissions set on it so that the user can execute the file. The shell reads the commands from the script and then executes them in order. It is pretty much equivalent to entering each of these commands one by one at the command line.

Shell scripts provide some ease and convenience but also have several other advantages…

Ease of Use: If you have to execute a series of commands, then it is easier to have a script do them. You just have to type in the name of the script to execute several commands.

Repeated Usage: When you find yourself executing the same set of commands multiple times, it is time to create a shell script. This allows you to reliably execute them with out making errors such as typos and missed commands.

Easier to Remember: It is easier to maintain especially if you are using the several arguments and command line options. It is quite easy to miss a command option or argument if you are typing it every time in to the command line.

Loops and Conditions: In addition to the simple commands, the shell also provide the ability to test for conditions and to loop over variables and files. These can be pretty lengthy statements to type directly to the shell.

Easier for Distribution: If you are distributing or sharing software, which needs several different commands to executed for either the configuration or execution then it is best to make a shell script. It is much easier than having to document all of it. In addition, you can account for environment variations using conditional statements. This is the reason many software have a either a configuration or startup script.

There really is no special requirements for a file to be shell script. Having said that here is a set of things that distinguishes it from other files

File Permissions: As it was noted earlier, the script or the file should have appropriate permissions. The minimum requirement is that the user have both the read and execute permissions for the file.

Valid Commands: The contents of the file should be valid shell commands or statements that the executing shells can execute without errors. There might be some variations between shells in terms of syntax and your commands should be correct for the executing shell.

Shell Specification or Shebang: The first line of the script should specify which shell or program should be used to interpret the commands. This is crucial as all shells may not have the same syntax for commands. This is also called shebang.

Name or File Extension: Usually but not always, the script ends with an extension and it is usually the executing shell. The examples include extensions such as .sh, .ksh or .csh. This is mostly a convention rather than a requirement. You can have a script with no extension at all.

File Path: The script usually will reside in a folder bin/. Again this is just a convention and not a rule. You can pretty much put the shell scripts anywhere in the file system. If the folder is in the file path, then you can execute them by just entering the script name. Otherwise, you can execute them by providing either the relative or absolute path to the script.

Finally here is a short step by step procedure for creating a simple shell script.

Create a text file using your favorite text editor. You can use vi for this, but another text editor will do as well. We will name this script myhelloworld.sh.

$ vi myhelloworld.sh

Enter the contents of the script, starting with the shebang. The file contents look something like this:

echo "Hello World"

Now, save the file in a folder and change its permissions to make it executable.

$ chmod 755 ./myhelloworld.sh

Execute the script, by using the path

$ ./myhelloworld.sh

The script should now print out the text “Hello World” into the screen. You can now start adding more commands to the script.