You are on page 1of 51

1

SOLUTIONS:

WEEK1
Session 1

1. Log in to the system


2. Use Vi editor to create a file called myfile.txt which contain
some text.
3. Correct typing errors during creation
4. Save the file
5. Logout of the file

Sol:

$ login: <user name>


$ password: ******

$ vi myfile.txt
~ Unix is Case Sensitive
~ Never leave the Computer without logging out when you are working in a
time sharing or network environments.

Type <Esc>
: wq myfile

$
Session 2
1. Log into the system
2. Open the file created in session 1
3. Add some text
4. Change some text
5. delete some text
6. Save the changes
7. Logout of the system
Sol:
$ login: <user name>
$ password: ******

$ vi myfile

~ Unix is Case Sensitive


~ Never leave the Computer without logging out when you are working in a
time sharing or network environments.
~ Shell Programming

: wq

Inserting or Adding Text


FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
2
The following commands allow you to insert and add text. Each of these
commands puts the vi editor into insert mode; thus, the <Esc> key must be
pressed to terminate the entry of text and to put the vi editor back into command
mode.
* i insert text before cursor, until <Esc> hit
I insert text at beginning of current line, until <Esc> hit
* a append text after cursor, until <Esc> hit
A append text to end of current line, until <Esc> hit
* o open and put text in a new line below current line, until <Esc> hit
* O open and put text in a new line above current line, until <Esc> hit

Changing Text

The following commands allow you to modify text.


*R replace single character under cursor (no <Esc> needed)
R replace characters, starting with current cursor position, until <Esc> hit

Cw
change the current word with new text,
starting with the character under cursor, until <Esc> hit

cNw
change N words beginning with character under cursor, until <Esc> hit;
e.g., c5w changes 5 words
C change (replace) the characters in the current line, until <Esc> hit
Cc change (replace) the entire current line, stopping when <Esc> is hit
change (replace) the next N lines, starting with the current line,
Ncc or cNc
stopping when <Esc> is hit

Deleting Text

The following commands allow you to delete text.


*X delete single character under cursor
Nx delete N characters, starting with character under cursor
Dw delete the single word beginning with character under cursor

dNw
delete N words beginning with character under cursor;
e.g., d5w deletes 5 words
D delete the remainder of the line, starting with current cursor position
* Dd delete entire current line
delete N lines, beginning with the current line;
Ndd or dNd
e.g., 5dd deletes 5 lines

WEEK2
Log into the system
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
3
Use the cat command to create a file containing the following data. Call it
mytable 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, my table


b. use the vi command to correct any errors in the file, my table
c. use the sort command to sort the file my table according to the
first field. Call the sorted file my table(same name)
d. print the file my table
e. use the cut & paste commands to swap fields 2 and 3 my table.
Call it mytable(same name)
f. print the new file, my table
g. logout of the system

Sol:

$ login: <user name>


$ password:******

b)Use the cat command to create a file containing the following data.
Call it mytable use tabs to separate the fields.
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86

Sol: cat > mytable

1425 Ravi 15.65


4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86

c)Use the cat command to display the file, mytable.

Sol: $cat mytable

1425 Ravi 15.65


4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86

d)Use the vi command to correct any errors in the file, mytable.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


4
Sol: Verify the file with Vi editor Commannds

e)Use the sort command to sort the file mytable according to the first
field. Call the sorted file my table
(same name)

Sol: $sort +0 -1 mytable > mytable

f)Print the file mytable

Sol: lp mytable

1425 Ravi 15.65


1450 Raju 21.86
4320 Ramu 26.27
6830 Sita 36.15

g)Use the cut and paste commands to swap fields 2 and 3 of mytable.
Call it my table (same name)

Sol:
$cut -f1 mytable> mytab1

$ cut –f 2 mytable > mytab 2

$cut –f 3 mytable > my tab3

$paste mytab3 mytab2 > mytab4

$paste mytab1 mytab4 > mytable

h)Print the new file, mytable

Sol: $ lp mytable

1425 15.65 Ravi


1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita

WEEK3

a. log in the system


b. use the appropriate commands to determine ur login shell
c. use the /etc/passwd file to verify the result of step b.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


5
d. use the who command redirect the result to a file called
myfile1.Use the more command to see the contents of myfile1.
e. Use the date and who commands in sequence ?(in one line) such
that the output of date will display on the screen and the output of
who will be redirected to a file called my file2.Use the more
command to check the contents of myfile2.

What is the difference between cat and more command?


Cat displays file contents. If the file is large the contents scroll off the screen
before we view it. So command 'more' is like a pager which displays the
contents page by page.
use the appropriate commands to determine ur login shell

$ echo $SHELL

/bin/bash

use the /etc/passwd file to verify the result of step b.

Sol: $cat /etc/passwd

1.Login Name 2.Password 3.UserID 4.GroupID 5.User info


6.Home Directory 7.Login Shell

d)Use the who command and redirect the result to a file called myfile1.
Use the more command to see the contents of myfile1.

Sol : $who > myfile1


$more myfile1

User1 pts/0 Apr 23 10:43


User2 pts/1 May 6 18:19

e)Use the date and who commands in sequence (in one line) such that the
output of date will display on the screen and the output of who will be
redirected to a file called myfile2. Use the more command to check the
contents of myfile2.

Sol: $ date ; who > myfile2


FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
6
Fri Aug 9 16:47:32 IST 2008

$cat myfile2 :

User3 pts/2 Apr 25 10:43


User4 pts/3 May 8 18:19

InputFile:file1.dat :
UnixisMultiuser OS
Unix was developed by Brian Kernighan and KenThomson

2.a)write a sed command that deletes the first character in each line in a
file

Sol: sed 's/^./ /’ file1.dat

nix is Multiuser OS
nix was developed by Brian Kernighan and KenThomson

b)write a sed command that deletes the character before the last
character in each line in a file.

Sol: sed ‘s,\(.*\)\(.\)\(.\),\1\3,g ’ file1.dat

Unix is Multiuser S
Unix was developed by Brian Kernighan and KenThomsn

c)Write a sed command that swaps the files and second words in each
line in a file

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


7

$ sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' file1.dat

WEEK4

a)pipe ur /etc/passwd file to awk and print out the home directory of
each user.

Sol: cat /etc/passwd | awk –F: ‘{ print $6}’

b)Develop an interactive grep script that asks for a word and a file
name and then tells how many lines contain that word

sol:

echo “Enter a word”


read word
echo “Enter the filename”
read file
nol=$(grep -c $word $file)
echo “ $nol times $word present in the $file”

Repeat

Part using awk

echo “Enter a word”


read word
echo “Enter the filename”
read file
nol=$(awk ‘/$word/ { print NR }’ Infile)
echo “ $nol times $word present in the $file”

WEEK5

a) Write A shell script that takes a command –line argument and


reports on whether it is directry ,a file,or something else

Sol:
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
8

echo "Enter a file name:"


read f
if [ -f $f ]
then
echo "file exists n it is an ordinary file"

elif [ -d $f ]
then
echo "Directory "
else
echo "Something else"
fi

b)Write a shell script that accepts one or more file name as a


arguments and converts all of them to uppercase, provided they
exits in the current directory

Sol:
Clear
if [ $# -eq 0 ]
then “echo enter the arguments”
exit
fi
for i in $*
do
if [ -f $i ]
then
echo it is a valid file
echo Contents of file before converting
cat $i
echo Contents of file after converting
tr '[a-z]' '[A-Z]' < $i
k=`ls $i | tr '[a-z]' '[A-Z]'`
mv $i $k
echo file $i renamed as $x
ls

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


9
else
echo file does not exist
fi
done
Output:
$sh 4b.sh test
It is a valid file
file test renamed as TEST
10b.sh 12b.awk 1bprg.sh 2a.sh 4a.sh 6a.sh 8a.sh a1 d1 first rv TEST
x
$sh 4b.sh program1file does not exist

b) Write a shell script that determines the period for which a


specified user is working on the system.

Sol:

echo “Enter the Login Name of a User”


read name
count=`who | grep –wo “$user” | wc –c`
if [ $count –eq 0 ]
then
echo “invalid user”
exit
fi
if [$count –gt 2 ]
then
echo “Multiple Login”
else
lt=`who | grep “user” | cut –c 34-38`
lh=`echo $lt | cut –c 1-2`
lm=`echo $lt | cut –c 4-5`
ch=`date +%H`
cm=`date +%M`
if [ $cm –gr $lm ]
then
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
10
sm=`expr $cm -$lm`
sh=`expr $ch - $lh`
else
sm=`expr 60 - $lm -$cm`
sh=`expr $ch - $lh – 1`
fi
echo “ The user is logged in from $sh hour $sm minutes”
fi
Output:
1)
Enter the user name :13HP1A1201
The user is logged in from 1 hour 20 minutes
2)
Enter the user name:abc
Invalid user

WEEK6
(a) Write a shell script that accepts a file name starting and ending
line numbers as arguments and displays all the lines between the given
line numbers

Sol:
echo "enter the file name"
read fname
echo "Enter the starting line number "
read s
echo "Enter the ending line number "
read n
sed -n $s,$n\p $fname | cat > newline
cat newline

(b) Write a shell script that deletes all lines containing a specified word
I one or more files supplied as arguments to it.

Sol:
if [ $# -eq 0 ]
then
echo "no arguments passed"
exit
fi
echo "the contents BEFORE deleting"
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
11
for i in $*
do
echo $i
cat $i
done
echo "Enter the word to be deleted:"
read word
for i in $*
do
grep -vi "$word" $i > temp
mv temp $i
echo "AFTER DELETING"
cat $i
done

WEEK7
a) Write a shell script that computes the gross salary of a employee
according to the following
1) if basic salary is <1500 then HRA 10% of the basic and DA =90%
of the basic
2) if basic salary is >1500 then HRA 500 and DA =98% of the basic
The basic salary is entered interactively through the key board

Sol:
echo enter basic salary
read sal
a=0.1
b=0.8
echo $a
echo "hra is"
hra=`echo 0.1 \* $sal|bc`
echo da is
da=`echo 0.8\*$sal|bc`
gsal=’expr $hra + $da + $sal’
echo $gsal
(b)Write a shell script that accepts two integers as its arguments and
computes the value of first number raised to the power of the second
number

Sol:

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


12
echo enter the integer value
read n
echo enter the power value
read m
pv=$n
i=1
while [ $i -lt $m ]
do
pv=`expr $pv \* $n`
i=`expr $i+1`
done
echo value of power is
echo "$pv"

WEEK 8

(a) Write an interactive file handling shell program. Let it offer the user the choice of
copying ,removing ,renaming or linking files. Once the use has made a choice, have
the program ask the user for necessary information, such as the file name ,new name
and so on.

echo "1.COPY"
echo "2.RENAME"
echo "3.REMOVE"
echo "4.LINK"
echo "5.EXIT"
echo "Enter your choice"
read ch
case $ch in
1) echo "Enter the sources file"
read s
echo "Enter the destination file "
read d
cp $s $d
;;
2) echo "Enter the old file name"
read of
echo "enter the new file name"
read nf
mv $of $nf
;;
3) echo "Enter file name to delete"
read df
rm $df
;;
4) echo "Enter the file1"
read f1
echo "enter the file2"
read f2
ln $f1 $f2

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


13
;;
5) exit 0
;;
esac

(b) Write a shell script that takes a login name as command –line
argument and reports when that person logs in

