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

PR

The document provides a comprehensive guide on UNIX and shell programming, detailing basic commands, file manipulation, and various shell script examples. It includes commands for managing files and directories, as well as scripts for arithmetic operations, file concatenation, and checking file permissions. Additionally, it covers user input handling and conditional statements in shell scripting.

Uploaded by

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

PR

The document provides a comprehensive guide on UNIX and shell programming, detailing basic commands, file manipulation, and various shell script examples. It includes commands for managing files and directories, as well as scripts for arithmetic operations, file concatenation, and checking file permissions. Additionally, it covers user input handling and conditional statements in shell scripting.

Uploaded by

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

UNIX AND SHELL PROGRAMMING TYBCA SEM 5

1. Unix basic commands( any ten).


ANS. 1. who : The „$ who‟ command displays all the users who have logged into
the system currently.
$ who
Output: harssh tty2 2017-07-18 09:32 (:0)

2. pwd : The „$pwd‟ command stands for „print working directory‟


and as the name says,it displays the directory in which we are
currently (directory is same as folder for Windows OS users).
$ pwd
Output: /home/harsh

3. mkdir : The „$ mkdir‟ stands for „make directory‟ and it creates a


new directory.
$ mkdir newfolder
$ cd newfolder
$ pwd
Output: /home/harssh/newfolder

4. rmdir : The „$ rmdir‟ command deletes any directory we want to


delete and you can remember it by its names „rmdir‟ which stands
for „remove directory‟.
$ rmdir newfolder

5. cd : The „$ cd‟ command stands for „change directory‟ and it


changes your current directory to the „newfolder‟ directory. $ cd
newfolder

RAHUL PATEL 1
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

6. ls : The „ls‟ command simply displays the contents of a directory.


$ ls

7. touch : The „$ touch‟ command creates a file(not directory) and


you can simple add an extension such as .txt after it to make it a
Text File.
$ touch example
$ ls

8. cp : This „$ cp „ command stands for „copy‟ and it simply


copy/paste the file wherever you want to.
$ cp copyfile newfile

9. mv : The „$ mv‟ command stands for „move‟ and it simply move


a file from a directory to another directory. $ mv renamefile
newname

10. rm : The „$ rm „ command for remove and the „-r‟ simply


recursively deletes file.
$ rm file1

2. Write minimum 10 command for each head, tail, cut, paste.


ANS. 1.Head 1. Display 10 lines from file temp.
head temp
2. Display first 5 lines from file temp. head -5 temp
3. Display first 5 lines from file temp and demo. head -5 temp
demo
4. Display first 5 characters from file temp. head -5c temp

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

tail –n 4 temp|head -6c


10. Display last 10 characters from file demo. tail -10c
demo

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

Display data and specified using “|,”. sh-


3.2$ paste -d":|" number state capital 4)
Display data verticaly without delimiter.
sh-3.2$ paste -s number state capital 5)
Display data verticaly with delimiter. sh-
3.2$ paste -s -d"|" number state capital 6)
Display files alternatively. sh-3.2$ paste d'\n'
number state capital
7) Display standered output in case of multiple files.
sh3.2$ cat number | paste -d"|" state - 8) Join all lines into
the single line.
sh-3.2$ paste -s number 9) Display
line into the 2 column. sh-3.2$
paste - -< number

DAVE PRIT
10) Display line into the 2 column with delimiter. sh3.2$ paste
-d"|" - -< number

3. Write shell script to make sum of digit. ANS. clear


echo "Enter first number:\c"; read a echo
"Enter first number:\c"; read b echo
"Sum of two digits: " expr $a
+ $b

OUTPUT:-
Enter first number:9 Enter first number:10 Sum of two digits:19

RAHUL PATEL 5
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

4. Write shell script to concatenate two files.


echo "Enter 1st Filename==> \c"; read f1 echo
"Enter 2nd Filename==> \c"; read f2 echo
"Enter New filename==> \c"; read f3 if [ -f
"$f1" ] then if [ -f "$f2" ] then cat $f1 $f2 >>
$f3 echo "Concatenation Successful" else
echo "File $f2 not found" fi else echo "File
$f1 not found" fi
OUTPUT:-
Enter 1st Filename==> Unix
Enter 2nd Filename==> Shell programming
Enter New filename==> Unix & Shell
Concatenation Successful

5. Write shell script to find area of rectangle values entered by


keyboard.
echo "Enter the length of rectangle:"; read l echo
"Enter the breadth of rectangle:"; read b echo
"Area:"`expr $l '*' $b`

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

6. Write shell script to find area of rectangle values entered by


keyboard.
echo "Enter the length of rectangle:"; read l echo
"Enter the breadth of rectangle:"; read b echo
"Area:"`expr $l '*' $b`

OUTPUT:-
Enter the length of rectangle:
10 Enter the breadth of
rectangle: 60
Area:600

7. Write shell script to calculate basic salary where da = 40% of


