4 Important Things You Need to Know About Shell Scripting  
4

4 Important Things You Need to Know About Shell Scripting  

By Nazifa Alam

Shell scripting is typically used to perform various system administration tasks such as disk backups, system log evaluation and installation of complex programs. It is executed by a UNIX shell command line interpreter for file manipulation, program administration, and text printing.  

Shell scripts are composed of a set of commands, which are performed by the Shell as instructed depending on its set parameters. In other words, the Shell is instructed to perform certain procedures where instructed. Individual commands are performed in a sequence to perform the operation and for this reason, it is known as shell scripting. Shell scripting is useful to have in place for repetitive tasks regarding system administration.  

The basic layout of the shell architecture of the operating system consists of: 

  1. User 
  1. Terminal 
  1. Shell (one of the two main components) 
  1. Operating System 
  1. Kernel (one of the two main components) 
  1. Hardware  

Types of Shell Scripting  

There are several types of shells that can be used for Shell Scripting and listed below, are four available shells that remain available to date: 

  • Bash – Also known as Bourne Again Shell, it is the highest-level script language available across all Linux and Debian based operating systems.
  • CSH or C Shell – Which uses the C programming’s shell syntax and is more identical to C. 
  • KSH or Korn Shell – This shell is a UNIX based scripting program similar to Bash. It is a high-level programming language.  
  • TCSH – Which is a more developed version of Berkley Unix C shell. There is no exact full form of TCSH and the shell supports C syntax.  

The Basics  

As Bash is the most commonly available shell that can be applied across all Linux based platforms (in addition to Mac OS), the following examples that will be covered further down are applicable to Bash. Due to the universality of the shell, scripting should not be an issue regardless of the platform.  

As scripting uses ‘administrator’ or ‘superuser’ privileges, it is important to put the intended scripts to test prior to entering them. It is important to use the right option for certain commands for example, i for rm commands as your interaction is needed.  

Creating a Shell Script  

Creating a shell script includes the following steps: 

  1. Using a vi editor or other editors to create a script and naming the file with extension .sh. 
  1. Beginning the script using #! /bin/sh
  1. Writing the script and saving the file. 
  1. Running the script using bash file[.]sh.  

Below is an outline of a script example involving the use of the is command to obtain the list of files and directory from the current directory: 

The script needs to begin with:  

#! /bin/sh 

And then followed up with,  

echo “what is your name?” 

read name 

echo “Hello, $name” 

The purpose of shell variables is to store data in the form of characters or number data. Specifically for this procured example, the name variable to store” John” (or other names) as a name can be indicated using $ as the prefix. The echo commands serve to show a text string on a file or a standard output.  

When the file runs as .sh, regarding this example, the following should appear: 

$[.]/file[.]sh 
What is your name? 
John 
Hello, John 

The following commands to be noted of are the following: 

  • gzip – For creating, extracting or viewing [.]gz files. 
  • zip – For creating or extracting zip files  
  • find – For searching certain files. 
  • echo – For showing a line of text. 

Further Guidelines  

Discussed below are some relevant programming components such as variables, control structures and other important aspects to be aware of: 

  1. Every script should begin with ‘#! /bin/bash’ so the script gets directed to the Bourne shell. The function ‘#!’ directs the script to the interpreter called shebang.  
  1. Every new line serves as a new command. 
  1. Comment lines should begin with a #
  1. Commands should be surrounded by ().  

The Hash-Bang Hack  

The most direct way to single out the file as a script is to ensure that the first line is: 

#!/bin/bash 

Beginning the script with # in addition to following it up with ! forces the script to be performed with the shell the line points to.  

Each New Line is a New Command  

Every new line is a new command or a constituent of something larger. For example, if/then/else statements will need multiple lines to complete however, each of these new lines are components of the entire statement. It is important to make sure that new commands interrupt these lines to prevent errors from occurring. Text editor should be turned off using ALT+L.  

Adding in Comments  

If you begin a line using #, the line is ignored and turned into a comment line. Using this newly created comment line, you can create reminders to yourself concerning the output of the previous command or what the next command will do. Applying this practice will help you and others edit your scripts more easily where needed. It is also important not to follow #s with !s due to the previously mentioned Hack-Bang function being an exception of this.  

Using Parentheses  

As the basics of shell scripting are being touched down upon, it is important to mention that parentheses should be used to create command substitutions. This is because it works better to place commands within other commands using this method.  

Stay up to date with the latest threats

Our newsletter is packed with analysis of trending threats and attacks, practical tutorials, hands-on labs, and actionable content. No spam. No jibber jabber.