7/30/2019 UNIX--Record 2012-13.doc
1/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
UNIX Lab Sheet -1
Exercise 1:
1. List different commands to create a file called nuva.txt
2. Count number of characters, lines and words in nuva.txt
Exercise 2
1. Create directory nuva123
2. Copy the file nuva.txt in to nuva123 directory
3. Add some more information to nuva.txt
4. Rename file nuva.txt to NUVACE.txt
5. Delete the file nuva.txt.
6. Delete the Directory nuva123.
Exercise 3:
1. Write a command to change user password.
2. Write a command to check current user.
3. Write a command to show the entire process running with process id.
4. Display file content using different commands and observe differences.
5. Display all process in the system.
6. Display Directory content page by page
Exercise 4:
1. Show content of current directory with different options and observe
different outputs.
2. Change directories and check where you are. Notice where is your home
directory.
3. Write a command to exit from the terminal.
Exercise 5:
1. Check IP of your machine
2. Ping to your machine using IP
3. Ping to servers in Nuva
4. Trace route to your machine using IP
5. Trace route to servers in Nuva.
Exercise 6:
1
7/30/2019 UNIX--Record 2012-13.doc
2/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
1. Log into the Student
2. Use the cat command to create a file containing the following data. Call it
mutable use tabs to separate the fields
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
a. use the cat command to display the file, mytable
b. use the vi command to correct any errors in the file, mytable
c. use the sort command to sort the file my table according to the
first field. Call the sorted file mytable(same name)
d. print the file mytable
e. logout of the system
Exercise 1:
2
7/30/2019 UNIX--Record 2012-13.doc
3/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
1. List different commands to create a file called nuva.txt
2. Count number of characters, lines and words in nuva.txt
Sol:
3
7/30/2019 UNIX--Record 2012-13.doc
4/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Exercise 2
1. Create directory nuva123
2. Copy the file nuva.txt in to nuva123 directory
3. Add some more information to nuva.txt
4. Rename file nuva.txt to NUVACE.txt
5. Delete the file nuva.txt.
6. Delete the Directory nuva123.
Sol:
Exercise 3:
4
7/30/2019 UNIX--Record 2012-13.doc
5/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
a. Write a command to change user password.
b. Write a command to check current user.
c. Write a command to show the entire process running with process id.
d. Display file content using different commands and observe differences.
e. Display all process in the system.
f. Display Directory content page by page
Sol:
Exercise 4:
5
7/30/2019 UNIX--Record 2012-13.doc
6/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
1. Show content of current directory with different options and observe
different outputs.
2. Change directories and check where you are. Notice where is your
home directory.
3. Write a command to exit from the terminal.
Sol:
Exercise 5:
6
7/30/2019 UNIX--Record 2012-13.doc
7/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
1. Check IP of your machine
2. Ping to your machine using IP
3. Ping to servers in Nuva
4. Trace route to your machine using IP
5. Trace route to servers in Nuva.
Sol:
Exercise 5:
7
7/30/2019 UNIX--Record 2012-13.doc
8/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Log into the Student
Use the cat command to create a file containing the following data. Call it
mutable use tabs to separate the fields
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
1. use the cat command to display the file, mytable
2. use the vi command to correct any errors in the file, mytable
3. use the sort command to sort the file my table according to the first field.
Call the sorted file mytable(same name)
4. print the file mytable
5. logout of the system
Sol:
UNIX Lab Sheet -2
8
7/30/2019 UNIX--Record 2012-13.doc
9/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Shell Scripts:
1. Shell Script to find out biggest number Among 3nubaers
2. Shell Script to Print N numbers
3. Shell Script to Perform Arithmetic operations using Case.
4. Shell Script to Swap 2 number without using 3rd.
5. Shell Script to find sum of digits of given number
6. Write a shell script to generate Fibonacci numbers from 1 to n
7. Shell Script to find factorial of given number
8. Shell Script to reverse given number
9. Write shell script to print prime numbers in a given range?
10. Shell Script to check given number is Magic number or not.
11. Shell Program to Sort an Array
UNIX Lab Sheet -2
9
7/30/2019 UNIX--Record 2012-13.doc
10/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Program 1: Shell Script to find out biggest number among 3nubaers
#!/bin/bash
echo Enter 3 numbers with spaces in between
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Lagest of $a $b $c is $l
OUTPUT:
Program 2: Shell Script to Print n numbers
10
7/30/2019 UNIX--Record 2012-13.doc
11/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
#!/bin/bash
for i in $(seq 5)
do
echo "Abdulla welcomes U $i time"
done
OUTPUT:
Program 3: Shell Script to Perform Arithmetic operations using Case.
11
7/30/2019 UNIX--Record 2012-13.doc
12/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
echo Enter two number
read a b
echo 1.Add 2.Sub 3.Mul 4.Div 5.Exit
read op
case $op in
1) c=`expr $a + $b` ;;
2) c=`expr $a - $b` ;;
3) c=`expr $c=$a \* $b` ;;
4) c=`expr $c=$a / $b` ;;
5) exit
esac
echo $c
OUTPUT:
Program 4: Shell Script to Swap 2 number without using 3rd .
12
7/30/2019 UNIX--Record 2012-13.doc
13/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
#!/bin/bash
echo "enter first number"
read a
echo "enter second number"
read b
echo "a before swapping is $a and b is $b"
#swapping
a=$((a+b))
b=$((a - b))
a=$((a-b))
echo "a after swapping is $a and b is $b"
OUTPUT:
Program 5: Shell Script to find sum of digits of given number
13
7/30/2019 UNIX--Record 2012-13.doc
14/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
echo "Enter number : "
read n
s=0
while [ $n -gt 0 ]
do
r=$(( $n % 10 ))
n=$(( $n / 10 ))
s=$(( $s + $r ))
done
echo "Sum of Digit is : $s"
OUTPUT:
Program 6: Write a shell script to generate Fibonacci numbers from 1 to n
14
7/30/2019 UNIX--Record 2012-13.doc
15/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
echo "Enter n for Fibonacci series: - "
read n
echo "Fibonacci series is: - "
echo "0"
echo "1"
i=0
j=1
c=2
while [ $n -gt $c ]
do
k=$(($i + $j))
i=$j
j=$k
echo $k
c=$(($c + 1))
done
OUTPUT:
Program 7: Shell Script to Finding factorial of a given number
15
7/30/2019 UNIX--Record 2012-13.doc
16/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
#!/bin/bash
# fact1
# Finding factorial of a given number
#
#!/bin/bash
echo "Enter a number"
read num
n=$num
fact=1
while [ $num -ge 1 ]
do
fact=`expr $num \* $fact`
num=$(($num - 1))
done
echo "Factorial of $n is = $fact"
OUTPUT:
Program 8: Shell Script to reverse given number
16
7/30/2019 UNIX--Record 2012-13.doc
17/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
echo -n "Enter number : "
read n
sd=0
rev=""
on=$n
while[ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
rev=$(echo ${rev}${sd})
done
echo "$on in a reverse order $rev"
OUTPUT:
Program 9 : Shell script to print prime numbers in a given range.
17
7/30/2019 UNIX--Record 2012-13.doc
18/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
echo enter a range
read rng
echo 2
j=3
while test $j -le $rng
do
i=2
x=`expr $j - 1`
while test $i -le $x
do
if [ `expr $j % $i` -ne 0 ]
then
i=`expr $i + 1`
else
break
fi
done
if [ $i -eq $j ]
then
echo $j
fij=`expr $j + 1`
done
OUTPUT:
Program 10: Shell Script to check given number is Magic number or not.
18
7/30/2019 UNIX--Record 2012-13.doc
19/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
OUTPUT:
19
7/30/2019 UNIX--Record 2012-13.doc
20/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Program 11: Shell Program to Sort an Array
clear
echo "Enter number of elements: "
read n
echo "Enter array elements: "
for ((i=0; i
7/30/2019 UNIX--Record 2012-13.doc
21/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Lab Sheet -3
21
7/30/2019 UNIX--Record 2012-13.doc
22/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Program 1: Write A shell script that takes a command line argument and reports
on whether it is directory, a file, or something else
Program 2: Write a shell script that computes the gross salary of a employee
according to the following
a) If basic salary is 1500 then HRA 500 and DA =98% of the basic The basic
salary is entered interactively through the key board
Program 3: Write a shell script which receives two files names as arguments. It
should check whether the two file contents are same or not. If they are same then
second file should be deleted.
Program 4: Write a shell script that displays a list of all files in the current directory
to which the user has read write and execute permissions
PROGRAM 5: Develop an interactive script that asks for a word and file name and
then tells how many times that word occurred in the file.
PROGRAM 6: Write a shell script to perform the following string operations.
1) To extract a sub string from a given string2) To find the length of a given string
Program 7: Write a C program that takes one or more file or directory names as
command line input and reports the following information on the file.
Program 8: Write C program that simulate the following unix commands
(a) mv
(b) cp
Program 9: Write a c program that simulates ls command
(Use system calls /directory API)
Lab Sheet -3
22
7/30/2019 UNIX--Record 2012-13.doc
23/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Program 1: Write A shell script that takes a command line argument and
reports on whether it is directory, a file, or something else
echo "Enter a file name:"
read f
if [ -f $f ]
then
echo "File"
elif [ -d $f ]
then
echo "Directory"
else
echo "Not"
fi
Output:
Directory
23
7/30/2019 UNIX--Record 2012-13.doc
24/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Program 2: Write a shell script that computes the gross salary of a
employee according to the following
a) If basic salary is 1500 then HRA 500 and DA =98% of the basic The
basic salary is entered interactively through the key board
echo " Enter the Salary "
read sal
if [ $sal1500]
hra=500
da=expr $sal*98/100
gsal=expr $sal+$hra+$da
gross=`expr $sa + $da + $hra`
fi
fi
Output:
Program 3: Write a shell script which receives two files names as
arguments. It should check whether the two file contents are same or not.
If they are same then second file should be deleted.
24
7/30/2019 UNIX--Record 2012-13.doc
25/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
echo "Enter I File Name:"
read f1
echo "Enter II File Name:"
read f2
d=`cmp $f1 $f2`
d1=""
if [ $d -eq $d2 ]
then
echo "Two Files are similar and $f2 is deleted"
rm $f2
else
echo "Two Files differ each other"
fi
Output:
Program 4: Write a shell script that displays a list of all files in the current
directory to which the user has read write and execute permissions
25
7/30/2019 UNIX--Record 2012-13.doc
26/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
# File Name : list.sh
#!/bin/bash
read -p "Enter a directory name : " dn
if [ -d $dn ]; then
printf "\nFiles in the directory $dn are :\n"
for fn in `ls $dn`
do
if [ -d $dn/$fn ]; then
printf " Directory "
elif [ -f $dn/$fn ]
then
printf "$fn File "
fi
if [ -r $dn/$fn ]; then
printf " Read"
fi
if [ -w $dn/$fn ];then
printf " Write"
fi
if [ -x $dn/$fn ];then
printf " Execute"fi
printf "\n"
done
else
printf "\n$dn not exists or not a directory"
fi
Output:
PROGRAM 5: Develop an interactive script that asks for a word and file
name and then tells how many times that word occurred in the file.
26
7/30/2019 UNIX--Record 2012-13.doc
27/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
# File Name : wcount.sh
#!/bin/bash
read -p "Enter a file name : " fn
if test -f $fn
then
echo "The contents of the file $fn is :"
cat $fn
echo "No. of Line : `wc -l $fn`"
echo "No. of Words : `wc -w $fn`"
echo "No. of Characters: `wc -c $fn`"
else
echo "$fn is not exists or not a file"
fi
Output:
PROGRAM 6: Write a shell script to perform the following string perations.
1) To extract a sub string from a given string
2) To find the length of a given string
27
7/30/2019 UNIX--Record 2012-13.doc
28/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
Print Enter the String:\c
read strIn
strlen=${# strIn}
print the string length is : $strlen
$ strlen.scr
Output:
Enter the String: Now is the time
The String length : 15
Program 7: Write a C program that takes one or more file or directory
names as command line input and reports the following information on the
file.
a) file type
b) number of links
28
7/30/2019 UNIX--Record 2012-13.doc
29/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
c) read, write and execute permissions
d) time of last access (Note: use /fstat system calls)
#include
main()
{
FILE *stream;
int buffer_character;
stream=fopen(test,r);
if(stream==(FILE*)0)
{
fprintf(stderr,Error opening file(printed to standard error)\n);
fclose(stream);
exit(1);
}
}
if(fclose(stream))==EOF)
{
fprintf(stderr,Error closing stream.(printed to standard error)\n);
exit(1);
}return();
}
Output:
Program 8: Write C program that simulate the following unix commands
(a) mv
(b) cp
/* File Name : bspace1.c */
29
7/30/2019 UNIX--Record 2012-13.doc
30/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
#include
#include
#include
main(int argc,char *argv[])
{
FILE *fp;
char ch;
int sc=0;
fp=fopen(argv[1],"r");
if(fp==NULL)
printf("unable to open a file",argv[1]);
else
{
while(!feof(fp))
{
ch=fgetc(fp);
if(ch==' ')
sc++;
}
printf("no of spaces %d",sc);
printf("\n");fclose(fp);
}
}
Output:
Program 9: Write a c program that simulates ls command
(Use system calls /directory API)
#include
#include
30
7/30/2019 UNIX--Record 2012-13.doc
31/31
Dept. of CE B.Tech IV/II UNIX & Shell Prog. Lab 2012-13
#include
main(int argc,char *argv[])
{
int fd,i;
char ch[1];
if (argc0)
printf("%c",ch[0]);
close(fd);
}
}
Output:
Top Related