0% found this document useful (0 votes)
10 views

Unix_Shell_Script

Uploaded by

K. S Saiyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unix_Shell_Script

Uploaded by

K. S Saiyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

SAIYED AHMAD KAMRUDDIN

Unix(Shell-Script1) Page : 1
Unix Shell Script
1)
date
ls –xp
who
pwd
2)
echo “Enter Name :\c” or echo –c “Enter Name :”
read name
echo “Enter Age :\c”
read age
echo Hello $name Good Morning
echo $name , You are $age years old.
3)
cat $2 $3 > $1
echo The contents of $2 is
cat $2
echo
echo The contents of $3 is
cat $3
echo
echo The contents of $1 is
cat $1
4)
echo “Which Film is not of Sanjay Lila
1. Black 2. Devdas 3. Guru 4. HDDCS “
Echo “Enter your choice :\c”
Read ch
If [ $ch -eq 3 ] ; then
Echo Right Answer
Elif [ $ch -eq 1 -o $ch –eq 2 –o $ch –eq 4 ] ; then
Echo Wrong Answer
Else
Echo Not one of Choices
Fi

7)
I=1
While [ $I -eq 1 ]
Do
Echo “1. List of Files \n2. Date & Time \n3. List Of Users \n4. Current Dir \n5. Exit”
Echo “\nEnter Your choice :\c”
Read ch
Case $ch in
1) ls –l ;;
2) date ;;
3) who;;
4) pwd;;
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 2
5) echo Bye Bye
i=0;;
*) echo Wrong Choice
esac
echo “\n\n”
done

8) List from literals


for I in 1 Raj 5 Jay 7 9
do
echo $I
done

To list all files


for i in *
do
Echo $i
Done

To check given file has read, write or execute permission or not . if file not exists
then display proper message.
Echo Enter file name
Read fn
if [ -e $fn ] ; then
if [ -w $fn ] ; then
echo File has write permission
fi
if [ -r $fn ] ; then
echo File has read permission
fi
if [ -x $fn ] ; then
echo File has execute permission
fi
else
echo File does not exists
fi

9) List from variables


for i in $HOME $PATH $SHELL
do
echo $i
done
10) To find sum of numbers given at command line args
s=0
for i in $*
do
s=`expr $s + $i`
done
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 3
echo The sum is $s

11)
I=1
While [ $I -eq 1 ]
Do
Echo “Enter Capital of Gujarat :”
Read s
S1=`echo $s | tr [a-z] [A-Z]`
If [ $s1 = “GANDHINAGAR” ] ; then
Echo “Correct”
I=0
Else
Echo Wrong Answer
Fi
Done
12)
echo The 1st arg is $1
echo The 2nd arg is $2
echo Filename is $0
echo Total Asgs : $#
echo All args are : $*
for i in $*
do
echo $i
done

To Run : sh a12.sh Raj Patel and Jay Desai

13)
Possibility 1)
for I in $*
do
echo $I
done
To Take each args separately , Take 1) $* 2) $@ ( No diff)
Input : Raj Desai
Output : Raj
Desai
Input : “Raj Desai”
Output: Raj
Desai

Possibilty : 2)
for I in “$*”
do
echo $I
done
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 4

To run : sh a13.sh Raj Desai Jay Patel


Output : Raj Desai Jay Patel
To run : sh a13.sh “Raj Desai” “Jay Patel”
Output : Raj Desai Jay Patel

Possiblity 3)
for I in “$@”
do
echo $I
done

To take args given in double quote as one string use “$@”


To run : sh a13.sh “Raj Desai “ “Jay Patel”
Output : Raj Desai
Jay Pate

14) To check given filename (given using commandline) is a file , directory , or


something else
k=`ls –l | grep –w $1`
if [ $? -eq 1 ] ; then
echo Something else
exit
fi
k=`echo $k | cut –c1`
if [ $k = “d” ] ; then
echo It is a Directory
else
echo It is a File
fi

15) Input a word and display it in uppercase


echo “Enter word :\c”
read s
echo $s | tr [a-z] [A-Z]

Input a word and display it in Proper case.(Initcap)


Echo Enter word
Read s
S1=`echo $s | cut -c1 | tr “a-z” “A-Z”`
S2=`echo $s | cut -c2- | tr “A-Z” “a-z”`
Echo $s1$s2

16) To know the username given at command prompt is logged in or not


