Basic Shell scripting in Linux

Basic Shell scripting in Linux

Scripts is a collection of commands that are stored in a file. Shell reads this file and acts on the commands as if they were typed in the keyboard. In this article, we will learn how to write Shell Scripts.

Basic step of shell script

[root@linuxhelp~]# vimfile1.sh
   Echo “ this is my first script” 

No need of giving .sh at last of file, it will automatically recognize the script. So ./filename is alone is enough to run the script.

[root@linuxhelp~]# sh file1.sh
this is my first script

If it not executed we need to change the mode to be executed as follows,

[root@linuxhelp~]# chmod 744 file1.sh

Next, we can give input to the script file,

[root@linuxhelp~]# vim file2.sh
Echo “ hello $USER” 
[root@linuxhelp~]# sh file2.sh
hello user1

So it will take the logged in user of system as default.

We can run many commands at a time,

[root@linuxhelp~]# vim file3.sh
echo “ hello $USER” 
date
ps
[root@linuxhelp~]# sh file3.sh
Hello user1
Fri Apr 1 04:43:23 IST 2016
PID   TTY    TIME  CMD
6284  pts/0  00.00.00 su
6268  pts/0  00.00.00 sh
8685  pts/0  00.00.00 ps

To view the interactive mode

[root@linuxhelp~]# vim file4.sh
Echo “ tell your name” 
Read a 
Echo “ hello $a”  
[root@linuxhelp~]# sh file4.sh
Tell your name
Linux
Hello Linux

These are the basic steps to know about shell scripts, and next we go for the simple coding programs

To make the text bold in the script file

[root@linuxhelp~]# vim bold.sh
echo -e " hello world e[1mlinuxhelp" 
[root@linuxhelp~]# sh bold.sh
Hello world linuxhelp

To underline the contents

[root@linuxhelp~]# vim underline.sh
echo -e " hello world e[4mlinuxhelp" 
[root@linuxhelp~]# sh underline.sh
Hello world linuxhelp

To color the contents inside the file

You can choose the following coding, to color the contents inside the file.

[root@linuxhelp~]# vim colors.sh
echo -e " hello world e[31m linuxhelp" 
echo -e " hello world e[32m linuxhelp" 
[root@linuxhelp~]# sh colors.sh
Hello world linuxhelp
Hello world linuxhelp

To create the file with that of encrypting option

We need to use a package called ‘ pinentry-gui

If not installed in Linux systems, you can done by using yum command,

Here you have to create a script and the contents are shown below,

[root@linuxhelp~]# vim encrypt.sh
echo " hello" 
echo " Encrpting the given file" 
echo " Enter the filename:" 
read a 
gpg -c $a
echo " given file is encrypted" 
echo " original file is removed" 
rm -rf $a

Now to create a file called testfile.txt with some contents, and then run the script,

[root@linuxhelp~]# sh encrypt.sh  
Hello
Encrypting the given file
Enter the filename:
testfile.txt
given file is encrypted
original file is removed

So here while running this command it will ask for the encryption key to generate, and then it will create the encrypted file.
There the original file is removed and the new file with .gpg extension is saved.

When you open the file the contents will be encrypted.

To decrypt the file

To decrypt the file you can done by script or done directly as,

[root@linuxhelp~]# gpg &ndash d testfile.txt.gpg >  test

It will ask for the decryption key. Once the key is given it will decrypted.

To Draw a Special Pattern

The following script is used,

[root@linuxhelp~]# vim pattern.sh

MAX_NO=0
echo -n " Enter Number between (4 to 8) : " 
read MAX_NO
if ![ $MAX_NO -ge 4 -a $MAX_NO -le 8 ]   then
echo " enter number between 4 to 8,sorry" 
exit 1
fi
clear
for (( i=1  i< =MAX_NO  i++ )) do for (( s=MAX_NO  s> =i  s-- ))
do
echo -n "  " 
done
for (( j=1  j< =i  j++ )) do echo -n "  ."  done echo " "  done #****************************************** for (( i=MAX_NO  i> =1  i-- ))
do
for (( s=i  s< =MAX_NO  s++ ))
do
echo -n "  " 
done
for (( j=1  j< =i   j++ ))
do
echo -n "  ." 
done
echo " " 
done

Output

[root@linuxhelp~]# chmod 755 Pattern.sh
[root@linuxhelp~]# ./Pattern.sh
Enter Number between (5 to 9) : 6
       .
      . .
     . . .
    . . . .
   . . . . .
  . . . . . .
  . . . . . .
   . . . . .
    . . . .
     . . .
      . .
       .

To Check Disk Space and Send an Email Alert

Run the following command to get an email, when disk use in partition PART is bigger than Maximum allowed.

MAX=95
EMAIL=test@domain.com
PART=sda1
USE=`df -h |grep $PART | awk ' { print $5 }'  | cut -d' %'  -f1`
if [ $USE -gt $MAX ]  then
echo " Percent used: $USE"  | mail -s " Running out of disk space"  $EMAIL
fi
Tag : Shell Script
FAQ
Q
How to accomplish any task via shell script in Linux?
A
You can accomplish any tasks using via "shell script" where at the dollar ($) prompt and vice versa in Linux.
Q
Can you give me the "shell script" to determine if a directory exists in Linux?
A
You can use the following "shell scripting" to determine if a directory exists in Linux.
" $ env
HISTFILE=/home/lfl/.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=livefirelabs.com
USER=lfl
MAIL=/var/spool/mail/lfl
HOME=/home/lfl
HISTSIZE=1000 "
Q
What is Shell Scripting?
A
Shell scripting is only arrangement or grouping of UNIX directions written in a plain content record. Rather than determining one occupation/order at once, in shell scripting, we give a rundown of UNIX directions like a plan for the day in a record to execute it.
Q
Shell programs are stored in which file format (or) extension in Linux?
A
Shell programs are stored in a ".sh" file format (or) extension in Linux.
Q
What are the different types of Shells available in Unix and Linux?
A
There are mainly 4 important types of shells that are widely used in Unix and Linux.
1. Bourne Shell (sh)
2. C Shell (csh)
3. Korn Shell (ksh)
4. Bourne Again Shell (bash)