Connect with us


How useful is shell scripting in a Linux/Unix environment?



shell programming

Let’s start with a simple definition of a shell; “what is a Shell?”

shell programming

A shell is a program that acts as the interface between you and the Linux/Unix system, allowing you to enter commands for the operating system to execute. So much more powerful, and it’s really a programming language in its own right. 

Not only can you execute commands and call Linux/Unix utilities, but you can also write them. It’s an interpreted language that makes debugging easier because you can execute single lines and there’s no recompile time.

How useful is shell scripting in a Linux environment?

Shell is very useful because you can program the shell quickly and simply, in addition, a shell is always available on the most basic Linux/Unix, Mac, and Android OS. By simply prototyping, you can find out if your idea works. It’s also ideal for any small utilities that perform some relatively simple task where efficiency is less important than easy configuration, maintenance, and portability. Other uses include organizing process control, which enables commands to run in a predetermined sequence dependent on the successful completion of each stage.

Let’s examine Shell as a Programming Language.

There are two ways to write shell programming:

  • Interactive: You can type a sequence of commands on your terminal/console and allow the shell to execute them interactively.
  • Non-Interactive: stores commands in a file and invokes it as a program.

For example, using interactive

ls -lah | more 

The above command uses the ls and more utilities and pipes the output to a screen at a time. Or let’s do another example:


Let’s verify that your server can send an email out to the world with a single command line, this try:

echo "Test"|mail -s "Testing email server" -r [email protected] [email protected] 

The above command will send an email to your email address at [email protected]Check your inbox or junk mail. If not received, that indicates that something is wrong with your server, or mail utilities not installed on your server. You can install mail utilities by doing:

apt-get install postfix  && apt-get install mail-utils -y 

After installation try the following commands;

echo "Test"|mail -s "Testing email server" -r [email protected] [email protected] 

Check your email again. Now if it works, let’s build a small Linux program/scripting that we can use to send us an Alert if our server or service goes offline/online, CPU usage, etc, this demonstrates a non-interactive example:

echo '  
# Filename : 
# The script send email notification to server admin.  

MsgToSend="Good morning, servers are running 100% on-line!" 
RCPT="[email protected]" 
SUBJ="Server Notification" 
FROM="[email protected]"  

echo MsgToSend | mail -s SUBJ -r FROM RCPT  ' >  
# make it executable chmod +x  
# Run your progam by doing: 

Check your email both inbox and spam to see if you receive the test email, in not check your mail log for errors.

Now you have a program to you can call that will send you an email.

Your alert program can be used to send alerts for the following services:


Server monitoring

  • Server offline
  • Server online
  • Server Health

Service monitoring

  • Service stop running
  • Service back online
  • CPU usage when above normal
  • Temperature monitoring and alert

Access monitoring

  • User access login alert
  • User-privileged command alert on execution
  • Failed login attempt alert

File Monitoring

  • Hard disk inspection and alert
  • Backup and Restore
  • Files integrity (file change alert)

Network monitoring

  • bandwidth monitoring
  • packet filtering
  • and more.

EDIT: Added more shell scripting examples; C++, Python, and PHP.

Or if you want to print every command ever typed on a compromised system?

Just do :

history | col -b | lpr #

and watch all the history or evidence flying out in ink on your connected printer. Cool?

There’s more to Shell Scripting in a Linux environment than just writing bash scripts, other interpreted languages that people may also use include C/C++, Perl, and Python.

Shell is considered a high-level programming language for Unix/Linux and because Linux is so modular, you can slot in one of the many different shells from the following:

  • sh(Bourne) – the original UNIX Shell
  • csh|zsh|zsh – The C shell created by Billy Joy[1] of Berkeley UNIX fame.
  • ksh|pdksh – The Korn Shell, written by David Korn[2]
  • rc – More C than csh from the GNU project
  • bash – Bash (Bourne again shell) is a shell replacement for the Bourne shell. Bash is a superset of sh. Bash supports sh. POSIX is a set of standards defining how POSIX-compliant systems should work. Bash is not actually a POSIX-compliant shell.

Let’s take a look at another example using a shell program that asks for user input:

echo ' 
echo "Is it morning? Please enter y for Yes, n for No" 
read UserInput  
if [ $UserInput = 'y' ]; then   
  echo "Good morning boss, making your coffee"   

# other morning task goes here. 
  echo "It's afternoon, time to start backups"
# evening tasks goes here. 
exit 0 

# Make it executable  
chmod +x  

# Test your program 

Also, you can write programs in C/C++, Perl, and Python and run them directly from the command line or from a shell script. Let’s write a famous “Hello, World!” in C.

echo '
#include <stdio.h>  
int main() { 	
  printf("Hello, World!\n"); 	
return 0; 
'> hello.c  

# compile it. 
gcc -o hello hello.c  

# Test it, run it. 

The above codes will create 2 program files, one hello.c and an executable hello file that can be invoked anytime to run the program. You can create programs in C/C++, python, perl without leaving your shell prompt. For example in:


Writing and compiling Hello World! in C++.

echo ' 
# Microsoft C/C++ Optimizing Compiler Version 19.00.23506 x64  
#include <iostream>  
int main() {     
  std::cout << "Hello, world!\n"; 
'> hello.cpp  

#Compile it.
g++ -o hellocpp -hello.cpp  
#Run it  ./hellocpp 

Hello World! In Python

echo ' 
#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
#python 3.5.2  

print ("Hello, world!")

# Make it executable 
chmod +x  

#Run it  

Hello World! in PHP .

echo ' 
print("Hello, World\n");  
'> hello.php 

# Make it executable 
chmod +x hello.php  

# Run it. 

In conclusion, shell scripting is useful in a variety of ways, and knowing how to write efficient shell scripting, gets you a step ahead.

Prince Adeyemi

As the Editor-in-Chief, a Cybersecurity Professional and experienced software engineer, he writes tech news articles, and tutorials relating to software development, computer security, cybersecurity, and network security.

Prince takes great pride in delivering quality Las Vegas news, Henderson news, business reviews, celebrity news, sports, and government news releases to readers in Las Vegas and beyond.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *



Place Your Ad Here.

Claim it.


Follow Us.