unix record
TRANSCRIPT
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 calc.sh
root@localhost:~/shellscript$ ./calc.sh
Enter first number
25
Enter second number
5
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 multable.sh
root@localhost:~/shellscript$ ./multable.sh
“Enter any Number”
7
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 cpfile.sh
root@localhost:~/shellscript$./cpfile.sh
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 wc.sh
root@localhost:~/shellscript$./wc.sh
Number of lines in wc.sh
25 wc.sh
Number of words in wc.sh
102 wc.sh
Number of Characters in wc.sh
511 wc.sh
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 ls.sh
root@localhost:~/shellscript$./ls.sh ./
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 fexists.sh
-rwxr-xr-x 1 root root 167 2010-01-05 10:20 fib.sh
-rwxr-xr-x 1 root root 344 2010-01-06 12:40 fordir.sh
-rwxr-xr-x 1 root root 252 2010-01-05 14:37 lsfile.sh
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 multable.sh
-rw-r--r-- 1 root root 296 2010-01-05 09:38 multable.sh~
-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 phonebook.sh
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 phonebook.sh
root@localhost:~/shellscript$ ./phonebook.sh
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 phonesearch.sh
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 strcompare.sh
root@localhost:~/shellscript$./strcompare.sh
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 eveorodd.sh
root@localhost:~/shellscript$./eveorodd.sh
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 fib.sh
root@localhost:~/shellscript$ ./fib.sh
Fibonacci Series
0
1
1
2
3
5
8
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 rev.sh
root@localhost:~/shellscript$./rev.sh
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 watisgiven.sh
root@localhost:~/shellscript$./watisgiven.sh
Enter any Character
a
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 arg.sh
root@localhost:~/shellscript$./arg.sh a b c d
Number of Command Line Argumnets are 4
Arguments Wich You are Entered in Command Line
a b c d
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 fordir.sh
root@localhost:~/shellscript$ ./fordir.sh fordir.sh
fordir.sh is Normal File
root@localhost:~/shellscript$./fordirsh clang
clang is Directory File
28
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
29