Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.

Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, variables. Scripts make it easy for the users to save certain sequences of codes that might be used again and again. Shell scripts can also have comments to increase readability.

A shell script needs to be saved with the extension .sh.

To let the Linux system know that the file is a shell script, the file needs to begin with the shebang construct.

After this, the script can contain commands, functions, loops, conditional checks, etc.

A good script always contains comments that make it readable.

Creating and running a basic shell script

A shell script can be created using vi, cat command, or the normal text editor in GUI.

Let’s create a basic shell script using vi

This will take you to the vi editor. Add the following lines:

This simple script should display the current user followed by the date.

To save and exit the vi editor:

  • Press ESC
  • Type :
  • Type in ‘wq’
  • Hit Enter

By default, the creator of the script does not get executable permission for the file.

To change that:

This will give you (current user) the permission to execute the file.

To run the script :

basic-script-1

The first line of output corresponds to ‘whoami’ command and the second line to ‘date’ command.

Another way of running the script is :

Running the file this way might require the user to give permission first. Running it with ‘bash’ doesn’t require the permission.

basic-script-1

The same script is running with ‘bash’ before it, but having permission issues when tried to execute directly. The reason this is happening is that the command bash [filename] only needs read permission from the file.

Whereas the command ./[filename] run the file as an executable and hence requires the permission to execute. This question has been answered in detail on StackExchange.

In general it is better to provide executable permission.

Using variables in shell scripts

Scripts can include user-defined variables, in fact as scripts get voluminous in size it is essential to have variables that are clearly defined. Variables that are self-descriptive in nature is another quality of a good script.

Add the following lines to the script :

shell-script-in-vi

GREETINGS is the variable defined and later accessed using ‘$’.

There should be no space in the line where variables being assigned a value.

Let’s run the script:

shell-script-in-vi

Reading input from the command line

Shell scripts can be made interactive with the ability to accept input from the command line. Read command can be used to store the command line input in a variable.

A variable NAME has been used to accept input from the command line.

On running the script :

basic-script-1

Defining functions

Users can define their own functions in a script. These functions can take multiple arguments.

In the script add :

This script will ask the user for a directory name. It will then create the directory and cd into it.

Conclusion

We saw how scripts can be used to run commands in a serial order. Scripts help users in reducing redundancy and save time. Scripts can also be shared among different users.

The Scripts we saw in this tutorial were quite basic, scripts can be designed to perform complex tasks as well. To learn more about scripting refer this.

By admin

Leave a Reply