if [ $# -lt 1 ]
then
echo improper usage
echo correct usage is: $0 username
exit
fi
logname=$1
while true
do
who|grep "$logname">/dev/null
if [ $? = 0 ]
then
echo $logname has logged in
echo "$logname">>sh01log.txt
date >>sh01log.txt
echo "Hi" > mesg.txt
echo "$logname" >> mesg.txt
echo "Have a Good Day" >> mesg.txt
mail "$logname" < mesg.txt
exit
else
sleep 60 fi done

(c) 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.

if(cmp $1 $2)
then

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


14
print "the two files are identical"
rm $2
print "Now $2 is deleted"
else
print "the two files are different"
fi

WEEK 9
(a) 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

Sol:
for File in *
doif [ -r $File -a -w $File -a -x $File ]
then
echo $File
fi
done
[or]

ls -l|grep '^.rwx'

(b) Develop an interactive script that asks for a word and file name
and then tells how many times that word occurred in the file.

SAME AS 4B QUESTION

(c) 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

echo "MENU"
echo "1.TO EXTRACT A SUBSTRING FROM AGIVEN STRING"
echo "2.TO FIND THE LENGTH OF THE STRING"
echo "ENTER YOUR CHOICE"
read choice
echo "enter a string"
read string
case $choice in

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


15
1) echo "enter position1"
read pos1
echo "enter position2"
read pos2
echo "the extracted substring from $pos1 to $pos2 of $string is"
echo $string|cut -c$pos1-$pos2
;;
2) len=`echo $string | wc -c`
len=`expr $len - 1`
echo Concatinated string is: $string of length: $len
;;
*) echo " wrong choice "
;;
Esac
WEEK 10
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.
1. file type
2. number of links
3. read, write and execute permissions
4. time of last access
(Note: use /fstat system calls)
PROGRAM #include<stdio.h>

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


16
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
int main()
{
int fd;
struct stat buf;
fd=open("samp.c",O_RDONLY|O_CREAT,600);
if(fd!=-1)
{
if(fstat(fd,&buf)==0)
{
printf("mode of fileis %u",buf.st_mode);
printf("\nsize of the file is %u",buf.st_size);
printf("\n devicename %u",buf.st_dev);
printf("\n inode of file is%u",buf.st_ino);
printf("\n no. of links are%u",buf.st_nlink);
printf("\n owner of file is%u",buf.st_uid);
printf("\n no.of blocks is%u",buf.st_blocks);
printf("\n group owner is%u",buf.st_gid);
printf("\n blocks size of the file is %u",buf.st_blksize);
}
else
printf("error in fstat() syscall");
}
else
printf("error in open() sys call");
}

Output:
mode of fileis 33188
size of the file is 65
devicename 2050
inode of file is4945691
no. of links are1
owner of file is694
no.of blocks is8
group owner is694
blocks size of the file is 4096
time of last access is 1427870386

Write C program that simulate the following unix commands


(a) mv
(b) cp

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


17

a)
#include<stdio.h>
int main()
{
int i;
char fname[255],fname2[255];
printf("enter old file name");
scanf("%s",&fname);
printf("enter new file name:");
scanf("%s",&fname2);
i=rename(fname,fname2);
if(i==0)
{
printf("%s moved to %s",fname,fname2);
}
else
printf("we cannot move this file");
}
b)cp

#include<stdio.h>
#include<string.h>
int main(int argc,char*argv[])
{
FILE *source,*target;
char ch;
if(argc!=3)
{
printf("Command Error!!Insufficient argument given\n");
return;
}
source=fopen(argv[1],"r");
target=fopen(argv[2],"w");
if(source==NULL || target==NULL)
{
printf("Unable to open..ERROR in opening file!!\n");
return;
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
18
}
while((ch=fgetc(source))!=EOF)
fputc(ch,target);
printf("Copy is Successful\n");
fclose(source);
fclose(target);
}
WEEK 12

Write a c program that simulates ls command


(Use system calls /directory API)

PROGRAM:

#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
struct dirent *dire;
DIR *dir;
if((dir=opendir(argv[1]))==NULL)
exit(0);
while((dire=readdir(dir))!=NULL)
printf("%s\n",dire->d_name);
closedir(dir);
}

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


19
WEEK 13
Do the following shell programs
1)write a shell script to check whether a particular user has logged in or
not.if he has eligibility to receive a message or not.

[or]
Write a shell scripts that finds total no. of users and finds out how
many of them are currently logged in. in Unix
cat /etc/passwd>user.txt
set `wc -l user.txt`
log=`who | wc -l`
echo "There are $1 users in network "
echo "There are $log user logged in right now"

2)Write a shell script to accept the name of the file from standard
input and performs the following tests on it
a)file executable b)file readable c)file writable d)both
readable&writable

echo "the list of file names in current directory"


echo "which have read, write & execute permissions"
for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w $file ]
then
ls -l $file
fi
else
echo "file not exit"
fi
done

3)Write a shell script which will display the username and terminal name
who login recently into the unix system.

$ who | cut -d' ' -f1 | sort | uniq

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


20
4)Write a shell script to find no.of files in a directory.

FILE="/tmp/count.txt"
echo -n "Enter directory name : "
read dname

ls $dname > $FILE

echo "No of files in directory : $(grep .^*$ $FILE -c)"


rm -f $FILE

5)write a shell script to check whether a given number is perfect or not.

echo "To know if the given number is Perfect or not"


echo "Enter any number"
read n
for((i=1;i<n;i++))
do
if(($n % i==0))
then
sum=`expr $sum + $i`
fi
done
if(($n==$sum))
then
echo "The given number $n is PERFECT"
else
echo "The given number $n is NOT PERFECT"
fi

6)write a menu driven shell script to copy,edit,rename and delete a file.

SAME AS 8A PROGRAM

7)write a shell script for concatenation of two strings.

echo “enter first string:”


read s1
echo “enter second string:”
read s2
s3 = $s1$s2
echo “concatenated string is $s3 of length $s3 ”

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


