While learning shell scripting I developed few scripts. Wanted to share it with everyone through
this post.
During our administration we do require some level of scripting knowledge. Common scripts
are mostly related to builds, deployments and system admin related tasks . Like all programming
languages it has loops, variables, functions and much more.
The programs below are very basic, but it gives an understanding of basic scripting. Hope
people find it useful.
#! /bin/ksh
#
#
# Add two numbers.
#
#
#
clear
printf “Read A n”
read a
printf “Read B n”
read b
let c=a+b
printf “the sum is $c n”
——————
# /bin/ksh
#
#
# Check a file or directory
#
#
#
printf “Enter a name: ”
read name
if [ -f $name ]
then
printf “Ordinary filen ”
fi
if [ -d $name ]
then
printf ” Directory n”
fi
——————
# /bin/ksh
#
#
# Print the total number of Files and Directories in the present directory
#
#
#
files=0
dir=0
for x in `ls -lrt | tr -s ” ” | cut -d ” ” -f 9`
do
if [ -f $x ]
then let files++
fi
if [ -d $x ]
then let dir++
fi
done
printf “Number of files $files n”
printf “Number of directories $dir n”
———————
# /bin/ksh
#
#
# Find the size of the total contents of the current directory
#
#
#
size=0
for x in `ls -lrt | tr -s ” ” | cut -d ” ” -f 5`
do
let size=$size+$x
done
printf “Size is $size”
———————————-
# /bin/ksh
#
#
# Check if two files are identical or not
#
#
#
printf “Enter two filenames:n ”
read a b
diff $a $b 2> /dev/null
z = $?
if [ $z -eq 0 ]
then
printf “Identical n”
elif [ $z -eq 1 ]
then
printf ” Not Identical n”
else
printf “Error in file namesn”
fi
————————————-
# /bin/ksh
#
#
# Create records
#
#
#
m=1
n=5
clear
while read x
do
a=`echo $x | cut -f1 -d “:”`
b=`echo $x | cut -f2 -d “:”`
tput cup $m $n; echo -e “$at$b”
let m++
sleep 1
done < records
———————————
#! /bin/ksh
m=1
n=5
clear
for x in `cat records`
do
a=`echo $x | cut -f1 -d “:”`
b=`echo $x | cut -f2 -d “:”`
tput cup $m $n; echo -e “$at$b”
let m++
sleep 1
done
————————————
# /bin/ksh
#
#
# Enter Employee Records
#
#
#
ans=”y”
clear
while [ $ans = “y” ]
do
tput cup 10 10; printf “enter number”; read no
tput cup 10 14; printf “enter name”; read name
echo $no:$name >> records
tput cup 10 16; printf “read answer”; read ans
done
——————————–
# /bin/ksh
#
#
# While with Case
#
#
#
while true
do
clear
tput cup 5 10;echo “1, add”
tput cup 7 10;echo “2, del”
tput cup 9 10;echo “3, mod”
tput cup 11 10;echo “4, exit”
tput cup 13 10;echo -n “choice “; read ch
case $ch in
1 ) echo “add” ;;
2 ) echo “del” ;;
3 ) echo “mod” ;;
4 ) break ;;
esac
sleep 1
done
——————————–
# /bin/ksh
#
#
# Delete a line in a record
#
#
#
ans=”y”
clear
while [ $ans = “y” ]
do
cat records > temp
printf “Enter line to be deleted”
read line
i=0
for x in `cat temp`
do
printf “inside for loop”
a=`echo $x | cut -f1 -d “:”`
b=`echo $x | cut -f2 -d “:”`
echo $line
if [ $a -ne $line ]
then
if [ i -eq 0 ]
then
echo $a:$b > records
let i++
else
echo $a:$b >> records
fi
fi
done
printf “Do you want to continue ? y/n “; read ans
done
————–
# /bin/ksh
#
#
# Creating Menu options
#
#
#
while true
do
clear
tput cup 5 10;echo “1, add”
tput cup 7 10;echo “2, del”
tput cup 9 10;echo “3, mod”
tput cup 11 10;echo “4, exit”
tput cup 13 10;echo -n “choice”; read ch
case $ch in
1) echo “add” ;;
2) echo “del” ;;
3) echo “mod” ;;
4) break ;;
esac
sleep 1
done
—————
let a=$1+$2
echo $a
shift
let c=$1+$2
echo $0
echo $c
————-
disp()
{
local m
k=1
while [ $k -le 10 ]
do
echo $k
let k++
done
return 123 }
clear
disp
r=$?
printf ” $r n”
printf ” $k n”
printf ” $m n”
——————
checkSize(){
printf “file one : $1 n”
printf “file two : $2 n”
a=`ls -l $1 | tr -s ‘ ‘ | cut -d ” ” -f 5`
b=`ls -l $2 | tr -s ‘ ‘ | cut -d ” ” -f 5`
printf “size of $1 is $a n”
printf “size of $2 is $b n”
if [ $a -gt $b ]
then
printf “$1 greater that $2 n”
else
printf “$2 greater that $1 n”
fi
}
———————-
k=0
while [ $k -le 5 ]
do
printf “Enter value $k n”
read val[$k]
let k++
done
k=0
while [ $k -le 5 ]
do
echo ${val[$k]}
let k++
done
——————————–
countFilesAndDir(){
k=1
while [ $k -le 5 ]
do
printf “Enter file/directory name $k : ”
read val[$k]
let k++
done
k=1
file=0
dir=0
while [ $k -le 5 ]
do
if [ -f ${val[$k]} ]
then
let file++
fi
if [ -d ${val[$k]} ]
then
let dir++
fi
let k++
done
printf “Fies : $file Directories : $dir” }
printf “Program to count files and directories.. n”
countFilesAndDir
—————————
You can refer Sumitava Das for Unix and Shell Scripting