An Advanced Guide to Shell Scripting in Unix!

Introduction 

A shell script is a powerful tool that is used to automate a wide variety of tasks. A script is simply a text file containing a series of shell (command line) commands executed in order, one after the other. This allows the user to automate tasks that would otherwise have to be performed manually.  

Generation of reports and performing complex administrative tasks is possible due to shell scripting. In addition, shell scripts can automate tasks that would otherwise have to be performed manually. This can save the user time and effort and make complex tasks easier.  

What Is Shell Scripting? 

Unix shell scripting uses a shell script by the Unix shell. A shell script is a program that provides a command-line interface (CLI) for interacting with the operating system. These are typically written in a text editor such as vi or emacs. 

Shell scripts can automate various tasks, such as system administration, application installation, etc. They are also often used to provide a user interface for applications that don’t have one. For example, a shell script may also print a menu of options and execute the selected option. 

Developers write shell scripts in a scripting language, a type of programming language designed to be easy to learn and use. The most popular scripting language for shell scripts is the Bourne shell, the default shell on many Unix-like systems. Other popular scripting languages include bash, Csh, and Ksh. 

Types of Shells 

There are two main categories of shells in Unix:

Bourne shell: The default prompt for a Bourne-type shell is the $ character. 

C-Shell: When using a C-type shell, the default prompt is the % character.

The following subcategories are included in The Bourne Shell:

  • Shelley’s Bourne (sh) 
  • Shelled korn (ksh) 
  • Shell for Bourne Again (bash) 
  • Shell POSIX (sh) 

The various C-type shells are as follows:

  • Shell C (csh) 
  • TENEX/TOPS Shell C (tcsh) 

While working at the AT&T Bell Labs in New Jersey in the middle of the 1970s, Stephen R. Bourne created the initial Unix shell. The Bourne shell is referred to as “the shell” because it was the first shell to arrive on Unix systems. 

The Bourne shell is commonly installed as /bin/sh in most Unix versions. Because of this, it is the preferred shell for creating scripts that can run on different Unix versions. 

Features of Shell Scripting 

Given below are some of the capabilities of shell scripting: 

1. Automation of Repetitive Tasks 

If you regularly perform a task that requires a series of steps, you can use a shell script to automate the task. This can save you time and effort and help to ensure that the task is performed each time correctly. 

2. Launch and Control Applications 

You can use a shell script to launch an application and then control how the application runs. This can include specifying the input and output files and the required options. 

3. Create, Modify, and Delete Files and Directories 

A shell script can be used to create a new file and then add text to the file. You can also use a shell script to delete a file or modify an existing file. 

4. Manipulate Text Files 

A shell script can be used to search for a particular word or phrase in a text file and then replace it with another word or phrase. You can also use a shell script to extract information from a text file or to convert a text file from one format to another. 

5. Generate Reports 

You can use a shell script to generate a report that lists all the files in a directory or all the users on a system. You can also use a shell script to generate a report that summarises the usage of a system. 

6. Perform System Administration Tasks 

It is possible to add a new user to a system or delete an existing user using a shell script. You can also use a shell script to change a user’s password or create a new group. 

7. Create, Modify, and Delete Users and Groups 

You can use a shell script to create or delete a new user. You can also use a shell script to add a user or remove a user from a group. 

8. Manage File Permissions 

A shell script can be used to grant read and write permission or deny read and write permission to a file. You can also use a shell script to change a file’s owner or the group of a file. 

9. Monitor System Performance 

You can use a shell script to monitor the CPU usage of a system or to monitor the memory usage of a system. You can also use a shell script to monitor the disk usage system or to monitor the network usage of a system. 

10. Troubleshoot System Problems 

You can use a shell script to check the configuration of a system or to find out what processes are running on a system. You can also use a shell script to determine why a system is not responding or to find out what is causing a system to crash. 

Advantages of Shell Script 

1. Automation of Repetitive or Tedious Tasks 

If you find yourself performing the same task repeatedly, a shell script can save you a lot of time and effort. You can run the shell script by automating the task and letting it do the work for you. 

2. A Consistent Interface to a Complex System 

If you have a complex system with many different components, a shell script can provide a consistent interface to the system. This can make it easier to use the system and reduce the learning curve. 

3. Perform System Administration Tasks 

Shell scripts can be used to automate system administration tasks. This can save you a lot of time and effort and help ensure that tasks are performed consistently. 

4. Create new Commands or Utilities 

If you need a new command or utility, a shell script can be used to create it. This can be a great way to extend the functionality of the system. 

5. Extend the Functionality of Existing Commands or Utilities 

If an existing command or utility is lacking in some way, a shell script can be used to extend its functionality. This can be a great way to add new features or fix existing problems. 

Disadvantages of Shell Script 

1. Not Portable 

Shell scripts are not portable, which means they can only be used on the system where they were created. If you need to use a shell script on another system, you must port it to that system. 

2. Not as Fast as Compiled Programs 

Shell scripts are interpreted, which means they are slower than compiled programs. If speed is critical, consider using a compiled language. 

3. Not as Flexible as Compiled Programs 

Shell scripts are not as flexible as compiled programs. This means they can only be used for the tasks they were designed for. If you need to perform a task that is not supported by a shell script, you will need to use a different language. 

4. Not as Easy to Debug as Compiled Programs 

Shell scripts are not as easy to debug as compiled programs. This means that it can be more difficult to find and fix errors in a shell script. 

Example of Shell Script 

Let’s say we write the test.sh script. Keep in mind that every script would end with .sh. The first thing you need to do in your script is to notify the system that a shell script is being started before adding anything else to it. The shebang syntax is used for this. For instance, 

#!/bin/sh 

This instructs the system that the Bourne shell will run the next instructions. The # symbol and the! Symbols are referred to as hashes and bangs, respectively, which is why it is known as a shebang. You must include the shebang line before adding the commands to a script that contains these commands. 

#!/bin/bash 

pwd 

ls 

Extended Shell Scripts 

There are a number of necessary constructs in shell scripts that instruct the shell environment on what to do and when to do it. Naturally, most scripts are more intricate than the ones mentioned. 

After all, the shell is a true programming language with variables, control structures, and other features. No matter how complex a script becomes, it is still nothing more than a series of commands that are run in order. 

The script follows uses the read command, which assigns the keyboard input value to the variable PERSON before printing it on STDOUT. 

#!/bin/sh 

# Author : john doe 

# Script follows here: 

echo “What is your name?” 

read PERSON 

echo “Hello, $PERSON” 

Sample Run Script 

$./test.sh 

What is your name? 

John doe 

Hello, john doe 

$ 

Conclusion 

Shell scripting makes business-grade task automation possible. While shell scripts have some disadvantages, such as not being as fast as compiled programs, their advantages far outweigh these disadvantages. Shell scripts are easy to learn and use and can perform various simple and complex tasks. 

Related Articles

} }
Request Callback