21
8)write a shell script to which will display Fibonacci series upto a given
number of arguments.
clear
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1`
z=`expr $x + $y`
echo "$z"
x=$y
y=$z
done

9)write a sell script to accept student number,name,marks in 5


subjects.find total,average and grade.Display the result of student and
store in a file called stu.dat
Rules: avg>=80 then grade A
Avg<80 && Avg>=70 then grade B
Avg<70 && Avg>=60 then grade C
Avg<60 && Avg>=50 then grade D
Avg<50 && Avg>=40 then grade E
Else grade F

echo “Enter the number of students:”


read n
for((i = 1 ; i <= n ; i++)) do
echo “Enter roll no.:”
read rollno
echo “Enter name:”
read name
echo “Entermark-1″
read m1
echo “Entermark-2:”
read m2
echo “Entermark-3:”
read m3

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


22

tot=`expr $m1 + $m2 + $m3`


avg=`expr $tot / 3`
if [ $avg -ge 80 ]
then
grade="A"
elif [ $avg -lt 80 -a $avg -ge 70 ]
then
grade="B"
elif [ $avg -lt 70 -a $avg -ge 60 ]
then
grade="C"
elif [ $avg -lt 60 -a $avg -ge 50 ]
then
grade="D"
elif [ $avg -lt 50 -a $avg -ge 40 ]
then
grade="E"
else
grade="Fail"
fi
echo "Roll no name tot avg grade\n"
echo "$rollno $name $tot $avg $grade"
done

10)Write a shell script to accept empno,empname,basic,find


DA,HRA,TA,PF using following rules.Display
empno,empname,basic,DA,HRA,PF,TA,GROSS SALand NETSAL.also
store all details in a file called emp.dat

Rules:HRA is 18% of basic if basic >5000 otherwise 550


DA is 35% of basic
PF is 13% of basic
IT is 14% of basic
TA is 10% of basic

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


23
11)write ashell script to demonstrate break and continue statements.

12)Write a shell script to satisfy the following menu options


a.Display current directory path
b.Display todays date
c.Display users who are connected to the unix system
d.Quit

13.Write a shell script to delete all files whose size is zero bytes from
current directory.

clear
echo -n "Enter name of the directory :"
read directory
if [ ! -d "$directory" ]
then
echo "Directory does not exist"
else
for i in `find $directory -type f -size 0`
do
rm -i $i
done
fi

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


24
14.write ashell script to display string palindrome from given arguments.

echo "Enter the String:"


read n
len=${#n}
flag=1
for((i=0;i<=len/2;i++))
do
c1="${n:$i:1}"
c2="${n:$len-$i-1:1}"
if [ $c1 != $c2 ]
then
flag=0
echo "String is not palindrome"
break
fi
done
if(( $flag==1))
then
echo "Input String is Palindrome"
fi

15.write a shell script which will display Armstrong numbers from given
arguments.
echo “Enter a number:”
read n
t=$n s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r \* $r \* $r`
n=`expr $n / 10`
done
if [ $s = $t ]
then
echo “$t is an armstrong number”
else
echo “$t is not an armstrong number”
fi

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


25
16.write a shell script to display reverse numbers from given argument
list
echo "Enter a 5 digit number"
read num
n=$num
rev=0
while [ $num -ne 0 ]
do
r=`expr $num % 10`
rev=`expr $rev \* 10 + $r`
num=`expr $num / 10`
done
echo "Reverse of $n is $rev"

17.write a shell script to display factorial value from given argument list

echo “Enter a number:”


read n
f=1
for((i = 1 ; i <= n ; i++)) do
f=`expr $f \* $i`
done
echo “The factorial of $n is $f”

18.Write a shell script which will find maximum file size in the given
argument list.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


26
19.Write a shell script which will greet you “Good Morning”,”Good
Afternoon”,”Good Evening” and Good night” according to current time.

clear
hours=`date|cut -c12-13`
if [ $hours -le 12 ]
then
echo "Good Morning"
else
if [ $hours -le 16 ]
then
echo "Good Afternoon"
elif [ $hours -le 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
fi

20.Write a shell script to sort the elements in a array using bubble sort
technique.

echo "enter the range"


read n
# taking input from user
echo "enter Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done
#printing the number before sorting
echo "Numbers in an array are:"
for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done
# Now do the Sorting of numbers
for (( i = 0; i < $n ; i++ ))
do
for (( j = $i; j < $n; j++ ))
do
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
27
if [ ${nos[$i]} -gt ${nos[$j]} ]
then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
# Printing the sorted number
echo "Sorted Numbers"
for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done

21.write a shell script to find largest element in a array.

echo “Enter the number of elements:”


read n
l=0
for((i = 1 ; i <= n ; i++))
do
echo “Enter the number:”
read no
if [ $no -gt $l ]
then
l=$no
fi
done
echo “The largest numbers is : $l”

22.Write an awk program to print sum,avg of students marks list.

23.Write an awk program to display students pass/fail report.

24.Write an awk program to count the no.of vowels in a given file.

25.Write an awk program which will find maximum word and its length in
the given input file.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


28
26.Write a shell script to generate the mathematical tables.

clear
echo "Type any number to generate Mathematical Table"
read number
i=1
while [ $i -le 10 ]
do
echo " $number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done

27.write a shell script to sort elements of given array by using


selection sort.

28.Write a shell script to search given number using binary search.

echo “Enter the limit:”


read n
echo “Enter the numbers”
for(( i=0 ;i<n; i++ ))
do
read m
a[i]=$m
done
for(( i=1; i<n; i++ ))
do
for(( j=0; j<n-i; j++))
do
if [ ${a[$j]} -gt ${a[$j+1]} ]
then
t=${a[$j]}
a[$j]=${a[$j+1]}
a[$j+1]=$t
fi
done
done
echo “Sorted array is”
for(( i=0; i<n; i++ ))
do
echo “${a[$i]}”
done
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
29
echo “Enter the element to be searched :”
read s
l=0
c=0
u=$(($n-1))
while [ $l -le $u ]
do
mid=$(((( $l+$u ))/2 ))
if [ $s -eq ${a[$mid]} ]
then
c=1
break
elif [ $s -lt ${a[$mid]} ]
then
u=$(($mid-1))
else
l=$(($mid+1))
fi
done
if [ $c -eq 1 ]
then
echo “Element found at position $(($mid+1))”
else
echo “Element not found”
fi

29.Write a shell script to find number of vowels, consonants, numbers,


whitespaces and special characters in a given string.

clear
echo "Type any String"
read string

length=`echo $string | wc -c`


nvowels=0
nconsonants=0
ndigits=0
nsymbol=0

while [ $length -gt 1 ]


do
length=`expr $length - 1`

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


30
h=`echo $string | cut -c $length`

case $h in
[AaEeIiOoUu])
nvowels=`expr $nvowels + 1`
;;

[BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz])
nconsonants=`expr $nconsonants + 1`
;;
[0-9]) ndigits=`expr $ndigits + 1`
;;

[@!%$,*+-\&:])
nsymbol=`expr $nsymbol + 1`
;;
esac
done

echo "Number of Vowels : $nvowels"


echo "Number of Consonants : $nconsonants"
echo "Number of Digits : $ndigits"
echo "Number of Symbols : $nsymbol"

WEEK30.Write a shell script to lock the terminal.

Clear
stty –echo
echo "enter password to lock the terminal"
read pass1
echo " Re-enter password"
read pass2
if [ "$pass1" = "$pass2" ]
then
echo "system is locked"
echo "enter password to unlock"
trap ``/1 2 3 9 15 18

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


31
while true
do
read pass3
if [ $pass1 = $pass3 ]
then echo "system unlocked"
stty echo
exit
else
echo "password mismatch"
fi
done
else
echo "password mismatch"
stty echo
fi
Output:

enter the password to lock terminal :****

re-enter the password:****

system is locked

enter the password to unlock:****

system unlocked

enter the password to lock terminal:********

re-enter the password:****

password mismatch

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


32

Viva Questions & Answers

What is a Make file?

Make file is a utility in Unix to help compile large programs. It helps by only compiling
the portion of the program that has been changed

Could you tell something about the Unix System Kernel?

The kernel is the heart of the UNIX operating system, it’s


responsible for controlling the computer’s resources and
scheduling user jobs so that each one gets its fair share of
resources.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


33
How can you tell what shell you are running on UNIX
system?

You can do the Echo $RANDOM. It will return a undefined


variable if you are from the C-Shell, just a return prompt if you
are from the Bourne shell, and a 5 digit random numbers if you
are from the Korn shell. You could also do a ps -l and look for
the shell with […]

What do you mean by u-area (user area) or u-block?

This contains the private data that is manipulated only by the


Kernel. This is local to the Process, i.e. each process is
allocated a u-area.

What scheme does the Kernel in Unix System V follow while


choosing a swap device among the multiple swap devices?

Kernel follows Round Robin scheme choosing a swap device


among the multiple swap devices in Unix System V.

List the system calls used for process management:

System calls Description


fork() To create a new process
exec() To execute a new program in a process
wait() […]

How do you change File Access Permissions?

Every file has following attributes:


owner’s user ID ( 16 bit integer )
owner’s group ID ( 16 bit integer )
File access mode word
‘r w x -r w x- r w x’
(user permission-group permission-others permission)
r-read, w-write, x-execute
To change the access mode, we use chmod(filename,mode).
Example:
To change mode of myfile to ‘rw-rw-r–’ (ie. read, write
permission for user - […]

Explain the layered aspect of a UNIX system. What are the


layers? What does it mean to say they are layers?

A UNIX system has essentially three main layers:


. The hardware
. The operating system kernel

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


34
. The user-level programs
The kernel hides the system’s hardware underneath an
abstract, high-level programming interface. It is responsible for
implementing many of the facilities that users and user-level
programs take for granted.
The kernel assembles all of the following UNIX concepts from
lower-level […]

What is the use of ‘grep’ command?

‘grep’ is a pattern search command. It searches for the pattern,


specified in the command line with appropriate option, in a
file(s).
Syntax : grep Example : grep 99mx mcafile

What difference between cmp and diff commands?

cmp - Compares two files byte by byte and displays the first
mismatch
diff - tells the changes to be made to make the files identical

What is the significance of the “tee” command?

It reads the standard input and sends it to the standard output


while redirecting a copy of what it has read to the file specified
by the user.

Is ‘du’ a command? If so, what is its use?

Yes, it stands for ‘disk usage’. With the help of this command
you can find the disk capacity and free space of the disk.

How to terminate a process which is running and the


specialty on command kill 0?

With the help of kill command we can terminate the process.


Syntax: kill pid Kill 0 - kills all processes in your system except
the login shell.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


35
Explain kill() and its possible return values.

There are four possible results from this call:


‘kill()’ returns 0. This implies that a process exists with the given
PID, and the system would allow you to send signals to it. It is
system-dependent whether the process could be a zombie.
‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with
the given PID, or […]

What does the command “ $who | sort –logfile > newfile”


do?

The input from a pipe can be combined with the input from a file
. The trick is to use the special symbol “-“ (a hyphen) for those
commands that recognize the hyphen as std input.
In the above command the output from who becomes the std
input to sort , meanwhile sort opens the file […]

What are shell variables?

Shell variables are special variables, a name-value pair created


and maintained by the shell.
Example: PATH, HOME, MAIL and TERM

How many prompts are available in a UNIX system?

Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).

Is it possible to create new a file system in UNIX?

Use ‘su’ command. The system asks for password and when
valid entry is made the user gains super user (admin) privileges.

