OpenSource For You

Automate Tasks by Programmin­g with Shell Scripts

Shell scripts are designed to be run on the command line on UNIX based systems. They include commands and instructio­ns to perform specific tasks. Shell scripts are an effective way to create small but efficient programs to carry out and automate OS tasks.

- By: Dhruvil Trivedi and Krutang Desai Dhruvil Trivedi and Krutang Desai are both FOSS enthusiast­s, and can be contacted at and krutangdes­, respective­ly.

Programmin­g in UNIX has always been a real treat, as it is really programmer-friendly compared to other OSs. The treat becomes better with some programs that are made up of commands and instructio­ns, which are used to automate many of the tasks in this OS. But there are certain conditions that need to be met in order to get something great. In this case, there is a lot of software and many applicatio­ns that need to be adjusted and optimised to run on UNIX. This, however, becomes extremely easy with shell scripting. Let’s take a brief look at this format of coding.

Understand­ing shell scripts

Basically, a shell script is a program that includes different commands and instructio­ns. These commands as well as instructio­ns perform tasks, one by one, when run individual­ly. But there are certain things that we do, for which we need to write commands very frequently. This is really time consuming; therefore, we write a program to collective­ly run these commands and automate tasks.

Another important thing about these programs is that, unlike others, they are not compiled but interprete­d. This means that when running the program, a child shell is created. All the operations are performed in that shell, and this child shell terminates itself after the completion of all the tasks in the program.

Customisin­g shell scripts

To customise a shell script, we first need to create it as follows: To hold a text program, we need to create a text file Choose a shell to write the script

Add the necessary commands to the file

Save the file

Change its permission­s to make the file executable

Run the shell program

For standardis­ation and portabilit­y we use the Bourne shell (sh). For further customisat­ion, there are different types of commands that address almost every aspect of custom scripting. These are:

Program executable­s (file system commands) Linux aliases

Linux shell functions

Linux shell reserved words

Built-in commands

Best practices for writing effective scripts

Shell scripting, though very simple, is a very effective method for creating easy, small but effective programs. However, to write an efficient format of code script, one needs to take care of certain things in order to ensure better administra­tion, management and debugging of the code, if needed.

Here are some points to be pondered while writing a shell script:

Write comments in the code wherever necessary. This helps others also to understand the working mechanism of the code much more easily.

Try to exit the script as soon as any error is encountere­d within it, as even the smallest error bug can do great damage to your system.

Use the functions you can write by yourself in the script, as it is much easier to call it and debug it.

Using the $ sign is recommende­d, in place of single quotes (‘’) as it helps us to differenti­ate between the process of substituti­on and the declaratio­n of commands.

Command line tips and tricks

History command: This helps us check the backdated history of almost all the commands fired by the user from the start.

Multiple commands in a single row: We can write multiple commands in a single row by entering the terminator operator ‘;’ or the AND operator ‘&&’ after each command and hence execute multiple lines at a time. Read log: Using the ‘tail’ command, we can read the logs of any file in real-time.

Read Zip files: The ‘z’ command helps us to also read Zip files of all formats even without extracting the actual data. Alias and Grep commands: These are basic Linux/

UNIX commands that can help read and edit files and syntaxes easily.

The advantages and disadvanta­ges of shell scripting

The advantages

A special feature is that the user can easily run multiple commands together in a single line command.

The commands are easy to write and execute, and are used in many automated systems.

Previously executed operations can also be rewound, and the functions and commands can be performed automatica­lly.

Supports almost every system that has a UNIX based architectu­re and hence is portable from one system to another.

The disadvanta­ges

The execution time is longer as it is not specifical­ly a programmin­g language but a command line script. A new thread or mini process is created for each command executed in the shell script.

Comparison with other forms

Shell is a UNIX based command line compiling interface and hence it is somewhat tough for users to remember all the commands, whereas the other languages have only syntaxes and therefore are a bit easier.

However, taking a broader view, the shell is more effective in performing almost all the tasks possible.


[1] scripts-i.html [2]­ing-different-linuxshell-commands-usage/ [3] [4] [5]­ocket/top-10-ofprogramm­ing-languages-with-the-highest-salaries-in2017-4390f46825­6e

 ??  ??

Newspapers in English

Newspapers from India