Subscribe to Get Free Material Updates!
Visit my new blog WebData Scraping - Web Scraping Service provider company in India.
Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

UNIX Shell Script Programming


When group of command is to be executed regularly, they should be stored in a file. This file is called Shell Script It is not mandatory to have .sh extension Vi editor is used to create shell script.  You can use any Unix based Operating System to run this script or you can also use UWIN (Unix like shell developed by Wipro)  program to execute Shell Script on Windows. Shell script is very sophisticated scripting language because white space also plays an important role in syntax grammar while in other languages like C and C++ or Java ignores white spaces.

For the practical purpose I am sharing this quick handy presentation which helps you to refreshing shell script syntax rules and various commands. I published this article to help students in MCA Unix Shell Script Programming. Download link is given at the end of this post. Also share this with all  GTU MCA students.

Unix script


Shell Script

You can Download This Presentation From This Link.

Write a script that behaves both in interactive and non-interactive mode

Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only.

flag=1
if [ $# -ne 0 ];then # IF USER PROVIDE ARGUMENTS
 echo $* >temp
 flag=1
else    # IF USER NOT PROVIDE ARGUMENTS
 ans=`ls *.c`
 if [ "" == "$ans" ];then
  echo "THERE ARE NO C FILE"
  flag=0
 else
  ls *.c>temp
  flag=1
 fi
fi
if [ $flag -eq 1 ];then
 for filename in `cat temp`
 do
  if [ -f $filename ];then
   echo "\t\t\t========="
   echo "\t\t\t$filename"
   echo "\t\t\t========="
   echo " "
   sed -n -e "1,10 p" $filename
   rm -i $filename
  fi
 done
 rm temp
fi

Script To Calculate Employee Salary

clear
echo "        GROSS SALARY CALCULATOR"
echo "        ======================="
echo "ENTER BASIC SALARY:"
read basic 
hra=`expr 10 \* $basic / 100`
da=`expr 15 \* $basic / 100`
gs=`expr $basic + $hra + $da`
# gs=`expr $gs + $da`
echo "GROSS SALARY IS $gs"
Output: 

Write a script which reads a text file and output the following Count of character, words and lines. File in reverse. Frequency of particular word in the file. Lower case letter in place of upper case letter.

clear
echo "Enter File Name :="
read filename
echo "1. Number Of Character"
echo "2. Number Of Words"
echo "3. Number Of Lines"
echo "4. File In Reverse"
echo "5. Fequency Of Particular Word"
echo "6. Convert Upper Case to Lower Case"
echo "Enter Your Choice :="
read ch
case $ch in
 1)
  echo "Total Number Of Characters are : " `cat $filename |wc -c` 
  ;;
 2)
  echo "Total Number Of Words are : " `cat $filename |wc -w` 
  ;;
 3)
  echo "Total Number Of Lines are : " `cat $filename |wc -l` 
  ;;
 4) 
  revstr=""
  while read -n1 ch;
  do
   revstr="$ch$revstr"
  done<$filename
  echo "File In Reverse Order : "
  echo $revstr
  ;;
 5)
  clear
  echo "Enter Word To Search : "
  read search
  filename="veer.txt"
  str=`grep -i "$search" $filename`
  `echo $str | tr '[A-Z]' '[a-z]'>file.txt`
  i=1
  count=0
  len=`echo $str | wc -c`
  echo "length is $len"
  search=`echo $search|tr '[A-Z]' '[a-z]'`
  echo "SEARCH WORD:$search"
  i=1
  while read -n1 ch;
  do
   if [ "$ch" == " " ] || [ $i -eq $len ];then
    if [ "$word" == "$search" ];then
     count=`expr $count + 1`
    fi
    word=""
   else
    word="$word$ch"
   fi
   i=`expr $i + 1`
  donefile.txt`
  `cat file.txt > $filename`
  echo "ALL CHARACTER CONVERTED TO LOWER CASE"
  ;;
 *)
  echo "Enter poper Values "
esac

Output: 

Write a script to make following file and directory management operations menu based


  1. Display current directory
  2. List directory
  3. Make directory
  4. Change directory
  5. Copy a file
  6. Rename a file
  7. Delete a file
  8. Edit a file
clear
echo " 1 : Display Current Directory"
echo " 2 : List directory"
echo " 3 : Make Directory"
echo " 4 : Change Directory"
echo " 5 : Copy a file"
echo " 6 : Rename a file"
echo " 7 : Delete a file"
echo " 8 : Edit a file"
echo " ENTER CHOICE : ";
read ch
if [ $ch -eq 1 ];then
 echo "YOUR CHOICE IS TO DISPLAY CURRENT DIRECTORY"
 pwd=`pwd`
 echo "YOUR CURRENT DIRECTORY IS : $pwd"
elif [ $ch -eq 2 ];then
 echo "YOUR CHOICE IS TO LIST THE FILE IN DIRECTORY"
 echo "FILE AND DIRECTORIES IN CURRENT DIRECTORY IS "
 echo `ls`
elif [ $ch -eq 3 ];then
 echo "YOUR CHOICE IS TO CREATE DIRECTORY"
 echo "Enter Name Of Directory:";
 read dir
 `mkdir $dir`
 echo "Directory Created successfully";
elif [ $ch -eq 4 ];then
 echo "YOUR CHOICE IS TO CHANGE DIRECTORY"
 echo "ENTER DIRECTORY TO CHANGE"
 read dir1
 cd $dir1
 echo "CHANGED SUCCESS "`pwd`
elif [ $ch -eq 5 ];then
 echo "Enter file to COPY"
 read file
 echo "Enter Directory to which file is to be copied"
 read dir
 cp $file $dir
elif [ $ch -eq 6 ];then
 echo "YOUR CHOICE IS TO RENAME A FILE"
 echo "ENTER FILE TO RENAME"
 read file
 echo "ENTER NEW NAME"
 read name
 mv $file $name
elif [ $ch -eq 7 ];then
 echo "YOUR CHOICE IS TO DELETE A FILE"
 echo "ENTER FILE TO DELETE"
 read file
 rm -i $file
elif [ $ch -eq 8 ];then
 echo "YOUR CHOICE IS TO EDIT A FILE"
 echo "ENTER FILENAME TO EDIT "
 read file
 echo "ENTER NEW DATA TO WRITE"
 read newdata
 echo $newdata > $file
else
 echo "ENTER CORRECT OPTION"
fi


Output: 

Write a script for generating a mark sheet after reading data from a file. File contains student roll no, name , marks of three subjects

clear
len=`cat student.txt | wc -l`
i=1
echo "\n\n\tSTUDENT MARKSHEET"
echo "\t=================\n"
echo "NAME \t \t TOTAL \t \t PERCENTAGE \t GRADE "
echo "====================================================="
while [ $i -le $len ]
do
 record=`head -n $i student.txt | tail -n 1`
 total=0
 for (( j=2 ; $j < 5 ; j=`expr $j + 1` ))
 do
  marks=`echo $record | cut -d " " -f $j`
  total=`expr $total + $marks`
 done
 name=`echo $record | cut -d " " -f 1`
 per=`expr $total / 3`
if [ $per -ge 85 ] && [ $per -le 100 ] ; then
    grade="AA"
 elif [ $per -ge 75 ] && [ $per -le 84 ] ; then
    grade="AB"
 elif [ $per -ge 65 ] && [ $per -le 74 ] ; then
    grade="BB"
 elif [ $per -ge 55 ] && [ $per -le 64 ] ; then
    grade="BC"
 elif [ $per -ge 45 ] && [ $per -le 54 ] ; then
    grade="CC"
 elif [ $per -ge 0 ] && [ $per -le 44 ] ; then
    grade="FAIL"
 else
    grade="-"
 fi

 echo "$name \t \t $total \t \t $per % \t \t $grade"
 i=`expr $i + 1`
done


Output: 

Write a script to implement the following commands Tree of DOS similar to which command of UNIX

echo "Enter path Name :="
read path
name=`find  "$path"  -print0 | xargs -0`
for i in $name
do
 if [ -d $i ];then
  echo $i>file.txt
  count=`awk 'NF{print NF-1}' FS="/" file.txt`
  format="|__"
  temp=`expr $count + 1`
  i=`echo $i|cut -d "/" -f $temp`
  for (( j=0 ; $j < $count ; j=`expr $j + 1` ))
  do
   format="|    "$format
  done
  echo "$format$i"
 fi
done
Output: 

Write a script to display the directory in the descending order of the size of each file.

clear
echo "ENTER DIR"
read dir
`echo ls -lS $dir` > file.txt
len=`cat file.txt | wc -l`
i=2
echo "SIZE          FILENAME"
echo "====       ==============="
while [ $i -le $len ]
do
  record=`ls -lS $dir | head -n $i | tail -n 1`
  # record=`cat file.txt | head -n $i | tail -n 1`
  filename=`echo $record | cut -d " " -f 9`
  size=` echo $record | cut -d " " -f 5`
  echo "$size        $filename" 
  i=`expr $i + 1`
done 

Output:

Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.

clear
HH=`date +%H`
time=`date +"%S %p"`
if [ $HH -ge 12 ];then
 HH=`expr $HH % 12`
 if [ $HH -lt 5 ];then
  msg="GOOD AFTERNOON"
 elif [ $HH -ge 5 ]  &&  [ $HH -lt 9 ];then
  msg="GOOD EVENING"
 else
  msg="GOOD NIGHT"
 fi
 echo "$msg ,CURRENT TIME $HH:$time"
 exit 1
else
 if [ $HH -lt 5 ];then
  msg="GOOD NIGHT"
 else
  msg="GOOD MORNING"
 fi
 echo "$msg ,CURRENT TIME $HH:$time"
 exit 1
fi

Output: 

Write a script to display the name of all executable files in the given directory.

clear
echo "Enter Directory Name:"
read dir
ls $dir>tempfile.txt
count=0
if [ -d $dir ];then
 for filename in `cat tempfile.txt`
 do
  if [ -x $filename ];then
   echo "$filename"
   count=`expr $count + 1`
  fi
 done
fi
echo "Total Executable Files Are $count"
rm tempfile.txt

Output: 

Write a script to display the name of those files (in the given Directory) which are having multiple links.

clear
echo "Enter Directory Name :="
read dir
len=`ls -l $dir | wc -l`
i=2
echo "File With Multiple Link are : "
echo " "
while [ $i -le $len ]
do
 record=`ls -l $dir | head -n $i | tail -n 1`
 filename=`echo $record | cut -d " " -f 9`
 link=`echo $record | cut -d " " -f 2`
 if [ $link -gt 1 ];then
  echo "$filename = $link"
 fi
 i=`expr $i + 1`
done

Output: 

Write a script to delete zero sized files from a given directory (and all its sub-directories).

clear
`echo ls`>filelist
for filename in `cat filelist`
do
 if [ ! -d $filename ];then
  size=`ls -s $filename`
  size=`echo $size|cut -d " " -f 1`
   if [ $size -eq 0 ];then 
   echo "Want to Remove $filename:"
   rm -i $filename
  fi
 fi
done

Output:

Write a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory

clear
echo "Enter Directory 1:"
read dir1
echo "Enter Directory 2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2 ]; then
 echo "dir1 not exist"
 exit 1
fi
`echo ls $dir1`>dir1.txt
`echo ls $dir2`>dir2.txt
echo "==============="
totalfile1=`wc -w dir1.txt|cut -c 8-9`  # HOME
totalfile2=`wc -w dir2.txt|cut -c 8-9`  # HOME
# totalfile1=`wc -w dir1.txt|cut -c 1-2`    # COLLEGE
# totalfile2=`wc -w dir2.txt|cut -c 1-2` # COLLEGE 
echo "totalfile:$totalfile1"
echo "totalfile:$totalfile2"
mkdir NEW_DIR
i=$totalfile1
while [ $i -ge 1 ] 
do
 file1=`tail -n $i dir1.txt | head -n 1`
 i=`expr $i - 1`
 j=$totalfile2
 while [ $j -ge 1 ] 
 do            
  file2=`tail -n $j dir2.txt | head -n 1`
  j=`expr $j - 1`        
  if [ "$file1" == "$file2" ];then
   str1=`ls -l $dir1/$file1 | cut -d " " -c 50-55`
   str2=`ls -l $dir2/$file2 | cut -d " " -c 50-55`
   # str1=`ls -l 3.sh|cut -d " " -f 7`     # str2=`ls -l 3a.sh|cut -d " " -f 7`  
   hh1=`echo $str1 | cut -c 1-2`
   ss1=`echo $str1 | cut -c 4-5`
   hh2=`echo $str2 | cut -c 1-2`
   ss2=`echo $str2 | cut -c 4-5`

   # echo "hh1:$hh1 and ss1:$ss1";
   # echo "hh2:$hh2 and ss2:$ss2";
   if [ $hh1 -eq $hh2 ];then
    if [ $ss1 -le $ss2 ];then
     cp $dir2/$file2 NEW_DIR
    else
     cp $dir1/$file1 NEW_DIR
    fi
   else
    if [ $hh1 -le $hh2 ];then
     cp $dir2/$file2 NEW_DIR
    else
     cp $dir1/$file1 NEW_DIR
    fi
   fi
  fi
 done
done


Output:

Write a script to find the global complete path for any file

clear
echo "Enter file name : "
read filename
str=`find . -name $filename`
if [ "$str" == "" ];then
    echo "File not found"
    exit 1
fi
path=`pwd`
len=`echo $str | wc -c`
str=`echo $str | cut -d "." -f 2-3`
echo "Full path of file is $path$str"


Output: 

Related Posts Plugin for WordPress, Blogger...

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes