0% found this document useful (0 votes)
381 views29 pages

UNIX Shell Scripting for MCA Students

The document contains 10 sections that provide shell script examples to perform various tasks: 1. A script to calculate addition, subtraction, multiplication, and division of two numbers. 2. A script to generate a multiplication table for a given number. 3. A script to copy files with a given extension to a new directory. 4. A script that counts the lines, words, and characters in a given file. 5. A script that displays all files in a given directory. 6. Scripts to add entries to and search a phone book. 7. A script to compare two strings. 8. A script to determine if a number is even or odd

Uploaded by

raju
Copyright
© Attribution Non-Commercial (BY-NC)
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)
381 views29 pages

UNIX Shell Scripting for MCA Students

The document contains 10 sections that provide shell script examples to perform various tasks: 1. A script to calculate addition, subtraction, multiplication, and division of two numbers. 2. A script to generate a multiplication table for a given number. 3. A script to copy files with a given extension to a new directory. 4. A script that counts the lines, words, and characters in a given file. 5. A script that displays all files in a given directory. 6. Scripts to add entries to and search a phone book. 7. A script to compare two strings. 8. A script to determine if a number is even or odd

Uploaded by

raju
Copyright
© Attribution Non-Commercial (BY-NC)
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

UNIX NETWORK PROGRAMMING LAB

MCA III Semester


1)Write a shell Script to finding Multiplication, Division, Subtraction
and Addition of two numbers.

clear

echo “Enter first number”

read a

echo “Enter second number”

read b

let c=a+b

echo “$a+$b= “$c

let c=a-b

echo “$a-$b= “$c

let c=a*b

echo “$a*$b= “$c

let c=a/b

echo “$a/$b= “$c

1
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$ ./[Link]

Enter first number

25

Enter second number

25+5= 30

25-5= 20

25*5= 125

25/5= 5

2
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
2) Write a shell Script to generate multiplication table.

clear

echo “Enter any Number”

read n

for (( i = 0; i <=10; i++ ))

do

let mul=n*i

echo "$n * $i = $mul"

done

3
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$ chmod +x [Link]

root@localhost:~/shellscript$ ./[Link]

“Enter any Number”

7*0=0

7*1=7

7 * 2 = 14

7 * 3 = 21

7 * 4 = 28

7 * 5 = 35

7 * 6 = 42

7 * 7 = 49

7 * 8 = 56

7 * 9 = 63

7 * 10 = 70

root@localhost:~/shellscript$

4
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
3) Write a shell Script to copy multiple files to a Directory.

clear

echo "Program to move Multiple Files into a Directory"

echo "Enter Wich type of Files you want to copy"

read a

mkdir moved

cp *.$a ./moved

echo "All .$a files are copied into moved folder"

5
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link]

Program to move Multiple Files into a Directory

Enter Wich type of Files you want to copy

txt

All .txt files are copied into moved folder

6
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
4) Write a shell Script that counts number of lines, words present in
a given file.

clear

if [ $# -eq 0 ]

then

echo "Error - missing command line arguments"

exit 1

fi

wc -l $1 >f2

echo "Number of lines in $1"

cat f2

wc -w $1 >f2

echo "Number of words in $1"

cat f2

wc -c $1 >f2

echo "Number of Characters in $1"

cat f2

7
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link]

Number of lines in [Link]

25 [Link]

Number of words in [Link]

102 [Link]

Number of Characters in [Link]

511 [Link]

root@localhost:~/shellscript$

8
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
5) Write a shell Script that displays list of all files in a given
Directory.

clear

if [ $# -eq 0 ]

then

echo "Error - missing command line arguments"

echo "Syntax : $0 DIR Name"

echo "****Use to Display all files in Given Directory****"

exit 1

fi

ls $1 -l >f1

echo "List of Files and directories in Given Dir $1"

cat f1

9
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link] ./

List of Files and directories in Given Dir ./

total 92

-rw-r--r-- 1 root root 0 2010-01-10 12:06 f1

-rw-r--r-- 1 root root 10 2010-01-06 15:20 f2

-rw-r--r-- 1 root root 7 2010-01-07 12:55 [Link]

-rwxr-xr-x 1 root root 167 2010-01-05 10:20 [Link]

-rwxr-xr-x 1 root root 344 2010-01-06 12:40 [Link]

-rwxr-xr-x 1 root root 252 2010-01-05 14:37 [Link]

drwxr-xr-x 2 root root 4096 2010-01-10 12:02 moved

