0% found this document useful (0 votes)
15 views6 pages

Unix Prog

Uploaded by

Himani Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
15 views6 pages

Unix Prog

Uploaded by

Himani Verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

#1) Check if an input number is positive:

$ echo “Enter a number”

$ read num

$ if [ $num -gt 0 ]

$ then

$ echo “It is a positive number”

$ fi

#2) Check if an input number is positive or not:


$ echo “Enter a number”

$ read num

$ if [ $num -gt 0 ]

$ then

$ echo “It is a positive number”

$ else

$ echo “It is not a positive integer”

$ fi

#3) Check if an input number is positive, zero or negative:


$ echo “Enter a number”

$ read num

$ if [ $num -gt 0 ]

$ then

$ echo “It is a positive number”

$ elif [ $num -eq 0 ]

$ then
$ echo “num is equal to zero”

$ else

$ echo “It is not a positive integer”

$ fi
Write a Shell Script that takes a search string and file name from the terminal & displays the results.

read a
read b
word= `grep $a $b`
if test `echo $word |wc –c`-eq 1
then
echo “patern not found”
else
grep $a $b
fi

Write a Shell Script that takes pattern and file name as command line arguments and displays the results
appropriately i.e. pattern found/pattern not found.

if test $# -ne 2
then
echo “Invalid no. of arguments”
else
word=’grep $1 $2
if test ‘word | wc –c’ –eq1
then
echo ”pattern not found ”
else
grep $1 $2
echo “pattern found”
fi
fi

Write a Shell Script that accepts only three arguments from the command line. The first argument is the pattern
string, the second argument is the file name in which the pattern is to be searches and the third argument is the
file name in which the result is to be stored.

if test $# -ne3
then echo “Invalid no. of arguments”
else
grep $1 $2 | cat>$3
if test –s $3
then
echo “pattern found”
cat $3
else
echo “pattern not found”
fi

________________________________________
echo "STRING MANIPULATION PROGRAM"
echo "1. COMPARE STRING"
echo "2. JOINT TWO STRING"
echo "3. LENGTH OF STRING"
echo "4. OCCOURANCE OF CHARACTER"
echo "5. OCCOURANCE OF WORD"
echo "6. REVERSE STRING"
echo "ENTER CHOICE:"
read ch
case $ch in
1)
echo "ENTER FIRST STRING:"
read str1
echo "ENTER SECOND STRING:"
read str2
len1=`echo $str1 | wc -c`
len2=`echo $str2 | wc -c`
if [ $len1 -eq $len2 ];then
echo "BOTH STRINGS ARE OF SAME LENGTH"
elif [ $len1 -gt $len2 ];then
echo "$str1 is greater than $str2"
else
echo "$str2 is greater than $str1"
fi
;;
2)
echo "ENTER FIRST STRING:"
read str1
echo "ENTER SECOND STRING:"
read str2
str1="$str1$str2"
echo "COMBINED STRING : $str1"
;;
3)
echo "ENTER STRING:"
read str
len=`echo $str | wc -c`
echo "Length of string is $len"
;;
4)
echo "ENTER STRING:"
read str
echo "ENTER character of word to search:"
read search
len=`echo $str | wc -c`
i=1
while [ $i -lt $len ]
do
ch=`echo $str | cut -c $i`
echo $ch
i=`expr $i + 1`
done
;;
6)
echo "ENTER STRING:"
read str
echo "REVERSE STRING IS : "`echo $str | rev`
;;
*)
echo "ENTER PROPER CHOICE"
echo "NOT THERE"
;;
Esac

A shell script illustrating the use of the case construct.


echo” MENU\n
1. Long List of files\n2. Today’s Date\n3. Process status\n4.Users of the
System\n5. Display the present working directory and \n 6. Quit to Unix\
nEnter your option : \c”
read choice
case “$choice” in
1) Is –I ;;
2) date ;;
3) ps -ef;;
4) who -Hu;;
5) pwd ;;
6) exit;;
*) echo “Invalid Choice”
exit ;;
esac

WRITE A LINUX SHELL PROGRAM TO PERFORM


COMPARE TWO FILES

clear
echo -----------------------------------
echo '\tCompare two files'
echo -----------------------------------
echo Enter the name of the First file
read first
echo Enter the name of the Second file
read second
cmp $first $second

-z STRING the length of STRING is zero


STRING1 = STRING2 the strings are equal
STRING1 != STRING2 the strings are not equal
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2 FILE1 is older than FILE2
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-h FILE FILE exists and is a symbolic link (same as -L)
-k FILE FILE exists and has its sticky bit set
-L FILE FILE exists and is a symbolic link (same as -h)
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and read permission is granted
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE FILE exists and its set-user-ID bit is set
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute (or search) permission is granted

You might also like