k=`who | grep –w $1`
if [ $? -eq 1 ] ; then
echo User $1 is not logged in
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 5
fi
17) Develop a regular expression for a valid identifier name
echo Enter Identifier Name
read s
k=`echo $s | grep “^[a-zA-Z_][a-zA-Z0-9_]*$”
if [ $? –eq 0 ] ; then
echo Valid
else
echo Invalid
fi
18) for Valid integer constant
k=`echo $s | egrep “^[+-]?[0-9][0-9]*$”
or
k=`echo $s | egrep “^[+-]?[0-9]+$”

19) for a valid decimal floating point constant


k=`echo $s | egrep “^[+-]?[0-9]*\.[0-9][0-9]*$”
or k=`echo $s | egrep “^[+-]?[0-9]*\.[0-9]+$”
20) Develop a regular expression for a Which will give the list of files which has 3
links.
a=`ls –l | tr –s “ “ | cut –d” “ –f2,9 | grep –w 3 | cut -d” “ -f2`
for i in $a
do
if [ -f $i ] ; then
echo $i
fi
done

21) To count Total alphabets from a file


cat test | tr -d [0-9\ ] | wc –c
or
cat test | tr -cd [a-zA-Z] | wc -c
22) To print all symbolic link of a given file
k=`ls –i $1 | cut -d” “ -f1`
find . –inum $k -print
or

k=`ls –i $1 | cut -d” “ -f1`


for I in *
do
if [ -f $I ] ; then
k1=`ls -i $1 | cut -d” “ -f1`
if [ $k -eq $k1 ] ; then
echo $I
fi
fi
done
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 6
or

k=`ls –i $1 | cut -d” “ -f1`


a=`ls –li | grep –w $k | tr –s “ “ | cut -d” “ -f10`
for i in $a
do
echo $I
done

23) Print total no. of files and dir of current dir


cf=`ls -l | grep -c “^-`
cd=`ls -l | grep -c “^d`
echo Total Files : $cf
echo Total Dirs : $cd

or
cf=0
cd=0

for i in *
do
if [ -f $i ] ; then
cf=`expr $cf + 1`
elif [ -d $i ] ; then
cd=`expr $cd + 1`
fi
done
echo Total files : $cf
echo Total Dirs : $cd

24) To count total no. of words from file


c=0
for I in `cat $1`
do
c=`expr $c + 1`
done
echo Total Words $c

25) Develop a script That will prompt when a particular user logs out
echo Enter User Name
read name
x=1
while [ $x –eq 1 ]
do
a=`cat who | grep -w $name`
if [ $? -eq 1 ] ; then
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 7
echo $name has been logged out
x=0
fi
done &
26)To kill all processes of a particular user
echo Enter User Name
read name
a=`ps -f | grep $name | tr -s “ “ | cut -d” “ -f2`
for I in $a
do
kill $I
done
or
echo Enter User Name
read name
a=`ps -f | grep $name | tr -s “ “ | cut -d” “ -f2`
kill $a
27) To print all palindrome words from file
for I in `cat $I`
do
x=`echo $I | wc -c`
x=`expr $l - 1`
r=””
while [ $x -ge 1 ]
do
ch=`echo $I | cut -c$x`
r=$r$ch
x=`expr $x - 1`
done
if [ $I = $r ] ; then
echo $I
fi
done

28)That acts as a RIGHT$ of qbasic.


echo Enter String
read s
echo Enter the value of n
read n
l=`echo $s | wc -c`
if [ $n -ge $l ] ; then
Exit
else
k=`expr $l - $n`
Echo “The resultant string is :\c”
Echo $s | cut -c$k-
fi
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 8
29) Create two files. 1) Student.txt (rno,name) 2) marks.txt (rno,m1,m2,m3)
(separated by |) Read two files and print report as follow. Name Marks1 Marks2
Marks3 Total
Cat student.txt | cut -d”|” -f2 > f1
Car marks.txt | cut -d”|” -f2-4 > f2
Paste -d”|” f1 f2 > f3

$ Open editor) Vi a1.sh

{
t=$2+$3+$4
printf “ %-10s %10d %10d %10d %10d\n” , $1,$2,$3,$4,t
}
To Run
$ awk -F”|” -f a1.sh f3

list last 10 days modified files

find . -mtime -10 -exec ls -lt {} \;

list modified files before 10 days

find . -mtime +10 -exec ls -lt {} \;

String :
1)To count total no.of vowels from given string
echo Enter string
read s
l=${#s}
c=0
for((i=0;i<l;i++))
do
k=${s:i:1}
case $k in
[aA]) c=$((c+1)) ;;
[eE]) c=$((c+1)) ;;
[iI]) c=$((c+1)) ;;
[oO]) c=$((c+1)) ;;
[uU]) c=$((c+1)) ;;
esac

done
echo "Total Vowels : $c"
SAIYED AHMAD KAMRUDDIN
Unix(Shell-Script1) Page : 9
2) To check two strings are equals or not
echo Enter string 1
read s1
echo Enter string 2
read s2
if [ $s1 = $s2 ] ; then
echo Equals
else
echo Not Equals
fi

3) To compare two files size wise


echo Enter file1
read a1
echo Enter file2
read a2
s1=`wc -c < $a1`
s2=`wc -c < $a2`
if [ $s1 -eq $s2 ] ; then
echo "$a1 = $a2"
elif [ $s1 -gt $s2 ] ; then
echo "$a1 > $a2 "
else
echo "$a1 < $a2"
fi

You might also like