You are on page 1of 23

Solutions to Chapter One Questions

1. Why does a computer require an operating system?


Ans.
A computer requires an operating system to control the various hardware devices and manage its own resources. The
operating system also allows users to create and manage files and run various applications. The operating system
performs the following basic tasks:
1. eripheral !anagement
". !emory !anagement
#. rocess !anagement
$. %ilesystem !anagement
&. 'ommand (nterpretation
2. )ow is *+(, different from other operating systems?
Ans.
The differences between *+(, and other operating systems are:
*+(, is a multi-user operating system. *nlike ./0 and Windows1 a number of users can
access the *+(, operating system simultaneously.
*+(, has better scalability than Windows /0.
*+(, is stable and can be operated continuously without being rebooted compared to
Windows /0.
3. What is the function of the *+(, kernel?
Ans.
The kernel1 collection of programs written in ' language1 is the core of the *+(, operating system. The kernel manages
the hardware and the e2ecuting processes. The kernel acts as an interface between the applications and the system
hardware. The application programs communicate with the hardware by using the services of the kernel.
4. What is the meaning of multitasking?
Ans.
A multitasking operating system allows a computer to run several programs at the same time. The multitasking operating
system divides the '* time among various processes and gives an impression of doing many tasks at the same time.
The concept of dividing '* time is known as time-sharing.
5. What is the importance of a user name and a password?
Ans.
A user needs to logon with the user name and password in order to start a *+(, session on a system. The user name1
which is also known as login name or login1 and the password are provided by the system administrator. (f a user does not
type his user name and password or types them incorrect1 the system does not starts the session.
.
Solutions to Chapter Two Questions
1. )ow are commands located in *ni2?
Ans.
The commands can be located using the type command in *ni2.
1 | P a g e
2. )ow does the type command work?
Ans.
The type command returns the location of the command that is passed as a parameter to the type command. %or
e2ample1
3 type who
When the above command is entered1 the shell searches for the command in the search path specified in the AT)
environmental variable. (f the command is found1 the command4s location is displayed on the console.
3. )ow do you find out whether a command is internal or e2ternal?
Ans.
The commands that are built into the shell are called internal commands. %or e2ample1 the cd command. The shell does
not start any separate process to run the internal commands.
The commands for which e2ecutable files are stored in some other directory rather then in shell itself are called e2ternal
commands. %or e2ample1 the cat command. The cat command is an e2ternal program stored in the 5bin5cat file. The shell
starts a new subprocess to e2ecute an e2ternal command.
4. What is a secondary prompt?
Ans.
0ometimes1 a command to be e2ecuted is lengthy and you need to split it in several lines. The *ni2 shell displays a
second prompt to accept the incomplete command. 6y default1 the second prompt is displayed as 7 symbol.
5. )ow do you stop and resume the scrolling of the screen display?
Ans.
8ou can stop scrolling of the screen display by pressing 'T9:-s and resume scrolling by pressing 'T9:-q keys from the
keyboard.
6. )ow do you get one-line descriptions of any command in *ni2?
Ans.
8ou can get one-line description of any command by using the whatis command in *ni2. %or e2ample1 to see the one-line
description of cat command1 you specify the following command:
3 whatis cat
2 | P a g e
Solutions to Chapter Three Questions
1. *se the cal command to find out whether the year 1;<< was a leap year.
Ans.
The following command can be used to find whether the year 1;<< is a leap year or not:
3 cal " 1;<<
As the output is showing "= days in %ebruary1 1;<< was not a leap year.
2. *se the echo command to display the message 4>ood !orning4. The cursor should move to the ne2t line after
displaying the message.
Ans.
The command to display 4>ood !orning4 using the echo command is:
3 echo ?>ood !orning?
3. .isplay the current date in the mm5dd5yy format.
Ans.
The command to display current date in mm5dd5yy format is:
3 date @?A.?
4. *se bc to divide #& by ;. 0et the scale to ".
Ans.
The following command is given for using the bc:
3 bc
scaleB"
#&5;
5. 9ecord your login session into a file.
Ans.
The following command is given to record the login session into a file:
3 script Cname of the script file7
6. :ist out the users logged on into the system.
Ans.
The command to list the current users is:
3 who
7. .isplay your terminal filename.
Ans.
The command to display the terminal file name is:
3 tty
8. .isplay the release version of your operating system.
3 | P a g e
Ans.
The command to display the release version of the operating system is:
3 uname -r
9. )ow do you check a file for spelling errors?
Ans.
A file can be checked for spelling errors by using the ispell command.
10. )ow do you lock your terminal for "< minutes?
Ans.
The command to lock the terminal for "< minutes is:
lock -"<
11. )ow do you check your terminal settings?
Ans.
8ou can check your terminal settings using the stty command.
Solutions to Chapter our Questions
1. What are the rules to be followed while naming files in *ni2?
Ans.
The rules to be followed while naming files in *ni2 are:
a The filename in *ni2 can be a combination of upper case letters1 lower case letters1 numbers1 a period D.E1 comma D1E1 or
underscore DFE.
b The filename should not contain G1 H1 I1 J1 D1 E1 K1 L1 M1 N1 31 C1 71 ?1 41 ?1 51 O1 P1 Q1 A1 R1 S1 and T.
c The *ni2 command names should not be used as filenames.
2. Which of the following file names are not valid and Why?
a. 6ooks
b. file?
c. cat
d. file name
e. book.lst
f. rankH
g. slnoT
Ans.
The invalid filenames are:
b. !ile": resence of ?.
c. #at: command name.
d. !ile na$e: resence of space.
f. ran%&: resence of H.
g. slno': resence of T.
3. 0uppose the working directory is user1 and the parent is user1 what is the absolute pathname of the file list.t2t in the
working directory user1.
Ans.
4 | P a g e
The absolute pathname of the file list.t2t is:
5user5user15list.t2t
4. What are the different types of files in *ni2?
Ans.
.ifferent types of files in *ni2 are ordinary files1 directory files1 special files1 and linked files.
5. Why is the siUe of a directory file small?
Ans.
The siUe of a directory file is small because a directory file contains the name and the i-number of each item Dfile or
subdirectoryE contained within the directory.
6. 'reate a new directory in your home directory and change to it. display the absolute path.
Ans.
The command to create a new diretory is:
3 mkdir mydir
The command to change to the new directory is:
3 cd mydir
The command to display the absolute path is:
3 pwd
7. 'an a directory be removed if it is not empty?
Ans.
+o1 a directory must be empty1 if you want to delete it.
8. What are hidden files and how can they be displayed?
Ans.
The files whose name begin with a dot D.E character is known as hidden file. The system files1 usually1 are hidden files.
8ou can display hidden files using the ls -a or ls -A commands.
S
Solutions to Chapter i(e Questions
1. 'reate a file called lesson1 in your home directory using the cat command.
Ans.
The command to create the file1 lesson1 is:
3 cat 7 lesson1
This is a sample file. C'T9:7@.
2. 'reate a directory called lessons and copy the file lesson1 to this directory.
Ans.
The command to create a directory is:
3 mkdir lessons
The command to copy the file1 lesson1 to the directory1 lessons is:
3 cp lesson1 lessons
5 | P a g e
3. 9emove the file lesson1 from the home directory.
Ans.
The command to remove the file1 lesson1 from the home directory is:
3 rm lesson1
4. 'reate another file called lesson" and move it to the directory lessons.
Ans.
The command to create the file1 lesson" is:
3 cat 7 lesson"
This is another sample file. C'T9:7@.
The command to move the file1 lesson" to the directory1 lessons is:
3 mv lesson" lessons
5. 'hange to the directory1 lessons1 and count the number of characters and lines in the files lesson1 and lesson".
Ans.
The command to change to the directory1 lessons is:
3 cd lessons
The command to counting the number of lines and characters in both the files is:
3 wc -cl lesson1 lesson"
6. What happens when lesson1 and lesson" are compared?
Ans.
When the lesson1 and lesson" files are compared using the cmp command1 the system displays no result1 if the content
of the files are same. (f the content of the files are different1 then the byte and line number are displayed where the first
difference occurs.
7. :ist the contents of the directory lessons datewise.
Ans.
The command to list the content of the directory lessons datewise is:
3 ls -lt
8. 'reate a file with at least 1<< bytes and split it into files of "< bytes each.
Ans.
The command to create a file1 myfile is:
3 cat 7 myfile
A dangling symbolic link is a symbolic linked file1 whose source file has been deleted. %or e2ample1 if you delete the
sample.t2t file from your system1 the samplelnk.t2t will become a dangling link. C'T9:7@.
The command to split myfile into files of "< bytes each is:
3 split -b "< myfile mfile
Solutions to Chapter Si) Questions
1. )ow do you list all the attributes of a file in your home directory?
6 | P a g e
Ans.
The command to list the attributes of a file in a home directory are:
3 ls -l Cfilename7
2. 'reate a file called sample.t2t. )ow do you assign all permissions to the owner1 read and write permissions to the
group1 and no permissions to others using relative permissions and absolute permissions?
Ans.
The command to create the sample.t2t file is:
3 cat 7 sample.t2t
Ctype the te2t7Cpress 'T9: @c7
The command to assign the permissions using relative permissions is:
3 chmod u@rw21g@rw1o-rw2 sample.t2t
The command to assign the permissions using absolute permissions is:
3 chmod VW< sample.t2t
3. What happens if a directory has permissions VVV?
Ans.
(f a directory has VVV permissions1 all the users have read1 write1 and e2ecute permissions on that particular directory.
They can add1 delete1 or modify any file present in that directory.
4. )ow are default file and directory permissions changed?
Ans.
The default file and directory permissions can be changed using the umask command. %or e2ample1
umask <""
The above command sets the following permissions on a newly created files:
9ead and Write permissions for file owner.
9ead permission for group owner.
9ead permission for other users.
5. What are the changes seen in the file4s inode number when a link is created?
Ans.
When a link of a file is created1 the inode number of the e2isting file is assigned to the new created linked file. 6oth linked
files will have the same inode number.
6. )ow are inode numbers displayed? (f two files have the same inode numbers1 what do you conclude? What happens
when you delete one of these files?
Ans.
The inode numbers of the files can be displayed using ls -il command. 0ame inode number of two files indicates that the
files are linked files. (f a linked file is deleted1 the second file still remains and is not deleted from the system.
7. )ow are hard links created?
Ans.
The hard links are created using the ln command.
8. What are the uses of hard links?
Ans.
The uses of hard links are:
7 | P a g e
a. )ard links prevent accidental deletion of files.
b. )ard links can make the files available to the users1 when files in a directory are shifted to another directory.
9. %or the file sample.t2t1 create a symbolic link. What is a dangling symbolic link?
Ans.
The command to create a symbolic link for the file1 sample.t2t is:
3 ln -s sample.t2t samplelnk.t2t
A dangling symbolic link is a symbolic linked file1 whose source file1 to which symbolic linked file is linked1 has been
deleted. %or e2ample1 if you delete the sample.t2t file from your system1 the samplelnk.t2t will become a dangling link.
10. )ow do you order the list of files by their modification time?
Ans.
The command to list the files based on their modification time is:
3 ls -t
11. %ind all the directories in the 5usr directory?
Ans.
The command to find all the directories in the 5usr directory is:
find 5usr -type d -print
12. %ind all the files having the same inode number in the 5usr directory?
Ans.
The command to find the files having same inode number in the 5usr directory is:
find 5usr -inum Cinode number7
13. %ind all the files called core in your home directory and remove it?
Ans.
The command to finding the files called core in your home directory and removing it is:
find 3)/!X -name core -e2ec rm MN IO
Solutions to Chapter Se(en Questions
1. )ow do you do the following using vi:
a. delete a line
b. overwrite te2t
c. move the cursor four lines down
d. add a new line above a line of te2t
e remove two lines from the end of a te2t and add it to the beginning of the file
f. open a file and move to the fifteenth line in the file
Ans.
a. dd
b. 9
c. $Y
d. /
e. Zeep the cursor at the second last line and in the escape mode press "81 press 1>1 and press .
8 | P a g e
f. Type vi Cfile name7 at the shell prompt1 press 1&> in the escape mode.
2. What are the different ways of adding te2t using vi?
Ans.
*sing vi1 you can add te2t in a file using the following commands:
a. i
b. a
c. (
d. A
e. o
f. /
3. )ow do you replace all the occurrences of the word printf with fprintf in a file?
Ans.
The following command needs to be given in the vi editor in escape mode to replace the word printf with fprintf in a file:
:11 3s5printf5fprintf5gp
4. What are the different ways of coming out of vi after saving the file?
Ans.
There are three different ways to quit vi after saving the file:
a. :wq
b. :2
c. [[
5. )ow do you yank and paste lines?
Ans.
To yank and paste the lines1 perform the following steps:
a. Zeep the cursor at the line you want to copy.
b. ress 8 or Cn781 where n is the total number of lines that you want to copy.
c. lace the cursor at the line1 where you want to paste the copied lines.
d. ress p to paste the line after the cursor or press to paste the lines before the cursor.
9 | P a g e
Solutions to Chapter *i+ht Questions
1. What are the file descriptor for standard out1 standard in1 and standard error?
Ans.
The file descriptor for standard out1 standard in1 and standard error are 11 <1 and " respectively.
2. X2plain the command: cat7out.dat.
Ans.
When you type the command cat7out.dat on the prompt1 it waits for the standard input to be entered. (nstead of displaying
the standard input on the monitor1 it redirects the output to the file1 out.dat.
3. Write a command to pipe the output of cat into the more command.
Ans.
3 cat Cname of the file whose content to be displayed pagewise7 J more
4. Write a command to append the file t2t1 to the end of the file t2t".
Ans.
3 cat t2t1 77 t2t"
5. What does the character I mean to the shell?
Ans.
The I DescapeE character is used to specify that the character1 written after it1 does not have any special meaning. The
character that follows the escape character should be displayed as literal. %or e2ample1
3 echo IH
The above command will display H on the standard output device.
6. )ow can you remove a file named ?ops without removing the file wops?
Ans.
3 rm KPwLops
7. )ow do you display the value of the shell variable AT)? What is the variable used for?
Ans.
The command to display the value of AT) variable is:
3 echo 3AT)
The AT) variable specifies the directories to be searched for the e2ecutable files1 when a command needs to be
e2ecuted.
8. )ow do you find out the number of users logged in to the system?
Ans.
The command to find the number of users logged into the system is:
3 who J wc -l
Solutions to Chapter ,ine Questions
1. What command will give you a listing of all running processes that are owned by you?
Ans.
10 | P a g e
The command to listing all the running processes is:
3 ps
2. What is the name and (. of your shell process?
Ans.
X2ecute the ps command and note the name and (. of your shell process. %or e2ample1
3 ps
(. TT8 T(!X '!.
"&W= pts5" <<:<<:<1 bash
1V;$" pts5" <<:<<:<< cat
1=#=$ pts5" <<:<<:<< ps
11 | P a g e
3. )ow can you tell if a process is linked to a terminal?
Ans.
8ou can use the ps command with -e option to know whether a process is linked to a terminal or not. The processes that
are not linked to any terminal contain ? in the terminal column.
4. )ow do you run a process in the background?
Ans.
To run a process in the background1 you need to append G symbol to the command. %or e2ample1
3 wc 9eport G
The above command will count the characters1 words1 and lines in the 9eport file in the background.
5. )ow can you terminate a running process?
Ans.
8ou can terminate a running process by using the kill command. With the kill command1 you need to specify the (. of the
process that you want to terminate.
6. /pen another terminal session. %ind the pid of this process and kill it. What command did you use?
Ans.
1. 0tart another terminal session by logging in as a different user.
". *se ps -au2 command to see the pid of the process.
#. *se kill -; command to kill the process.
7. What is the significance of -; in the command: kill -; pid?
Ans.
The -; option of the kill command is used to kill the process forcefully because some processes ignore the kill signal.
8. What is a daemon process?
Ans.
The processes that are not associated with any terminal are called daemon processes.
9. )ow do you kill a process running in the background?
Ans.
8ou can kill the process running in the background using the kill command. 8ou need to specify the (. with the kill
command. The status of the background process can be checked using the ps command.
10. What does the following crontab entry mean?
#< 1& H H H sort file1.lst 7 file1.srt
Ans.
(n the above command1 #< represent minutes and 1& represents hours. The whole command means that everyday at #:#<
!1 the content of file1.lst will be sorted and saved in the file1 file1.srt.
Solutions to Chapter Ten Questions
12 | P a g e
1. What is the difference between write and talk commands?
Ans.
The write command copies the te2t from one user terminal to the terminal of another user who is also logged on the *ni2
operating system. The recipient can reply the sender by writing on his terminal and communication starts between the two
users. The communication continues until an end-of-file is read from the terminal or an interrupt is sent.
The talk command is a visual communication program that copies lines from a user terminal to the terminal of another
user. The talk command splits the user terminal into two sections. The upper section is used by the sender to write
messages and the lower section displays the received messages.
2. (f a user is logged into more than one terminal1 how do you send the user a message using write?
Ans.
(f a user is logged into more than one terminal1 the ttyname is used to indicate the appropriate terminal.
3. %ind a user who has an account on the system and send a message using mail2?
Ans.
The command to find a user who can send a message using mail2 is:
who -T
4. What happens if you type q at the mail2 prompt?
Ans.
Typing q at the mail2 prompt enables you to quit the mail2.
5. Where is a user4s personal mailbo2 located?
Ans.
A user4s personal mailbo2 is located in the user4s home directory.
6. *sing the redirection operator on the command line1 mail the file mesg.t2t to user 1.
Ans.
The command to mail the file mesg.t2t to user1is:
mail2 user1 C mesg.t2t
7. (nclude the subYect 4my message4 in the command line1 in e2ercise no. W.
Ans.
The command to mail the file mesg.t2t to user1 with the subYect 4my message4is:
mail2 -s ?my message? user1 C mesg.t2t
8. )ow is pine different from mail2?
Ans.
The mail2 is a command line tool that is used to send or receive messages at the shell prompt. (n the sending mode1 mail2
runs in an interactive mode and asks for the subYect and message to be sent to the receiver.
The pine is a character-based menu driven mailer that also provides an address bo2 to maintain the mail addresses list.
The incoming and outgoing messages are stored in different folders. The pine main menu window displays various
options1 such as viewing help on pine1 composing a new message1 creating address book1 and configuring pine.
9. )ow do you create an address book using pine?
Ans.
13 | P a g e
To create an address book using pine1 you need to press the key 4A4 to enter the address book1 while the pine main menu
window is displayed.
14 | P a g e
10. 0end a mail to user1 using pine.
Ans.
The steps to send a mail using pine are:
$. Type pine at the shell prompt
&. ress the key ' to compose and send a message.
W. Type the mail address of user1 in the line labeled as 4To4 .
V. Type the subYect of your mail in the line labeled as 40ubYect4.
=. Type your message in the !essage Te2t area.
;. ress 'T9:@, to send the message.
1<. ress 484 at the confirmation to send the message to user1.
Solutions to Chapter *le(en Questions
1. *se head and tail to select lines & to 1< of a file.
Ans.
The command to display the lines &-1< of a file is:
3 tail @& Cfile name7 J head -n W
2. >enerate a code list by selecting departments from emp.lst.
Ans.
The command to select departments from the emp.lst file is:
3 cut -d IJ -f # emp.lst
(n the above command1 # represents the column number of the department field of the emp.lst file.
+ote: The content of the emp.lst file is given in the chapter.
3. 9emove the repeated lines from a file.
Ans.
The command to remove the repeated lines from a file is:
sort Cname of the file from which repeated lines are to be removed7 J uniq - Cnew file name7
4. 'onvert the content of the file emp.lst to uppercase.
Ans.
The command to convert the content of the file emp.lst to upper case is:
3 tr 4Ka-UL4 4KA-[L4 C emp.lst
5. )ow will you find out the number of times the character occurs in a file?
Ans.
3 cat Cfilename7 J grep -o ??? J wc -l
6. !ail a sorted list of users currently logged in to root1 taking account of the possibility that a user may be logged in more
than once.
Ans.
3 who J sort J uniq -u 7 userlist
3 mail root C userlist
15 | P a g e
Solutions to Chapter Twel(e Questions
1. .efine each of the following termsO
0hell script1 comment1 subshell
'hild process1 positional parameter1 selection structure
.efault case1 repetition structure1 iteration
loop
Ans.
Shell s#ript: (s a script or program written for the shell of a *+(,. The name of the file can
later be used to e2ecute the stored sequence with a single command. (n !0-./01 these
files are known as ?batch files?1 which are recogniUed by the .bat file name e2tension. (n
*+(,1 they are called ?shell scripts? and do not require special file name e2tension.
Co$$ent: A comment is a part of a source code that e2plains the working of a code.
'omments are ignored by compilers and interpreters. A comment in a ost0cript is the te2t1
which is preceded by a 4A4 sign. The comment in which 4AQ4 is included as the first two
characters marks the file as a ost0cript code.
Su-shell: The shell is a utility so you can e2ecute the shell command1 such as sh for the
6ash shell1 to create another shell. This new shell is known as the subshell or the child shell
of the current shell. The shell creates a child shell1 sub-shell1 to e2ecute a shell script. This
is done so that the current shell is not affected by the script. The shell script is passed to
the child shell for e2ecution. The new shell that is created is terminated as soon as the
script running on it completes e2ecution.
Chil. pro#ess: A child process is a computer process1 which is created by an e2isting
process. The e2isting process is called the parent process.
/ositional para$eter: A positional parameter is a subroutine parameter identified by the
order in which it is listed. ositional parameters are enclosed in parentheses and are
separated by commas in a subroutine call. ositional parameters are listed in the order in
which they appear in the corresponding subroutine definition.
Sele#tion stru#ture: The selection structure gives a choice to choose one options based
on a condition from a set of options. %or e2ample1 if-else and case construct.
0e!ault #ase: (n a case statement1 the default case enables you to specify the code to be
e2ecuted when the e2pression does not match any of the specified cases. The default case
corresponds to an unrecogniUed case.
1epetition stru#ture: The repetition structure is used for carrying out commands more
than once. %or e2ample1 for1 while1 and until.
2teration: (teration refers to the use of a loop to repeat one or more actions.
3oop: :oop refers to the e2ecutable statements repeated a several times in a program1
such as ./-loop1 an (%-statement.
2. .evelop a script logic that allows only kumar and sachin Dlogin namesE to e2ecute a program.
Ans.
TQ5bin5sh
T Allow kumar and sachin to e2ecute a program
uBSwhoamiS
if test 3u BB ?kumar? -o 3u BB ?sachin?
then
set SdateS
echo ?9emember for today?
case 31 in
!onE echo ?lan the week.?OO
TueE echo ?Take clothes to the cleaners.?OO
WedE echo ? Attend group meeting.?OO
ThuE echo ?!ake plans for the weekend.?O
16 | P a g e
echo ?ick up the clothes from the cleaners.?OO
%riE echo ?Answer e-mail.?OO
0atE echo ?8ou should not be here working.?O
Xcho ?%inish your work and log off.?OO
0unE echo ?'all >randma and >randpa.?OO
esac
else
echo ?8ou are not allowed to run this script.?
fi
3. .evelop a script logic that e2ecutes script only from terminal tty<1 and tty<".
Ans.
TQ5bin5sh
T Allows script to be e2ecuted from terminal tty<1 and tty<"
uBSttyS
if test 3u BB ?tty<1? -o 3u BB ?tty<"?
then
set SdateS
echo ?9emember for today?
case 31 in
!onE echo ?lan the week.?OO
TueE echo ?Take clothes to the cleaners.?OO
WedE echo ? Attend group meeting.?OO
ThuE echo ?!ake plans for the weekend.?O
echo ?ick up the clothes from the cleaners.?OO
%riE echo ?Answer e-mail.?OO
0atE echo ?8ou should not be here working.?O
echo ?%inish your work and log off.?OO
0unE echo ?'all >randma and >randpa.?OO
esac
else
echo ?The script cannot be e2ecuted on this terminal.?
fi
4. Write a shell script that accepts one or more filenames as arguments1 and converts them all to uppercase provided they
e2ist in the current directory.
Ans.
TQ5bin5sh
T Translating files into uppercase
iB1
for name in 3H
do
if test -f 3name
17 | P a g e
then
echo ?3name in upper case:?
aBSecho 3name J tr ?Ka-UL? ?KA-[L?S
mv 3name 3a
else
echo ?%ile does not e2ist.?
fi
done
5. Write a shell script that accepts two directory names1 foo1 and foo"1 and deletes those files in foo" which are identical
to their namesakes in foo1.
Ans.
TQ5bin5sh
T.eleting identical files from a directory
echo ?Xnter two directories?
read d1
read d"
if test Q -d 3d1 -o Q -d 3d"
then
echo ?8ou have entered wrong directory names?
else
for name in Sls 3d"S
do
for na in Sls 3d1S
do
if test 3name BB 3na
then
UB4y4
break
else
UB4n4
fi
done
if test 3U QB 4n4
then
echo ?.eleting 3name?
rm 3d"53name
else
echo ?3name does not match with any file in 3d1?
fi
done
fi
18 | P a g e
6. Write a shell script that lists files by modification time when called with lm and by access time called with la. 6y default1
the script should show the listing of all files in the current directory.
Ans.
TQ5bin5sh
T :isting files by modification time and access time
sB31
if test ?3s? BB ?lm?
then
ls -lt
elif test ?3s? BB ?la?
then
ls -lu
else
ls -l
fi
19 | P a g e
7. Write a shell script to display the processes in the system every #< seconds five times using a DiE while loop DiiE for loop.
Ans.
DiE The shell script using the while loop is:
TQ5bin5sh
T .isplaying current processes in the system every #< seconds
iB<
while test 3i -lt &
do
ps
sleep #<
iBSe2pr 3i @ 1S
done
DiiE The shell script using the for loop is:
TQ5bin5sh
T .isplaying current processes in the system every #< seconds
for i in 1 " # $ &
do
ps
sleep #<
done
Solutions to Chapter Thirteen Questions
1. Write a shell script modch.sh that uses chmod to change the access permissions on a file so that only the owner may
read1 write1 or e2ecute it. 6e sure to label the output to show what was done to the file.
Ans.
TQ5bin5sh
T:abel the output of chmod
echo ?Xnter the name of the file1 whose permissions you want to change?
read filename
echo ?6efore changing the file permissions1 %ile permissions for the owner of the 31 file are: Sls -l 3filenameJtr -s ? ?Jcut -c
"1#1$S?
echo ?6efore changing the file permissions1 %ile permissions for the group owner of the 31 file are: Sls -l 3filenameJtr -s ? ?J
cut -c &1W1VS?
echo ?6efore changing the file permissions1 %ile permissions for the other users of the 31 file are: Sls -l 3filenameJtr -s ? ?J
cut -c =1;11<S?
echo ?--------'hanging the file permissions--------?
chmod u@rw21go-rw2 3filename
echo ?After changing the file permissions1 %ile permissions for the owner of the 31 file are: Sls -l 3filenameJtr -s ? ?Jcut -c
"1#1$S?
echo ?After changing the file permissions1 %ile permissions for the group owner of the 31 file are: Sls -l 3filenameJtr -s ? ?J
cut -c &1W1VS?
echo ?After changing the file permissions1 %ile permissions for the other users of the 31 file are: Sls -l 3filenameJtr -s ? ?Jcut
-c =1;11<S?
20 | P a g e
2. !odify the del script so that it detects whether the user has specified a directory to be deleted1 in which case the script
should call the rmdir command. D)int: use the test with the -d option to test for a directory.E
11. 9ewrite tickle to use an if5then5elif5.....5fi structure.
1". 9ewrite echo.sh to use an until loop.
Ans.
The del script:
TQ5bin5sh
T.elete a file or directory interactively
filenameB31
if test -f 3filename
then
echo ?.o you want to delete I?3filenameI???
read choice
if test 3choice BB y
then
rm 3filename
echo ?I?3filenameI? deleted.?
else
echo ?I?3filenameI? not deleted.?
fi
elif test -d 3filename
then
echo ?.o you want to delete I?3filenameI???
read choice1
if test 3choice1 BB y
then
echo ?.o you want to delete the subdirectories also??
read ans
if test ?3ans? BB ?y?
then
rm -r 3filename
else
echo ?I?3filenameI? not deleted.?
fi
fi
else
echo ?I?3filenameI? does not e2ist.?
fi
1. The ti#%le s#ript:
TQ5bin5sh
T A daily reminder service
set SdateS
21 | P a g e
echo ?9emember for today?
if K 31 BB ?!on? L
then
echo ?lan the week.?O
elif K 31 BB ?Tue? L
then
echo ?Take clothes to the cleaners.?O
elif K 31 BB ?Wed? L
then
echo ? Attend group meeting.?O
elif K 31 BB ?Thu? L
then
echo ?!ake plans for the weekend.?O
echo ?ick up clothes at the cleaners.?O
elif K 31 BB ?%ri? L
then
echo ?Answer e-mail.?O
elif K 31 BB ?0at? L
then
echo ?8ou should not be here working.?O
echo ?%inish your work and log off.?O
elif K 31 BB ?0un? L
then
echo ?'all >randma and >randpa.?O
else
echo ?(nvalid day?O
fi
". The e#ho.sh s#ript:
TQ5 bin5sh
T Xcho a line 4n4 times
countB31
shift
messageB3H
until test 3count -eq <
do
echo 3message
countBSe2pr 3count - 1S
done
#. The lshea..sh s#ript:
T:abeling the output of ls -l command
echo
22 | P a g e
echo ?.isplaying the output of ls-l?
echo
ls -l J grep K-rw2L 7 try
echo ?%A :inks /wner >roup owner 0iUe .ate-Time %ilename?
cat try J more
23 | P a g e

You might also like