How the Kernel handles the copy on write bit of a page,


when the bit is set?

In situations like, where the copy on write bit of a page is set


and that page is shared by more than one process, the Kernel
allocates new page and copies the content to the new page and
the other processes retain their references to the old page. After
copying the Kernel updates the page […]

Difference between the fork() and vfork() system call?


During the fork() system call the Kernel makes a copy of the parent process’s address
space and attaches it to the child process.But the vfork() system call do not makes any
copy of the parent’s address space, so it is faster than the fork() system call. The child
process as a result of the vfork() […]

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


36
How the Kernel handles the fork() system call in traditional
Unix and in the System V Unix, while swapping?

Kernel in traditional Unix, makes the duplicate copy of the


parent’s address space and attaches it to the child’s process,
while swapping. Kernel in System V Unix, manipulates the
region tables, page table, and pfdata table entries, by
incrementing the reference count of the region table of shared
regions.

What are the requirements for a swapper to work?

The swapper works on the highest scheduling priority. Firstly it


will look for any sleeping process, if not found then it will look for
the ready-to-run process for swapping. But the major
requirement for the swapper to work the ready-to-run process
must be core-resident for at least 2 seconds before swapping
out. And for swapping […]

What is Expansion swap?

At the time when any process requires more memory than it is


currently allocated, the Kernel performs Expansion swap. To do
this Kernel reserves enough space in the swap device. Then the
address translation mapping is adjusted for the new virtual
address space but the physical memory is not allocated. At last
Kernel swaps the […]

What is Fork swap?

fork() is a system call to create a child process. When the parent


process calls fork() system call, the child process is created and
if there is short of memory then the child process is sent to the
read-to-run state in the swap device, and return to the user state
without swapping the parent process. […]

What are the entities that are swapped out of the main
memory while swapping the process out of the main
memory?

All memory space occupied by the process, process’s u-area,


and Kernel stack are swapped out, theoretically.
Practically, if the process’s u-area contains the Address
Translation Tables for the process then Kernel implementations
do not swap the u-area.

Is the Process before and after the swap are the same? Give
reason.
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
37
Process before swapping is residing in the primary memory in
its original form. The regions (text, data and stack) may not be
occupied fully by the process, there may be few empty slots in
any of the regions and while swapping Kernel do not bother
about the empty slots while swapping the process outAfter
swapping […]

What are the events done by the Kernel after a process is


being swapped out from the main memory?

When Kernel swaps the process out of the primary memory, it


performs the following:
Kernel decrements the Reference Count of each region of the
process. If the reference count becomes zero, swaps the region
out of the main memory.
Kernel allocates the space for the swapping process in the swap
device.
Kernel locks the other swapping process while […]

What is major difference between the Historic Unix and the


new BSD release of Unix System V in terms of Memory
Management?

Historic Unix uses Swapping – entire process is transferred to


the main memory from the swap device, whereas the Unix
System V uses Demand Paging – only the part of the process is
moved to the main memory. Historic Unix uses one Swap
Device and Unix System V allow multiple Swap Devices

What is an advantage of executing a process in


background?

The most common reason to put a process in the background is


to allow you to do something else interactively without waiting
for the process to complete. At the end of the command you add
the special background symbol, &. This symbol tells your shell
to execute the given command in the background.
Example: cp *.* […]

What Happens when you execute a program?

When you execute a program on your UNIX system, the system


creates a special environment for that program. This
environment contains everything needed for the system to run
the program as if no other program were running on the system.
Each process has process context, which is everything that is
unique about the state of […]

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


38
What are the process states in Unix?

As a process executes it changes state according to its


circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a
signal.
Zombie : The […]

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


39
What is a zombie?

When a program forks and the child finishes before the parent,
the kernel still keeps some of its information about the child in
case the parent might need it - for example, the parent may
need to check the child’s exit status. To be able to get this
information, the parent calls `wait()‘; In the […]

How can a parent and child process communicate?

A parent and child can communicate through any of the normal


inter-process communication schemes (pipes, sockets,
message queues, shared memory), but also have some special
ways to communicate that take advantage of their relationship
as a parent and child. One of the most obvious is that the parent
can get the exit status of the […]

How can you get/set an environment variable from a


program?

Getting the value of an environment variable is done by using


`getenv()’.
Setting the value of an environment variable is done by using
`putenv()’.

Explain fork() system call.

The `fork()’ used to create a new process from an existing


