Shell Scripts

shell script is a text file that contains one or more UNIX commands. You run a shell script to perform commands you might otherwise enter at the command line. Shell scripts are useful because you can combine many common tasks into one script, saving your time and possible errors when performing similar tasks over and over. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions

Shell Types

n Unix, there are two major types of shells 

  • Bourne shell : –  If you are using a Bourne-type shell, the $ character is the default prompt.
  • C shell :- If you are using a C-type shell, the % character is the default prompt.

A shell script begins with a character combination that identifies it as a shell script—specifically the characters # and ! (together called a shebang) followed by a reference to the shell the script should be run with. For example, here’s the first line of a shell script that would be run with sh:

#!/bin/sh alternatively , possible also to use #!/bin/bash

You should document your shell scripts with comments. To add a comment, start the line with the number sign (#). Every line of a comment needs to begin with the number sign: You can also put blank lines in a shell script to help visually distinguish different sections of the script.

Example : – Comment in the shell script

  • #This program returns the
  • #contents of my Home folder

File Extension & Execution

The shell script file has an extension .sh . After the shell script is create, it is required to change the file to executable using a command chmod. The applicaton to change the file to executable will be as follow

Example : – Change the file to executable

  • chmod +x fileName.sh 

Example : – Running a shell script

  • ./fileName.sh  or alternatively, possible to run as well bash fileName.sh

Figure 1.1 – Shell Script Command

Figure 1.2 – Shell Script generate a today date

User Inputs/Read Inputs

Shell is, about, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, it is still just a list of commands executed sequentially. Consider the method read which takes the input from the keyboard and assigns it as the value of the variable and print the ourcome. Have a look the following example

Figure 1.3 – Shell Script read an input and print our

Variable

Variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. The shell enables you to create, assign, and delete variables. The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _) By convention, Unix shell variables will have their names in UPPERCASE.

  • Defining Variables : – Variable declaration consists of variable NAME and its value
  • Accessing Variable : – Accessing variable dollar sign ($

Figure 1.4 – Variable Definition and Accessing

Scroll to Top