How to create Shell Scripts in Linux System

This blog post will teach you how to create Shell Scripts in Linux, including the basics of shell scripting, how to write shell scripts, and how to execute shell scripts.

Scripting Basics

  • Shell scripts are test files containing commands to be executed.
  • Shell scripts are useful for:
    • Automating commonly used commands
    • Performing system administration and troubleshooting
    • Creating simple applications
    • Creating simple applications
    • Manipulation of text or files

A shell script is simply a text file containing commands. Scripts are useful for automating processes that you perform repeatedly at the command line. For example, suppose every morning when you log in, you perform the following operations:

  • Check the system date.
  • Look at the calendar for the current month.
  • Check your email.
  • Display the list of logged in users.

Instead of entering the commands to perform steps 1-4 individually, you could create a shell script containing those command. Every morning you could enter just one command to perform these steps. in addition, one of the key benefits to using Linux is its powerful, yet simple commands. These simple commands may be put together in a script to perform complex operations or automate procedures such as adding batches of users.

Linux users involved with system administration and troubleshooting often work with shell scripts. Many of these are created during the installation of the operating system. Consider the script /etc/profile. This file is the system-wide user login script which runs whenever a user logs into the system,. System administrators and troubleshooters will consider operations performed in this script when troubleshooting user login problems.

Programmers often create simple versions of programs using scripts during the initial phases of a programming project. This is called application prototyping. Once they and the program’s users are happy with the main functionality of the program, the programmer will then create the full-featured program in a programming language such as C.

Creating Shell Scripts

  • Step 1 : Create a text file containing commands
    • First line contains the magic shebang sequence: #!
      • #!/bin/bash
  • Comment your scripts!
    • Comments start with a #

Shell scripts are text files that generally contain one command per line, but you can have multiple commands on a line if you separate them with semicolons (:). In order to continue a command onto the next line you use the line continuation character. For the Bourne shell(/bin/sh) and its derived shells such as bash, this is a backslash followed by a newline. you can enter this by pressing the \ key followed by the Enter key on most keyboards. This will enable you to enter one command that spans multiple lines.

The first line in a shell script should contain ‘magic’, which is commonly referred to as the shebang. This tells the operating system which interpreter to use in order to execute the script. Some examples are:

Shebang Use

  • #!/bin/bash – used for Bash scripts (most common on Linux)
  • #!/bin/sh – used for Bourne shell scripts (common on BSD derived systems)
  • #!/bin/csh -used for C shell scripts (common on BSD derived systems)
  • #!/user/bin/perl – used for Perl scripts (an advanced scripting and programing language)
  • #!/usr/bin/python – used for Python scripts (an object-oriented programming language)

Commenting Shell Scripts

It is extremely important to put comments in your shell scripts. Anything following the # symbol is a comment and therefore ignored by the interpreter. It is good practice to make a shell script self-documenting. Self-documenting means that someone with almost no scripting knowledge can read your comments in the script and reasonably understand what the script does. The easiest way to do this is to write the comments before you actually write the code. This has an additional benefit of clarifying to yourself, what your objective is. In addition, this practice makes the script easier to maintain for both yourself and others. As time progresses, you may not recall what you were trying to do at the time and comments may help clarify the original objective.

  • Step 2: Make the script executable:
    $chmod u+x myscript.sh
  • To execute the new script:
    • Place the script file in a directory in the executable path, such as ~/bin or /usr/local/bin -OR- Specify the absolute or relative path to the script one the command line.

After you have created the script and saved it using a text editor, you will need to change its file permissions in order to make it executable. (Note that scripts must also be readable to run so that the shell can read the script and interpret it into executable code.)

An example of making a script you own executable:

[user@stationX ~]$ chmod ug+x scriptfile

-or-

chmod 750 scriptfile

Ensure that the script is located in a directory listed by the PATH environmental variable. To do this, enter the following command:

[user@stationX ~]$ echo $PATH

If the script is not in a directory listed in the PATH variable, either move the script to a directory that is (such as $HOME/bin) or specify the absolute or relative path on the command line when executing the script:

[user@stationX ~ ]$ /home/user/mytestscript

OR

[user@stationX ~]$ cd /home/user

[user@stationX ~]$ ./mytestscript

You can create a bin directory in your home directory and store your own scripts here. Since this directory is normally added in your PATH environment variable, you can run any of your scripts without having to type in the full path. You may need to open a new shell or re-run your shell initialization files after you first create the directory.

Sample Shell Script

#!/bin/bash
# This script displays some information about your environment

echo “Greetings. The date and time are $(date)”
echo “Your working directory is: $(pwd)”

Assuming that this script was named info.sh, it would display something like:
[user@stationX ~]$ ./info.sh
Greetings. The date and time are Fri Oct 29 16:14:36 IST 2022
Your working directory is: /home/user

Note that the script is invoked as ./info.sh, not just info.sh. In order to refer to a script by the name alone, it must be in one of the directories collectively referred to as your PATH. To see the directories in your PATH, run the command:

[user@stationX ~]$ echo $PATH
/usr/erberos/bin:/usr/local/bin:/user/bin:/bin:/user/X11R6/bin:/home/user/bin

Note that your home directory is not in this list. Because the script is not in your PATH, you must explicitly give its location or bash will not know which file you are referring to. Because. refers to the current directory, you could use ./info.sh instead of ~/info.sh or /home/user/info.sh

Note that most users keep thir scripts in ~/bin because it is part of the default PATH.

Leave a Comment