process. The new process is called the child process, and the
existing process is called the parent. We can tell which is which
by checking the return value from `fork()’. The parent gets the
child’s pid returned to him, but […]

What are various IDs associated with a process?

Unix identifies each process with a unique integer called


ProcessID. The process that executes the request for creation
of a process is called the ‘parent process’ whose PID is ‘Parent
Process ID’. Every process is associated with a particular user
called the ‘owner’ who has privileges over the process. The
identification for the user is […]

Brief about the initial process sequence while the system


boots up.

While booting, special process called the ’swapper’ or


’scheduler’ is created with Process-ID 0. The swapper manages
memory allocation for processes and influences CPU allocation.
The swapper inturn creates 3 children:
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
40
the process dispatcher,vhand and dbflush with IDs 1,2 and 3
respectively.
This is done by executing the file /etc/init. Process dispatcher
gives birth to the shell. […]

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


41
What is a shell?

A shell is an interactive user interface to an operating system


services that allows an user to enter commands as character
strings or through a graphical user interface.
The shell converts them to system calls to the OS or forks off a
process to execute the command. System call results and other
information from the OS […]

How does the inode map to data block of a file?

Inode has 13 block addresses. The first 10 are direct block


addresses of the first 10 data blocks in the file. The 11th
address points to a one-level index block. The 12th address
points to a two-level (double in-direction) index block. The 13th
address points to a three-level(triple in-direction)index block.
This provides a very large maximum […]

Discuss the mount and unmount system calls

The privileged mount system call is used to attach a file system


to a directory of another file system; the unmount system call
detaches a file system. When you mount another file system on
to your directory, you are essentially splicing one directory tree
onto a branch in another directory tree. The first argument to
[…]

How do you create special files like named pipes and device
files?

The system call mknod creates special files in the following


sequence.
1. kernel assigns new inode,
2. sets the file type to indicate that the file is a pipe, directory or
special file,
3. If it is a device file, it makes the other entries like major, minor
device numbers.
For example: If the device is a disk, major […]

What are links and symbolic links in UNIX file system?

A link is a second name (not a file) for a file. Links can be used
to assign more than one name to a file, but cannot be used to
assign a directory more than one name or link filenames on
different computers.
Symbolic link ‘is’ a file that only contains the name of another
file.Operation […]

What are the Unix system calls for I/O?


FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
42
open(pathname,flag,mode) - open file
creat(pathname,mode) - create file
close(filedes) - close an open file
read(filedes,buffer,bytes) - read data from an open file
write(filedes,buffer,bytes) - write data to an open file
lseek(filedes,offset,from) - position an open file
dup(filedes) - duplicate an existing file descriptor
dup2(oldfd,newfd) - duplicate to a desired file descriptor
fcntl(filedes,cmd,arg) - change properties of an open file
ioctl(filedes,request,arg) - change the behaviour […]

What Happens when you execute a program?

When you execute a program on your UNIX system, the system


creates a special environment for that program. This
environment contains everything needed for the system to run
the program as if no other program were running on the system.
Each process has process context, which is everything that is
unique about the state of […]

What are the process states in Unix?

As a process executes it changes state according to its


circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a
signal.
Zombie : The […]

What is a zombie?

When a program forks and the child finishes before the parent,
the kernel still keeps some of its information about the child in
case the parent might need it - for example, the parent may
need to check the child’s exit status. To be able to get this
information, the parent calls `wait()‘; In the […]

How can a parent and child process communicate?

A parent and child can communicate through any of the normal


inter-process communication schemes (pipes, sockets,
message queues, shared memory), but also have some special
ways to communicate that take advantage of their relationship
as a parent and child. One of the most obvious is that the parent
can get the exit status of the […]

How can you get/set an environment variable from a


program?
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
43
Getting the value of an environment variable is done by using
`getenv()’.
Setting the value of an environment variable is done by using
`putenv()’.

Explain fork() system call.

The `fork()’ used to create a new process from an existing


process. The new process is called the child process, and the
existing process is called the parent. We can tell which is which
by checking the return value from `fork()’. The parent gets the
child’s pid returned to him, but […]

What are various IDs associated with a process?

Unix identifies each process with a unique integer called


ProcessID. The process that executes the request for creation
of a process is called the ‘parent process’ whose PID is ‘Parent
Process ID’. Every process is associated with a particular user
called the ‘owner’ who has privileges over the process. The
identification for the user is […]

Brief about the initial process sequence while the system


boots up.

While booting, special process called the ’swapper’ or


’scheduler’ is created with Process-ID 0. The swapper manages
memory allocation for processes and influences CPU allocation.
The swapper inturn creates 3 children:
the process dispatcher,vhand and dbflush with IDs 1,2 and 3
respectively.
This is done by executing the file /etc/init. Process dispatcher
gives birth to the shell. […]

What is a shell?

A shell is an interactive user interface to an operating system


services that allows an user to enter commands as character
strings or through a graphical user interface.
The shell converts them to system calls to the OS or forks off a
process to execute the command. System call results and other
information from the OS […]

How does the inode map to data block of a file?

Inode has 13 block addresses. The first 10 are direct block


addresses of the first 10 data blocks in the file. The 11th
address points to a one-level index block. The 12th address
points to a two-level (double in-direction) index block. The 13th
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
44
address points to a three-level(triple in-direction)index block.
This provides a very large maximum […]

Discuss the mount and unmount system calls

The privileged mount system call is used to attach a file system


to a directory of another file system; the unmount system call
detaches a file system. When you mount another file system on
to your directory, you are essentially splicing one directory tree
onto a branch in another directory tree. The first argument to
[…]

How do you create special files like named pipes and device
files?

The system call mknod creates special files in the following


sequence.
1. kernel assigns new inode,
2. sets the file type to indicate that the file is a pipe, directory or
special file,
3. If it is a device file, it makes the other entries like major, minor
device numbers.
For example: If the device is a disk, major […]

What is a FIFO?

FIFO are otherwise called as ‘named pipes’. FIFO (first-in-first-


out) is a special file which is said to be data transient. Once data
is read from named pipe, it cannot be read again.
Also, data can be read only in the order written. It is used in
interprocess communication where a process writes to one end
of […]

What are links and symbolic links in UNIX file system?

A link is a second name (not a file) for a file. Links can be used
to assign more than one name to a file, but cannot be used to
assign a directory more than one name or link filenames on
different computers.
Symbolic link ‘is’ a file that only contains the name of another
file.Operation […]

What are the Unix system calls for I/O?

open(pathname,flag,mode) - open file


creat(pathname,mode) - create file
close(filedes) - close an open file
read(filedes,buffer,bytes) - read data from an open file
write(filedes,buffer,bytes) - write data to an open file
FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET
45
lseek(filedes,offset,from) - position an open file
dup(filedes) - duplicate an existing file descriptor
dup2(oldfd,newfd) - duplicate to a desired file descriptor
fcntl(filedes,cmd,arg) - change properties of an open file
ioctl(filedes,request,arg) - change the behaviour […]

Brief about the directory representation in UNIX

s a file containing a correspondence between filenames and


inodes. A directory is a special file that the kernel maintains.
Only kernel modifies directories, but processes can read
directories. The contents of a directory are a list of filename and
inode number pairs. When new directories are created, kernel
makes two entries […]

What is ‘inode’?

