Professional Documents
Culture Documents
clear
read a
read b
let c=a+b
let c=a-b
let c=a*b
let c=a/b
1
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x calc.sh
root@localhost:~/shellscript$ ./calc.sh
25
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
read n
do
let mul=n*i
done
3
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$ ./multable.sh
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
read a
mkdir moved
cp *.$a ./moved
5
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x cpfile.sh
root@localhost:~/shellscript$./cpfile.sh
txt
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
exit 1
fi
wc -l $1 >f2
cat f2
wc -w $1 >f2
cat f2
wc -c $1 >f2
cat f2
7
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x wc.sh
root@localhost:~/shellscript$./wc.sh
25 wc.sh
102 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
exit 1
fi
ls $1 -l >f1
cat f1
9
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x ls.sh
root@localhost:~/shellscript$./ls.sh ./
total 92
root@localhost:~/shellscript$
10
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
6) Create a file called phone book and write the following Script.
clear
read name
read num
11
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x phonebook.sh
root@localhost:~/shellscript$ ./phonebook.sh
Enter Name
Ashwin
9959999934
Entry Added
root@localhost:~/shellscript$
12
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
#Shell Script to Search Entries from a Phonebook.
clear
read data
if [ -z $output ]
then
else
echo "$output"
fi
13
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x phonesearch.sh
root@localhost:~/shellscript$./phonesearh
Ashwin
Ashwin 9959999934
root@localhost:~/shellscript$
14
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
7) Write a shell Script to compare two strings.
clear
read a
read b
if [ $a == $b ]
then
else
fi
15
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x strcompare.sh
root@localhost:~/shellscript$./strcompare.sh
KMIT
KMIT
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
read num
let rem=num%2
if [ $rem -eq 0 ]
then
else
fi
17
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x eveorodd.sh
root@localhost:~/shellscript$./eveorodd.sh
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
read num
a=0
b=1
clear
echo $a
echo $b
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
root@localhost:~/shellscript$
20
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
10) Write a shell Script to reverse a given number.
clear
read num
clear
rev=0
rem=0
do
let rem=num%10
let rev=rev*10
let rev=rev+rem
let num=num/10
done
21
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x rev.sh
root@localhost:~/shellscript$./rev.sh
567
22
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
11) Write a shell Script which finds type of character entered.
clear
read ch
case $ch in
;;
;;
;;
;;
esac
23
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$chmod +x watisgiven.sh
root@localhost:~/shellscript$./watisgiven.sh
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 "$*"
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
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
fi
if [ $# -gt 2 ]
then
exit 1
fi
data=$1
if [ -f $data ]
then
elif [ -d $data ]
then
else
fi
27
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
Output:
root@localhost:~/shellscript$./fordirsh clang
28
UNIX NETWORK PROGRAMMING LAB
MCA III Semester
29