A little bit about Shell Scripting

In this tutorial, we are going to discuss a little bit about shell scripting and the basics.

Shell

There are lots of shells are used in the Linux operating system. Bourne Again Shell, C Shell, Korn Shell are some of the shell used in the Linux. All these shell function same jobs but recognize different commands and provide different built-in functions.

 

Bash: It is the free version of the Bourne shell. Bash is similar to the original but has added features such as command line editing. On Linux system, bash shell is one of the s most widely used. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.

 

C shell: It is shell created as an alternative to Bourne shell. The C shell was built mainly for the programmers and which are similar to the C programming language.

 

Korn shell: The Korn shell is the comprehensive combined version of other major shells. It also was the base for the POSIX Shell standard specifications etc.

 

Scripting

Usually, shells are the two-sided process, they accept command as the input and execute them. There are thousands of commands available for the command line user, it is not possible to remember them all. So we can use shell scripting it writes a series of command for the shell to execute. It can be useful for executing lengthy and repetitive sequences of commands by combining all into one script. This reduces the effort required by the end user by avoiding repetitive work and automation. It can be used for routine backups and for system monitoring.

A shell script comprises the following elements.

Shell keywords:

if,then,else,elif,fi,case,etc

Shell commands:

Alias,bg,bind,break,builtin,caller,cd,command

Functions:

Functions are nothing but small subroutines or subscripts within a Bash shell script.

Shell 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 (_).

 

Simple demo of shell scripting using Bash Shell

Below is a small script which will use a variable.

#!/bin/sh

echo “what is your name?”

read name

echo “How do you do, $name?”

read remark

echo “I am $remark too!”

 

The following shell script will be used for sending email alert when the system is running with low memory.

#!/bin/bash

########################################################################

#Define mail variables

#subject

subject=”Your server memory is running low”

#sending mail as

from=”test@example.com”

#sending mail to

to=”client@example.com”

#send carbon copy to

also_to=”admin@example.com”

 

# Calculate the free memory size(MB)

free=$(free -mt | grep Total | awk ‘{print $4}’)

 

#Compare the value with the defined limit

if [[ “$free” -le 100  ]]; then

#Get list of process consuming system memory

ps -eo pid,ppid,cmd,%mem,%cpu –sort=-%mem | head >/tmp/top_proccesses_consuming_memory.txt

 

file=/tmp/top_proccesses_consuming_memory.txt

# Send notification to the client and admin if memory is below the limit

echo -e “Warning, server memory is running low!\n\nFree memory: $free MB” | mailx -a “$file” -s “$subject” -r “$from” -c “$to” “$also_to”

fi

exit 0

 

If we have not made the file executable, we cannot able to execute the shell script due to the permission issue. To make the shell script executable we need to give the file executable permission using the following command.

# chmod -x path/to/our/file/backup.sh

Then from a terminal prompt, run the following command.

# ./backup.sh

We have covered many shell features, but we have focused on those features most often used directly on the command line. The shell can be also used0020sto add some features when writing programs.

 

If you need any further help, please do reach our support department.

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)