Professional Documents
Culture Documents
Lab Sheet - 1: Dept. of Ce B.Tech Iv/Ii Unix & Shell Prog. Lab 2012-13
Lab Sheet - 1: Dept. of Ce B.Tech Iv/Ii Unix & Shell Prog. Lab 2012-13
of CE
Exercise 2
1. 2. 3. 4. 5. 6. Create directory nuva123 Copy the file nuva.txt in to nuva123 directory Add some more information to nuva.txt Rename file nuva.txt to NUVACE.txt Delete the file nuva.txt. 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. 2. 3. Show content of current directory with different options and observe different outputs. Change directories and check where you are. Notice where is your home directory. Write a command to exit from the terminal.
Exercise 5:
1. 2. 3. 4. 5. Check IP of your machine Ping to your machine using IP Ping to servers in Nuva Trace route to your machine using IP Trace route to servers in Nuva.
Exercise 6: 1
Dept. of CE
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 4320 6830 1450 a. b. c. d. e. ravi ramu sita raju 15.65 26.27 36.15 21.86
use the cat command to display the file, mytable use the vi command to correct any errors in the file, mytable use the sort command to sort the file my table according to the first field. Call the sorted file mytable(same name) print the file mytable logout of the system
Exercise 1: 2
Dept. of CE
1. List different commands to create a file called nuva.txt 2. Count number of characters, lines and words in nuva.txt
Sol:
Dept. of CE 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
Dept. of CE
a. b. c. d. e. f.
Write a command to change user password. Write a command to check current user. Write a command to show the entire process running with process id. Display file content using different commands and observe differences. Display all process in the system. Display Directory content page by page
Sol:
Exercise 4: 5
Dept. of CE
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
Dept. of CE
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
Dept. of CE
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 4320 6830 1450 ravi ramu sita raju 15.65 26.27 36.15 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:
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
Dept. of CE
Program 1:
#!/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:
10
Dept. of CE
OUTPUT:
Program 3:
11
Dept. of CE
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:
12
Dept. of CE
#!/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:
13
Dept. of CE
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:
14
Dept. of CE
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:
15
Dept. of CE
#!/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:
16
Dept. of CE
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:
17
Dept. of CE
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 fi j=`expr $j + 1` done OUTPUT:
Program 10: Shell Script to check given number is Magic number or not.
18
Dept. of CE
OUTPUT:
19
Dept. of CE
Program 11: clear
echo "Enter number of elements: " read n echo "Enter array elements: " for ((i=0; i<n; i++)) do read a[$i] done for ((i=0; i<n; i++)) do for ((j=$i; j<n; j++)) do if [ ${a[$i]} -gt ${a[$j]} ] then temp=${a[$i]} a[$i]=${a[$j]} a[$i]=$temp fi done done echo "Array after sorting: " for ((i=0; i<n; i++)) do echo ${a[$i]} done
OUTPUT:
20
Dept. of CE
Lab Sheet -3
21
Dept. of CE
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 10% of the basic and DA =90% of the basic b) 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 string 2) 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
Dept. of CE
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
Dept. of CE
Program 2:
employee according to the following a) If basic salary is <1500 then HRA 10% of the basic and DA =90% of the basic b) 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 [ $sal<1500] then da=`expr $sal*90/100` hra=`expr $sal*10/100` gsal=expr $sal +$hra+$da echo $gsal elif [$sal>1500] hra=500 da=expr $sal*98/100 gsal=expr $sal+$hra+$da gross=`expr $sa + $da + $hra` fi fi
Output:
Program 3:
arguments. It should check whether the two file contents are same or not. If they are same then second file should be deleted.
24
Dept. of CE
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
Dept. of CE
# 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 "<$fn> 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
Dept. of CE
# 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
Dept. of CE
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
Dept. of CE
c) read, write and execute permissions d) time of last access (Note: use /fstat system calls) #include<stdio.h> 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
Dept. of CE
#include<fcntl.h> #include<unistd.h> #include<stdio.h> 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<stdio.h> #include<fcntl.h>
30
Dept. of CE
#include<stdlib.h> main(int argc,char *argv[]) { int fd,i; char ch[1]; if (argc<2) {
printf("Usage: mycat filename\n"); exit(0); } fd=open(argv[1],O_RDONLY); if(fd==-1) printf("%s is not exist",argv[1]); else { printf("Contents of the file %s is : \n",argv[1]); while(read(fd,ch,1)>0) printf("%c",ch[0]); close(fd); } } Output:
31