All UNIX files have its description stored in a structure called


‘inode’. The inode contains info about the file-size, its location,
time of last access, time of last modification, permission and so
on. Directories are also represented as files and have an
associated inode. In addition to descriptions about the file, the
inode contains pointers […]

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


46
How are devices represented in UNIX?

All devices are represented by files called special files that are
located in/dev directory. Thus, device files and other files are
named and accessed in the same way. A ‘regular file’ is just an
ordinary data file in the disk. A ‘block special file’ represents a
device with characteristics similar to a disk (data transfer […]

Brief about the directory representation in UNIX

A Unix directory is a file containing a correspondence between


filenames and inodes. A directory is a special file that the kernel
maintains. Only kernel modifies directories, but processes can
read directories. The contents of a directory are a list of
filename and inode number pairs. When new directories are
created, kernel makes two entries […]

What is ‘inode’?

All UNIX files have its description stored in a structure called


‘inode’. The inode contains info about the file-size, its location,
time of last access, time of last modification, permission and so
on. Directories are also represented as files and have an
associated inode. In addition to descriptions about the file, the
inode contains pointers […]

How are devices represented in UNIX?

All devices are represented by files called special files that are
located in/dev directory. Thus, device files and other files are
named and accessed in the same way. A ‘regular file’ is just an
ordinary data file in the disk. A ‘block special file’ represents a
device with characteristics similar to a disk (data transfer […]

How is the command


“$cat file2 “
different from “$cat
>file2

The Commond $cat file in unix is used to display the content


of the file and where as commond $cat >> file is to append
the text to the end of the file without overwritting the
information of the file. Incase if the file does not exist in the
directory the commond will create a newfile in file system.
Answer

$cat >file means to create a new file $cat file means to open
an existing file.

Answered By: selva,ravi Date: 7/13/2007

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


47

cat > file it means creating file for file cat file it means used
to display the file content

Explain the steps


that a shell follows
while processing a
command.

When processing a command the searchs for the utility for


the command in the directories specified in the PATH
Answer varible and it in invokes that utility. That utility will execute
the command with help of kernel and the output is given to
shell. And then the displays out put to the user.

Explain the steps


that a shell follows
while processing a
command.

When processing a command the searchs for the utility for


the command in the directories specified in the PATH
Answer varible and it in invokes that utility. That utility will execute
the command with help of kernel and the output is given to
shell. And then the displays out put to the user.

Which command is
used to delete all
files in the current
directory and all its
sub-directories?

#rm -fr <Directory name>

# rm -rf *
Answer
Answered By: Amit Shiknis Date: 12/25/2007

rm -r *

What is the use of

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


48
the command "ls -x
chapter[1-5]"

Yes you are correct. It stands for listing the files Chapter
Answer with suffix 1 to 5 but it will display the files in columns as
with-x option.

How does the kernel


differentiate device
files and ordinary
files?

Device filles are of 2 types --- charcater device file and block
device file

type field in the file's inode structure


Answer
b--- block device file

c--- character device file

How to switch to a
super user status to
gain privileges?

Use ‘su’ command. The system asks for password and when
Answer valid entry is made the user gains super user (admin)
privileges.

What are shell


variables?

Shell variables are system environment variables.They


include

TERM,SHELL, MAIL

the output of the shell variable we can see by typing the


Answer command

$>echo $TERM

ansi

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


49
at the prompt.

What is redirection?

Redirection is a feature in Unix where the data from the


standard out put or a file,so on.can be redirected i.e divert
to a file or a program and vice versa.

Answer > -- out put redirection

>> -- out put redirectin(appending at the last)

< -- input redirection

How to terminate a
process which is
running and the
specialty on
command kill 0?

With the help of kill command we can terminate the


process.
Answer Syntax: kill pid
Kill 0 - kills all processes in your system except the login
shell.

How to terminate a
process which is
running and the
specialty on
command kill 0?

With the help of kill command we can terminate the


process.
Answer Syntax: kill pid
Kill 0 - kills all processes in your system except the login
shell.

How to sfind free


space in unix/linux

Df and du commands are used for checking free space on


Answer disk .

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


50
df -h or df -Humanreadable gives human readable format of
free space.

What is the
difference between
soft link and hard
link in unix
operating system ?

Hard Links :

1. All Links have same inode number.

2.ls -l command shows all the links with the link


column(Second) shows No. of links.

3. Links have actual file contents

4.Removing any link ,just reduces the link count , but


doesn't affect other links.

Answer Soft Links(Symbolic Links) :

1.Links have different inode numbers.

2. ls -l command shows all links with second column value 1


and the link points to original file.

3. Link has the path for original file and not the contents.

4.Removing soft link doesn't affect anything but removing


original file ,the link becomes "dangling" link which points
to nonexistant file.

to concatenate
(attach) two
strings?

For concatenating two string we use cat command.

Answer Ex:- cat str1 str2

Explain the UNIX


Kernel.

UNIX Kernel is heart of the operating system. UNIX kernal is


loaded first when UNIX system is booted. It handles
Answer allocation of devices, cpu, memory from that ponint on.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET


51
How many prompts
are available in a
UNIX system?

Unix/ Linux Supports four Prompts PS1, PS2, PS3, PS4


Answer
#,@,$,% are 4 prompts

REFERENCES:

Books:

1)Introduction to UNIX & SHELL programming, M.G. Venkatesh Murthy, Pearson Education.

2)Unix concepts and applications, Fourth Edition, Sumitabha Das, TMH.

3)Unix for programmers and users, 3rd edition, Gaham Glass & K. Ables, pearson education.

4)Unix and shell Programming –A text book, B.A. Forouzan & R.F. Giberg, Thomson.

5)Beginning shell scripting, E. Foster – Johnson & other, Wile Y- India.

FOSS LAB MANUAL :: DEPARTMENT OF IT :: ALIET

You might also like