-rwxr-xr-x 1 root root 173 2010-01-06 21:36 [Link]

-rw-r--r-- 1 root root 296 2010-01-05 09:38 [Link]~

-rw-r--r-- 1 root root 98 2010-01-06 12:00 phone

-rwxr-xr-x 1 root root 147 2010-01-06 11:59 [Link]

root@localhost:~/shellscript$

10
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
6) Create a file called phone book and write the following Script.

I) To add an entry into the phone book such that entries


must be in sorted order

II) To search for a given entry

clear

echo "Enter Name"

read name

echo "Enter Phone Number"

read num

echo "$name $num" >>phone

sort phone >temp

cat temp >phone

echo "Entry Added"

11
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$ ./[Link]

Enter Name

Ashwin

Enter Phone Number

9959999934

Entry Added

root@localhost:~/shellscript$

12
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
#Shell Script to Search Entries from a Phonebook.

clear

echo "Phone Book Search"

echo "Enter name or Number of the Person You Want"

read data

output=`grep $data phone`

if [ -z $output ]

then

echo "$data is Not Found"

else

echo "$output"

fi

13
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./phonesearh

Phone Book Search

Enter name or Number of the Person You Want

Ashwin

Ashwin 9959999934

root@localhost:~/shellscript$

14
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
7) Write a shell Script to compare two strings.

clear

echo "Enter first String"

read a

echo "Enter Second String"

read b

if [ $a == $b ]

then

echo "$a and $b are Same"

else

echo "$a and $b are Different"

fi

15
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link]

Enter first String

KMIT

Enter Second String

KMIT

KMIT and KMIT are Same

root@localhost:~/shellscript$

16
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
8) Write a shell Script to find whether a given number is even or
odd.

clear

echo "Enter any Number"

read num

let rem=num%2

if [ $rem -eq 0 ]

then

echo "$num is Even"

else

echo "$num is Odd"

fi

17
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link]

Enter any Number

543

543 is Odd

root@localhost:~/shellscript$

18
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
9) Write a shell Script to generate Fibonacci series.

clear

echo "Enter Any Number"

read num

a=0

b=1

clear

echo "Fibonacci Series"

echo $a

echo $b

for (( i = 1; i <= num; i++ ))

do

let c=a+b

let a=b

let b=c

echo $c

done

19
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$ ./[Link]

Fibonacci Series

root@localhost:~/shellscript$

20
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
10) Write a shell Script to reverse a given number.

clear

echo "Enter any Number"

read num

clear

rev=0

rem=0

while [ $num -gt 0 ]

do

let rem=num%10

let rev=rev*10

let rev=rev+rem

let num=num/10

done

echo "Reversre of GIVEN number is "$rev

21
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link]

Enter any number

567

Reverse of GIVEN number is 765

22
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
11) Write a shell Script which finds type of character entered.

clear

echo "Enter any Character"

read ch

case $ch in

[0-9]) echo "you entered Number Betwwen 0 to 9"

;;

[a-b]) echo "You enterd alphabet"

;;

?) echo "You Entered Special Character"

;;

*) echo "You Entered Somethin More"

;;

esac

23
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link]

Enter any Character

You enterd alphabet

root@localhost:~/shellscript$

24
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
12) Write a shell Script to count number of arguments and list all
arguments.

clear

let a=$#

echo "Number of Command Line Argumnets are $a"

echo "Arguments Wich You are Entered in Command Line"

echo "$*"

25
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$chmod +x [Link]

root@localhost:~/shellscript$./[Link] a b c d

Number of Command Line Argumnets are 4

Arguments Wich You are Entered in Command Line

abcd

root@localhost:~/shellscript$

26
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
13) Write a Shell Script to find the given name is file or directory.

clear

if [ $# -eq 0 ]

then

echo "Please Enter atleast one Argument"

echo "Usage $0 STRING"

fi

if [ $# -gt 2 ]

then

echo "Please Enter only one Argument"

echo "Usage $0 STRING"

exit 1

fi

data=$1

if [ -f $data ]

then

echo "$data is Normal File"

elif [ -d $data ]

then

echo "$data is Directory File"

else

echo "$data is Not a File"

fi

27
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:

root@localhost:~/shellscript$ chmod +x [Link]

root@localhost:~/shellscript$ ./[Link] [Link]

[Link] is Normal File

root@localhost:~/shellscript$./fordirsh clang

clang is Directory File

28
UNIX NETWORK PROGRAMMING LAB
MCA III Semester

29

You might also like