PR
PR
RAHUL PATEL 1
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
2
RAHUL PATEL
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
5. Display first 5 characters from file temp and demo. head -5c
temp demo
6. Display first 5 lines from file temp and demo without header.
head -5q temp demo
7. Display first 5 characters from file temp and demo without
header. head -5cq temp demo
8. Display last 8 characters from line 5 from file temp.
head -5 temp|tail -8c
DAVE PRIT
9. Display lines 4-10 from file temp.
head temp|tail -7
10. Display last 6 characters of line 10 from file temp. head
temp|tail -6c
2.Tail
1. Display last 10 lines from file temp. tail temp
2. Display last 4 lines from file temp. tail -4 temp
3. Display last 4 characters from file temp. tail -4c temp
4. Display last 4 lines from file temp and demo. tail –n 4
temp
5. Display 10th line from file temp. tail –n 4 temp|head -1
6. Display file temp and demo without header. tail –q
temp demo
7. Display last 4 lines of file temp and demo without
header. tail
–q –n 4 temp demo
8. Display first 3 lines from file temp. tail –n 13
temp|head -3
9. Display 10th line first 6 characters from file temp.
RAHUL PATEL 3
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
3.Cut
1) Display first four characters of each line of file temp.
cut –c-4 temp
2)Display 1st character to 5th character. sh-
3.2$ cut -c1-5 file1
3) Display 4th character in first 5 line only.
sh-3.2$ head -n 5 file1 | cut -c 4 4) Display 1st
, 3rd,5th character in file.
sh-3.2$ cut -c1,3,5 file2
5) Display 3rd character of 3rd line in file.
sh-3.2$ head -3 file2 | tail -1 | cut -c3 6)
Display only 2 delimiters in file. sh-3.2$
cut -d" " -f1-2 file1
7) Display 1st and 3rd delimiters in file. sh-3.2$ cut -d" " f1,3
file1
8) Display 3rd character to last character in file. sh-3.2$ cut -
c3- file1
9) Display last character of all line in file. sh-3.2$ cut -c1- file1
| rev | cut -c1 10)Display
last character of word in file. sh-3.2$ cut -d"
" -f1 file1 | rev | cut -c1 | rev
4.Paste
1) Display verticaly data. sh-3.2$ paste number state capital
2) Display data with one “|” symbol. sh-3.2$ paste
-d"|" number state capital 3)
4
RAHUL PATEL
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
DAVE PRIT
10) Display line into the 2 column with delimiter. sh3.2$ paste
-d"|" - -< number
OUTPUT:-
Enter first number:9 Enter first number:10 Sum of two digits:19
RAHUL PATEL 5
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
OUTPUT:-
Enter the length of rectangle:
10 Enter the breadth
of rectangle: 60
Area:600
6
RAHUL PATEL
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
OUTPUT:-
Enter the length of rectangle:
10 Enter the breadth of
rectangle: 60
Area:600
RAHUL PATEL 7
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
8. Write shell script for swapping two number without using
third variable.
echo "Enter first number"; read a
echo "Enter second number"; read b
a=`expr $a + $b` b=`expr $a - $b`
8
RAHUL PATEL
UNIX AND SHELL PROGRAMMING
9. Write shell script to prepare result sheet (marks of 5
subjects entered through keyboard and rules for
division as per following) 1. above 70% distinction
2. 60-69 first class
3. 50-59 second class
4. 40-49 pass class
5. <40 fail
ANS. echo "Enter WFS marks: "; read wfs echo "Enter ASP.NET
marks: "; read asp echo
OUTPUT:-
Enter WFS marks: 58 Enter ASP.NET marks: 50 Enter UNIX marks: 50
Enter AWD marks: 50 Enter NT marks: 50
Total: 258
Percentage:51 Second Class
9
RAHUL PATEL
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
OUTPUT:-
Enter file:
temp
Words in file:24
Characters in file:151
Lines in file:13
11. Write shell script to find out largest number from any three number.
ANS. echo "Enter first number: "; read a echo "Enter second number: ";
read b echo "Enter third number: "; read b if test $a -gt $b && test $a -gt
$c then echo
$a elif test $b -gt $c && test $b -gt $a then echo
"$b is largest number" else
echo "$c is largest number" fi
OUTPUT:-
Enter first number:
1 Enter first
number:
2
Enter first number: 3 3
is largest number
RAHUL PATEL 10
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
12. Write shell script to calculate simple interest through command line
arguments.
ANS. principal="$1" rate="$2" time="$3"
interest=$(( $principal * $rate * $time / 100 ))
echo "Principal: $principal" echo "Rate of
Interest: $rate" echo "Time (in years): $time"
echo "Simple Interest: $interest"
OUTPUT:-
Principal: 1200
Rate of Interest: 10
Time (in years): 2
Simple Interest: 240
13. Write shell script to check inputted file exist or not if exist then check
read, write and execute permission of that file.
ANS. echo "Enter a file"; read file if
[ -e $file ] then echo exist
if [ -r $file ]
then
else echo do not have read permission
fi if [ -w $file ] then
echo have write permission else
echo do not have write permission
fi
if [ -x $file ] then
echo have execute permission else
echo do not have execute
permission fi else
echo not exist
RAHUL PATEL 11
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
fi
OUTPUT:-
Enter a file oddeven exist have
read permission have write permission
do not have
execute permission
14.
Write shell script to reverse each word of string. ANS.
echo "Enter any string: "; read str echo $str | rev
OUTPUT:-
Enter any string: my name
is sanal lanas
si eman ym
15.
Write shell script to perform mathematical operations (use
command prompt for input). ANS. a=$1 b=$2
add=$(( $a + $b )) sub=$((
$a - $b )) mul=$((
$a * $b )) div=$((
$a / $b ))
RAHUL PATEL 12
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
OUTPUT:- ADD
= 120
SUB = 80
MUL = 2000
DIV = 5
16.
Write shell script for inputted string is palindrome or not. ANS.
echo "Enter any string"; read str echo $str>temp
reverse="$(rev temp)" echo $reverse if [ $str = $reverse ] then echo
"Palindrome" else
echo "not" fi
OUTPUT:-
Enter any string sanal
lanas not
17.
Write shell script to take month number as input and print month
name.
ANS. echo "Enter any number: "; read a case
$a in "1")echo "January"
;;
"2")echo "February"
;;
"3")echo "March"
;;
RAHUL PATEL 13
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
"4")echo "April"
;;
"5")echo "May"
;;
"6")echo "June"
;;
*) esac
OUTPUT:-
Enter any number:
5
May
18. Write shell script to print the table of any inputted number (use
command prompt for input.
ANS. echo "Enter a number:"; read a c=1
while [ $c -le 10 ] do
echo "$a * $c ="`expr $a \* $c`
c=`expr $c + 1` done
OUTPUT:-
Enter a number:
9
9 * 1 =9
9 * 2 =18
9 * 3 =27
9 * 4 =36
9 * 5 =45
RAHUL PATEL 14
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
9 * 6 =54
9 * 7 =63
9 * 8 =72
9 * 9 =81
9 * 10 =90
$file
;;
"4")
echo "Enter the file you want to copy:"; read copyfile if
[ -e $copyfile ] then
echo "Enter new file name:"; read newfile cp
$copyfile $newfile
echo "File copied" else echo
"File $copyfile not found" fi
;;
"5")echo "Enter the file you want to delete"; read rmfile if
[ -e $rmfile ] then rm $rmfile echo
"File deleted" else
echo "File not found" fi
;;
"6")echo "Enter the file you want to rename:"; read rename
if [ -e $rename ] then
echo "Enter new name:"; read newname
mv $rename $newname
else
echo "File $rename does not exist"
fi
;;
"7")echo "Currently logged in user:"
Who
;;
"8")cal
;;
RAHUL PATEL 16
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
"9")a=1
echo "Exit"
;;
*)echo "Invalid Choice" esac
done
OUTPUT:- 1 pwd
2 ls
3 mkdir
4 copy
5 delete
6 rename
7 logged in
8 date and calendar
9 exit
Enter your choice
2
area concat file1 fileexist largest marksheet menu month odd_even
palindrome salary sanal sum swap temp
1 pwd
2 ls
3 mkdir
4 copy
5 delete
6 rename
7 logged in
8 date and calendar
9 exit
Enter your choice
RAHUL PATEL 17
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
1
/home/vishakha
1 pwd
2 ls 3 mkdir
4 copy
5 delete
6 rename
7 logged in
8 date and calendar
9 exit
Enter your choice
9
Exit
20.
Write shell script for inputted number is palindrome or not ANS.
echo "Enter any string"; read str echo $str>temp
reverse="$(rev temp)" echo $reverse if [ $str = $reverse ] then echo
"Palindrome" else
echo "not" fi
OUTPUT:-
Enter any string
151
151
Palindrome
RAHUL PATEL 18
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
21.
Write shell script to count files, directories and symbolic link files
from directory.
ANS. file=`ls -1` fc=0 dc=0
lc=0 for i in $file do
if [ -f $i ] then
fc=`expr
$fc + 1`
elif [ -d $i ] then
dc=`expr $dc + 1` elif [ -L
$i ] then
lc=`expr $lc + 1` else
echo "Directory is empty"
fi done
echo "Regular files: $fc" echo
"Directories: $dc"
echo "Symbolic Link Files: $lc"
OUTPUT:-
Regular files: 15
Directories: 1
Symbolic Link Files: 0
RAHUL PATEL 19
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
snaps facebook
6. -v : This prints out all the lines that do not matches the
pattern grep -v facebook greps
unix shell shell SHELL
UNIX whatsapp
WHATSAPP
instagram whatsapp
SNAPCHAT FACEBOOK
snapchat
RAHUL PATEL 20
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
snaps facebook
RAHUL PATEL 21
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
stars
galaxy
RAHUL PATEL 22
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
24.
Write any 10 commands of awk.
ANS. 1. Print all data
awk '{print}' awks
johns CEO 10000 julie
manager 100000
smith clerk 6000
RAHUL PATEL 23
UNIX AND SHELL PROGRAMMING TYBCA SEM 5
RAHUL PATEL 24