basic and hra=20% of basic. ANS. clear
echo "Enter your basic salary"; read sal da=`expr
$sal '*' 40 / 100` echo
$da
OUTPUT:-
Enter your basic salary
1000
400

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`

a=`expr $a - $b` echo "After


swapping:" echo "a: "$a
echo
"b: "$b
OUTPUT:-
Enter first number 9
Enter second number 10
After swapping: a: 10 b: 9

8. Write shell script to find number is odd or even. ANS. echo


"Enter a number "; read a if expr $a % 2 == 0 then echo
"Even number" else
echo "Odd number"
fi
OUTPUT:-
Enter a number
5
0
Odd number

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

"Enter UNIX marks: "; read unix echo "Enter


AWD marks: "; read awd echo "Enter NT marks:
"; read nt total=`expr $wfs + $asp +
$unix + $awd + $nt` echo
"Total: "$total per=`expr
$total '*' 100 / 500` echo
"Percentage:"$per if test $per
-gt 70 then echo
"Distinction" elif test $per -gt
60 then echo "First Class"
elif test $per -gt 50 then echo
"Second Class" elif test $per
-gt 40 then echo "Pass Class"
else echo
"Fail" fi

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

10. Write shell script to count number of word, character and


line of any file. ANS. clear echo "Enter file:" read f1 words=`cat $f1
| wc -w` character=`cat $f1 | wc -c` lines=`cat $f1 | wc -l` echo "Words in
file:"$words echo "Characters in file:"$character echo "Lines in
file:"$lines

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 ))

echo "ADD = $add" echo

RAHUL PATEL 12
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

"SUB = $sub" echo


"MUL = $mul"
echo "DIV = $div"

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

19. Write menu driven program for following services


1. display current directory
2. list current directory
3. make directory
4. copy a file
5. delete a file
6. rename a file
7. list of current logged in users 8. current date and calendar
9.exit.
ANS. a=0 while [ $a=0 ] do echo
"1 pwd" echo "2 ls" echo
"3 mkdir" echo "4 copy"
echo "5 delete" echo "6
rename" echo "7 logged
in" echo "8 date and
calendar" echo "9
exit"
echo "Enter your choice"; read a case $a in
"1")pwd
;;
"2")ls
;;
"3")echo "Enter file"; read file mkdir
RAHUL PATEL 15
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

$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

22. Write any 10 commands of grep. ANS.


Filename:-greps unix shell
shell
SHELL UNIX whatsapp WHATSAPP
instagram whatsapp facebook
SNAPCHAT FACEBOOK
snapchat

RAHUL PATEL 19
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

snaps facebook

1. -c : This prints only a count of the lines that match a


pattern grep facebook greps facebook snaps facebook

2. -h : Display the matched lines, but do not display the


filenames. grep -h facebook greps greps1 facebook
snaps facebook facebook snapchatfacebook
3. -i : Ignores, case for matching grep -i facebook greps
facebook
SNAPCHAT FACEBOOK snaps facebook
4. -l : Displays list of a filenames only. grep -l facebook
greps greps1 greps greps1

5. -n : Display the matched lines and their line numbers.


grep -n facebook greps 8:facebook
11: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

7. -w : Match whole word grep -w facebook greps


facebook

RAHUL PATEL 20
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

snaps facebook

8. -o : Print only the matched parts of a matching line


grep -o facebook greps facebook facebook

9. Word starting from “f” grep "^f" greps facebook


10.Word ending with “k” $ grep "k$" greps facebook
23.
snaps facebook Write any 10 commands of sed.

ANS. Filename:-seds moon sun solar


system planets stars galaxy black holes

1. Replacing or substituting string


sed 's/galaxy/galaxies/' seds
moon sun solar system planets
stars galaxies black holes

2. Delete the line having “galaxy”


sed '/galaxies/d' seds
moon sun
solar system
planets
stars galaxy
black holes

3. Delete particular line sed '2d'


seds moon solar system planets
stars galaxy black holes
4. Delete the last line sed '$d' seds
moon sun solar system planets

RAHUL PATEL 21
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

stars
galaxy

5. Insert word at particular line


number sed '3 i black holes' seds
moon sun black holes solar
system planets stars galaxy
black holes
6. To Delete from nth to last line
sed '2,$ d' seds moon

7. To Delete pattern matching line


sed '/moon/d' seds sun solar
system planets stars galaxy
black holes

8. Append any text to the end of a


file sed 's/$/hey/' seds moon hey
sunhey solar systemhey
planetshey starshey galaxyhey
black holeshey

9. Append any text to the beginning


of a file sed 's/^/hey/' seds
heymoon heysun
heysolar system
heyplanets
heystars
heygalaxy
heyblack holes

RAHUL PATEL 22
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

10.Delete empty lines sed '/^$/d'


seds
moon sun solar
system planets
stars galax
black
holes

24.
Write any 10 commands of awk.
ANS. 1. Print all data
awk '{print}' awks
johns CEO 10000 julie
manager 100000
smith clerk 6000

2. Print the lines which match the given


pattern. awk '/manager/' awks
julie manager 100000

3. Splitting a Line Into Fields awk '{print


$1,$2}' awks johns CEO julie manager
smith clerk

4. Display Line Number awk '{print NR,$0}'


awks 1 johns CEO 10000 2 julie manager
100000
3 smith clerk 6000

RAHUL PATEL 23
UNIX AND SHELL PROGRAMMING TYBCA SEM 5

5. To find/check for any string in any specific


column awk '{if ($1 == "johns") print $0}'
awks
johns CEO 10000

6. To count the lines in a file: awk 'END


{print NR}' awks
3
7. Print line whose salary is greater than
6000 awk '{if ($3 > 6000) print $0}' awks
johns CEO 10000 julie manager 100000

8. Print row which name starts with “jo” or


“ju” awk '/[jo|ju]/{print $0}' awks johns
CEO 10000
julie manager 100000

9. Print data who is not CEO awk '{if($2 !~


"CEO") print $0}' awks julie manager
100000 smith clerk
6000

10.Display Last Field awk '{print $NF}' awks


10000
100000
6000

RAHUL PATEL 24

You might also like