Sei sulla pagina 1di 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. Peripheral Management
2. Memory Management
3. Process Management
4. Filesystem Management
5. Command Interpretation

2. How is UNIX different from other operating systems?


Ans.
The differences between UNIX and other operating systems are:

 UNIX is a multi-user operating system. Unlike DOS and Windows, a number of users can
access the UNIX operating system simultaneously.
 UNIX has better scalability than Windows OS.
 UNIX is stable and can be operated continuously without being rebooted compared to
Windows OS.

3. What is the function of the UNIX kernel?


Ans.
The kernel, collection of programs written in C language, is the core of the UNIX operating system. The kernel manages
the hardware and the executing 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 CPU time among various processes and gives an impression of doing many tasks at the same time.
The concept of dividing CPU 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 UNIX session on a system. The user name,
which is also known as login name or login, and the password are provided by the system administrator. If a user does not
type his user name and password or types them incorrect, the system does not starts the session.

Solutions to Chapter Two Questions


1. How are commands located in Unix?
Ans.
The commands can be located using the type command in Unix.

1|Page
2. How 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. For
example,
$ type who
When the above command is entered, the shell searches for the command in the search path specified in the PATH
environmental variable. If the command is found, the command's location is displayed on the console.

3. How do you find out whether a command is internal or external?


Ans.
The commands that are built into the shell are called internal commands. For example, the cd command. The shell does
not start any separate process to run the internal commands.
The commands for which executable files are stored in some other directory rather then in shell itself are called external
commands. For example, the cat command. The cat command is an external program stored in the /bin/cat file. The shell
starts a new subprocess to execute an external command.

4. What is a secondary prompt?


Ans.
Sometimes, a command to be executed is lengthy and you need to split it in several lines. The Unix shell displays a
second prompt to accept the incomplete command. By default, the second prompt is displayed as > symbol.

5. How do you stop and resume the scrolling of the screen display?
Ans.
You can stop scrolling of the screen display by pressing CTRL-s and resume scrolling by pressing CTRL-q keys from the
keyboard.

6. How do you get one-line descriptions of any command in Unix?


Ans.
You can get one-line description of any command by using the whatis command in Unix. For example, to see the one-line
description of cat command, you specify the following command:
$ whatis cat

2|Page
Solutions to Chapter Three Questions
1. Use the cal command to find out whether the year 1900 was a leap year.
Ans.
The following command can be used to find whether the year 1900 is a leap year or not:
$ cal 2 1900
As the output is showing 28 days in February, 1900 was not a leap year.

2. Use the echo command to display the message 'Good Morning'. The cursor should move to the next line after
displaying the message.
Ans.
The command to display 'Good Morning' using the echo command is:
$ echo "Good Morning"
3. Display the current date in the mm/dd/yy format.
Ans.
The command to display current date in mm/dd/yy format is:
$ date +"%D"

4. Use bc to divide 35 by 9. Set the scale to 2.


Ans.
The following command is given for using the bc:
$ bc
scale=2
35/9

5. Record your login session into a file.


Ans.
The following command is given to record the login session into a file:
$ script <name of the script file>

6. List out the users logged on into the system.


Ans.
The command to list the current users is:
$ who

7. Display your terminal filename.


Ans.
The command to display the terminal file name is:
$ tty

8. Display the release version of your operating system.

3|Page
Ans.
The command to display the release version of the operating system is:
$ uname -r

9. How do you check a file for spelling errors?


Ans.
A file can be checked for spelling errors by using the ispell command.

10. How do you lock your terminal for 20 minutes?


Ans.
The command to lock the terminal for 20 minutes is:
lock -20
11. How do you check your terminal settings?
Ans.
You can check your terminal settings using the stty command.

Solutions to Chapter Four Questions


1. What are the rules to be followed while naming files in Unix?
Ans.
The rules to be followed while naming files in Unix are:
a The filename in Unix can be a combination of upper case letters, lower case letters, numbers, a period (.), comma (,), or
underscore (_).
b The filename should not contain &, *, \, |, (, ), [, ], {, }, $, <, >, ?, ', ", /, ;, ^, !, %, ~, `, and #.
c The Unix command names should not be used as filenames.

2. Which of the following file names are not valid and Why?
a. Books
b. file?
c. cat
d. file name
e. book.lst
f. rank*
g. slno#
Ans.
The invalid filenames are:
b. file?: Presence of ?.
c. cat: command name.
d. file name: Presence of space.
f. rank*: Presence of *.
g. slno#: Presence of #.

3. Suppose the working directory is user1 and the parent is user, what is the absolute pathname of the file list.txt in the
working directory user1.
Ans.

4|Page
The absolute pathname of the file list.txt is:
/user/user1/list.txt

4. What are the different types of files in Unix?


Ans.
Different types of files in Unix are ordinary files, directory files, special files, and linked files.

5. Why is the size of a directory file small?


Ans.
The size of a directory file is small because a directory file contains the name and the i-number of each item (file or
subdirectory) contained within the directory.
6. Create a new directory in your home directory and change to it. display the absolute path.
Ans.
The command to create a new diretory is:
$ mkdir mydir
The command to change to the new directory is:
$ cd mydir
The command to display the absolute path is:
$ pwd

7. Can a directory be removed if it is not empty?


Ans.
No, a directory must be empty, 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 (.) character is known as hidden file. The system files, usually, are hidden files.
You can display hidden files using the ls -a or ls -A commands.

Solutions to Chapter Five Questions


1. Create a file called lesson1 in your home directory using the cat command.
Ans.
The command to create the file, lesson1 is:
$ cat > lesson1
This is a sample file. <CTRL>+D

2. Create a directory called lessons and copy the file lesson1 to this directory.
Ans.
The command to create a directory is:
$ mkdir lessons
The command to copy the file, lesson1 to the directory, lessons is:
$ cp lesson1 lessons

5|Page
3. Remove the file lesson1 from the home directory.
Ans.
The command to remove the file, lesson1 from the home directory is:
$ rm lesson1

4. Create another file called lesson2 and move it to the directory lessons.
Ans.
The command to create the file, lesson2 is:
$ cat > lesson2
This is another sample file. <CTRL>+D
The command to move the file, lesson2 to the directory, lessons is:
$ mv lesson2 lessons
5. Change to the directory, lessons, and count the number of characters and lines in the files lesson1 and lesson2.
Ans.
The command to change to the directory, lessons is:
$ cd lessons
The command to counting the number of lines and characters in both the files is:
$ wc -cl lesson1 lesson2

6. What happens when lesson1 and lesson2 are compared?


Ans.
When the lesson1 and lesson2 files are compared using the cmp command, the system displays no result, if the content
of the files are same. If the content of the files are different, then the byte and line number are displayed where the first
difference occurs.

7. List the contents of the directory lessons datewise.


Ans.
The command to list the content of the directory lessons datewise is:
$ ls -lt

8. Create a file with at least 100 bytes and split it into files of 20 bytes each.
Ans.
The command to create a file, myfile is:
$ cat > myfile
A dangling symbolic link is a symbolic linked file, whose source file has been deleted. For example, if you delete the
sample.txt file from your system, the samplelnk.txt will become a dangling link. <CTRL>+D
The command to split myfile into files of 20 bytes each is:
$ split -b 20 myfile mfile

Solutions to Chapter Six Questions


1. How do you list all the attributes of a file in your home directory?

6|Page
Ans.
The command to list the attributes of a file in a home directory are:
$ ls -l <filename>

2. Create a file called sample.txt. How do you assign all permissions to the owner, read and write permissions to the
group, and no permissions to others using relative permissions and absolute permissions?
Ans.
The command to create the sample.txt file is:
$ cat > sample.txt
<type the text><press CTRL +c>
The command to assign the permissions using relative permissions is:
$ chmod u+rwx,g+rw,o-rwx sample.txt
The command to assign the permissions using absolute permissions is:
$ chmod 760 sample.txt

3. What happens if a directory has permissions 777?


Ans.
If a directory has 777 permissions, all the users have read, write, and execute permissions on that particular directory.
They can add, delete, or modify any file present in that directory.
4. How are default file and directory permissions changed?
Ans.
The default file and directory permissions can be changed using the umask command. For example,
umask 022
The above command sets the following permissions on a newly created files:

 Read and Write permissions for file owner.


 Read permission for group owner.
 Read permission for other users.

5. What are the changes seen in the file's inode number when a link is created?
Ans.
When a link of a file is created, the inode number of the existing file is assigned to the new created linked file. Both linked
files will have the same inode number.
6. How are inode numbers displayed? If two files have the same inode numbers, 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. Same inode number of two files indicates that the
files are linked files. If a linked file is deleted, the second file still remains and is not deleted from the system.

7. How 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|Page
a. Hard links prevent accidental deletion of files.
b. Hard links can make the files available to the users, when files in a directory are shifted to another directory.

9. For the file sample.txt, create a symbolic link. What is a dangling symbolic link?
Ans.
The command to create a symbolic link for the file, sample.txt is:
$ ln -s sample.txt samplelnk.txt
A dangling symbolic link is a symbolic linked file, whose source file, to which symbolic linked file is linked, has been
deleted. For example, if you delete the sample.txt file from your system, the samplelnk.txt will become a dangling link.
10. How do you order the list of files by their modification time?
Ans.
The command to list the files based on their modification time is:
$ ls -t
11. Find all the directories in the /usr directory?
Ans.
The command to find all the directories in the /usr directory is:
find /usr -type d -print

12. Find all the files having the same inode number in the /usr directory?
Ans.
The command to find the files having same inode number in the /usr directory is:
find /usr -inum <inode number>

13. Find 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 $HOME -name core -exec rm {} \;

Solutions to Chapter Seven Questions


1. How do you do the following using vi:
a. delete a line
b. overwrite text
c. move the cursor four lines down
d. add a new line above a line of text
e remove two lines from the end of a text 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. R
c. 4j
d. O
e. Keep the cursor at the second last line and in the escape mode press 2Y, press 1G, and press P.

8|Page
f. Type vi <file name> at the shell prompt, press 15G in the escape mode.

2. What are the different ways of adding text using vi?


Ans.
Using vi, you can add text in a file using the following commands:
a. i
b. a
c. I
d. A
e. o
f. O

3. How 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:
:1, $s/printf/fprintf/gp

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. :x
c. ZZ
5. How do you yank and paste lines?
Ans.
To yank and paste the lines, perform the following steps:
a. Keep the cursor at the line you want to copy.
b. Press Y or <n>Y, where n is the total number of lines that you want to copy.
c. Place the cursor at the line, where you want to paste the copied lines.
d. Press p to paste the line after the cursor or press P to paste the lines before the cursor.

9|Page
Solutions to Chapter Eight Questions
1. What are the file descriptor for standard out, standard in, and standard error?
Ans.
The file descriptor for standard out, standard in, and standard error are 1, 0, and 2 respectively.

2. Explain the command: cat>out.dat.


Ans.
When you type the command cat>out.dat on the prompt, it waits for the standard input to be entered. Instead of displaying
the standard input on the monitor, it redirects the output to the file, out.dat.

3. Write a command to pipe the output of cat into the more command.
Ans.
$ cat <name of the file whose content to be displayed pagewise> | more

4. Write a command to append the file txt1 to the end of the file txt2.
Ans.
$ cat txt1 >> txt2

5. What does the character \ mean to the shell?


Ans.
The \ (escape) character is used to specify that the character, written after it, does not have any special meaning. The
character that follows the escape character should be displayed as literal. For example,
$ echo \*
The above command will display * on the standard output device.
6. How can you remove a file named ?ops without removing the file wops?
Ans.
$ rm [^w]ops

7. How do you display the value of the shell variable PATH? What is the variable used for?
Ans.
The command to display the value of PATH variable is:
$ echo $PATH
The PATH variable specifies the directories to be searched for the executable files, when a command needs to be
executed.

8. How 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:
$ who | wc -l

Solutions to Chapter Nine 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:
$ ps

2. What is the name and PID of your shell process?


Ans.
Execute the ps command and note the name and PID of your shell process. For example,
$ ps
PID TTY TIME CMD
2568 pts/2 00:00:01 bash
17942 pts/2 00:00:00 cat
18384 pts/2 00:00:00 ps

11 | P a g e
3. How can you tell if a process is linked to a terminal?
Ans.
You 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. How do you run a process in the background?


Ans.
To run a process in the background, you need to append & symbol to the command. For example,
$ wc Report &
The above command will count the characters, words, and lines in the Report file in the background.

5. How can you terminate a running process?


Ans.
You can terminate a running process by using the kill command. With the kill command, you need to specify the PID of the
process that you want to terminate.

6. Open another terminal session. Find the pid of this process and kill it. What command did you use?
Ans.
1. Start another terminal session by logging in as a different user.
2. Use ps -aux command to see the pid of the process.
3. Use kill -9 command to kill the process.

7. What is the significance of -9 in the command: kill -9 pid?


Ans.
The -9 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. How do you kill a process running in the background?


Ans.
You can kill the process running in the background using the kill command. You need to specify the PID 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?


30 15 * * * sort file1.lst > file1.srt
Ans.
In the above command, 30 represent minutes and 15 represents hours. The whole command means that everyday at 3:30
PM, the content of file1.lst will be sorted and saved in the file, 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 text from one user terminal to the terminal of another user who is also logged on the Unix
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. If a user is logged into more than one terminal, how do you send the user a message using write?
Ans.
If a user is logged into more than one terminal, the ttyname is used to indicate the appropriate terminal.
3. Find a user who has an account on the system and send a message using mailx?
Ans.
The command to find a user who can send a message using mailx is:
who -T

4. What happens if you type q at the mailx prompt?


Ans.
Typing q at the mailx prompt enables you to quit the mailx.

5. Where is a user's personal mailbox located?


Ans.
A user's personal mailbox is located in the user's home directory.

6. Using the redirection operator on the command line, mail the file mesg.txt to user 1.
Ans.
The command to mail the file mesg.txt to user1is:
mailx user1 < mesg.txt

7. Include the subject 'my message' in the command line, in exercise no. 6.
Ans.
The command to mail the file mesg.txt to user1 with the subject 'my message'is:
mailx -s "my message" user1 < mesg.txt

8. How is pine different from mailx?


Ans.
The mailx is a command line tool that is used to send or receive messages at the shell prompt. In the sending mode, mailx
runs in an interactive mode and asks for the subject and message to be sent to the receiver.
The pine is a character-based menu driven mailer that also provides an address box to maintain the mail addresses list.
The incoming and outgoing messages are stored in different folders. The pine main menu window displays various
options, such as viewing help on pine, composing a new message, creating address book, and configuring pine.

9. How do you create an address book using pine?


Ans.

13 | P a g e
To create an address book using pine, you need to press the key 'A' to enter the address book, while the pine main menu
window is displayed.

14 | P a g e
10. Send a mail to user1 using pine.
Ans.
The steps to send a mail using pine are:
4. Type pine at the shell prompt
5. Press the key C to compose and send a message.
6. Type the mail address of user1 in the line labeled as 'To' .
7. Type the subject of your mail in the line labeled as 'Subject'.
8. Type your message in the Message Text area.
9. Press CTRL+X to send the message.
10. Press 'Y' at the confirmation to send the message to user1.

Solutions to Chapter Eleven Questions


1. Use head and tail to select lines 5 to 10 of a file.
Ans.
The command to display the lines 5-10 of a file is:
$ tail +5 <file name> | head -n 6

2. Generate a code list by selecting departments from emp.lst.


Ans.
The command to select departments from the emp.lst file is:
$ cut -d \| -f 3 emp.lst
In the above command, 3 represents the column number of the department field of the emp.lst file.
Note: The content of the emp.lst file is given in the chapter.

3. Remove the repeated lines from a file.


Ans.
The command to remove the repeated lines from a file is:
sort <name of the file from which repeated lines are to be removed> | uniq - <new file name>

4. Convert 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:
$ tr '[a-z]' '[A-Z]' < emp.lst

5. How will you find out the number of times the character occurs in a file?
Ans.
$ cat <filename> | grep -o "?" | wc -l

6. Mail a sorted list of users currently logged in to root, taking account of the possibility that a user may be logged in more
than once.
Ans.
$ who | sort | uniq -u > userlist
$ mail root < userlist

15 | P a g e
Solutions to Chapter Twelve Questions
1. Define each of the following terms;
Shell script, comment, subshell
Child process, positional parameter, selection structure
Default case, repetition structure, iteration
loop
Ans.

 Shell script: Is a script or program written for the shell of a UNIX. The name of the file can
later be used to execute the stored sequence with a single command. In MS-DOS, these
files are known as "batch files", which are recognized by the .bat file name extension. In
UNIX, they are called "shell scripts" and do not require special file name extension.
 Comment: A comment is a part of a source code that explains the working of a code.
Comments are ignored by compilers and interpreters. A comment in a PostScript is the text,
which is preceded by a '%' sign. The comment in which '%!' is included as the first two
characters marks the file as a PostScript code.
 Subshell: The shell is a utility so you can execute the shell command, such as sh for the
Bash shell, 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 shell, sub-shell, to execute 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 execution. The new shell that is created is terminated as soon as the
script running on it completes execution.
 Child process: A child process is a computer process, which is created by an existing
process. The existing process is called the parent process.
 Positional parameter: A positional parameter is a subroutine parameter identified by the
order in which it is listed. Positional parameters are enclosed in parentheses and are
separated by commas in a subroutine call. Positional parameters are listed in the order in
which they appear in the corresponding subroutine definition.
 Selection structure: The selection structure gives a choice to choose one options based
on a condition from a set of options. For example, if-else and case construct.
 Default case: In a case statement, the default case enables you to specify the code to be
executed when the expression does not match any of the specified cases. The default case
corresponds to an unrecognized case.
 Repetition structure: The repetition structure is used for carrying out commands more
than once. For example, for, while, and until.
 Iteration: Iteration refers to the use of a loop to repeat one or more actions.
 Loop: Loop refers to the executable statements repeated a several times in a program,
such as DO-loop, an IF-statement.

2. Develop a script logic that allows only kumar and sachin (login names) to execute a program.
Ans.
#!/bin/sh
# Allow kumar and sachin to execute a program
u=`whoami`
if test $u == "kumar" -o $u == "sachin"
then
set `date`
echo "Remember for today"
case $1 in
Mon) echo "Plan the week.";;
Tue) echo "Take clothes to the cleaners.";;
Wed) echo " Attend group meeting.";;
Thu) echo "Make plans for the weekend.";

16 | P a g e
echo "Pick up the clothes from the cleaners.";;
Fri) echo "Answer e-mail.";;
Sat) echo "You should not be here working.";
Echo "Finish your work and log off.";;
Sun) echo "Call Grandma and Grandpa.";;
esac
else
echo "You are not allowed to run this script."
fi

3. Develop a script logic that executes script only from terminal tty01 and tty02.
Ans.
#!/bin/sh
# Allows script to be executed from terminal tty01 and tty02
u=`tty`
if test $u == "tty01" -o $u == "tty02"
then
set `date`
echo "Remember for today"
case $1 in
Mon) echo "Plan the week.";;
Tue) echo "Take clothes to the cleaners.";;
Wed) echo " Attend group meeting.";;
Thu) echo "Make plans for the weekend.";
echo "Pick up the clothes from the cleaners.";;
Fri) echo "Answer e-mail.";;
Sat) echo "You should not be here working.";
echo "Finish your work and log off.";;
Sun) echo "Call Grandma and Grandpa.";;
esac
else
echo "The script cannot be executed on this terminal."
fi

4. Write a shell script that accepts one or more filenames as arguments, and converts them all to uppercase provided they
exist in the current directory.
Ans.
#!/bin/sh
# Translating files into uppercase
i=1
for name in $*
do
if test -f $name

17 | P a g e
then
echo "$name in upper case:"
a=`echo $name | tr "[a-z]" "[A-Z]"`
mv $name $a
else
echo "File does not exist."
fi
done

5. Write a shell script that accepts two directory names, foo1 and foo2, and deletes those files in foo2 which are identical
to their namesakes in foo1.
Ans.
#!/bin/sh
#Deleting identical files from a directory
echo "Enter two directories"
read d1
read d2
if test ! -d $d1 -o ! -d $d2
then
echo "You have entered wrong directory names"
else
for name in `ls $d2`
do
for na in `ls $d1`
do
if test $name == $na
then
z='y'
break
else
z='n'
fi
done
if test $z != 'n'
then
echo "Deleting $name"
rm $d2/$name
else
echo "$name does not match with any file in $d1"
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. By default,
the script should show the listing of all files in the current directory.
Ans.
#!/bin/sh
# Listing files by modification time and access time
s=$1
if test "$s" == "lm"
then
ls -lt
elif test "$s" == "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 30 seconds five times using a (i) while loop (ii) for loop.
Ans.
(i) The shell script using the while loop is:
#!/bin/sh
# Displaying current processes in the system every 30 seconds
i=0
while test $i -lt 5
do
ps
sleep 30
i=`expr $i + 1`
done
(ii) The shell script using the for loop is:
#!/bin/sh
# Displaying current processes in the system every 30 seconds
for i in 1 2 3 4 5
do
ps
sleep 30
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
read, write, or execute it. Be sure to label the output to show what was done to the file.
Ans.
#!/bin/sh
#Label the output of chmod
echo "Enter the name of the file, whose permissions you want to change"
read filename
echo "Before changing the file permissions, File permissions for the owner of the $1 file are: `ls -l $filename|tr -s " "|cut -c
2,3,4`"
echo "Before changing the file permissions, File permissions for the group owner of the $1 file are: `ls -l $filename|tr -s " "|
cut -c 5,6,7`"
echo "Before changing the file permissions, File permissions for the other users of the $1 file are: `ls -l $filename|tr -s " "|
cut -c 8,9,10`"
echo "--------Changing the file permissions--------"
chmod u+rwx,go-rwx $filename
echo "After changing the file permissions, File permissions for the owner of the $1 file are: `ls -l $filename|tr -s " "|cut -c
2,3,4`"
echo "After changing the file permissions, File permissions for the group owner of the $1 file are: `ls -l $filename|tr -s " "|
cut -c 5,6,7`"
echo "After changing the file permissions, File permissions for the other users of the $1 file are: `ls -l $filename|tr -s " "|cut
-c 8,9,10`"

20 | P a g e
2. Modify the del script so that it detects whether the user has specified a directory to be deleted, in which case the script
should call the rmdir command. (Hint: use the test with the -d option to test for a directory.)
11. Rewrite tickle to use an if/then/elif/...../fi structure.
12. Rewrite echo.sh to use an until loop.
Ans.
The del script:
#!/bin/sh
#Delete a file or directory interactively
filename=$1
if test -f $filename
then
echo "Do you want to delete \"$filename\"?"
read choice
if test $choice == y
then
rm $filename
echo "\"$filename\" deleted."
else
echo "\"$filename\" not deleted."
fi
elif test -d $filename
then
echo "Do you want to delete \"$filename\"?"
read choice1
if test $choice1 == y
then
echo "Do you want to delete the subdirectories also?"
read ans
if test "$ans" == "y"
then
rm -r $filename
else
echo "\"$filename\" not deleted."
fi
fi
else
echo "\"$filename\" does not exist."
fi

1. The tickle script:


#!/bin/sh
# A daily reminder service
set `date`

21 | P a g e
echo "Remember for today"
if [ $1 == "Mon" ]
then
echo "Plan the week.";
elif [ $1 == "Tue" ]
then
echo "Take clothes to the cleaners.";
elif [ $1 == "Wed" ]
then
echo " Attend group meeting.";
elif [ $1 == "Thu" ]
then
echo "Make plans for the weekend.";
echo "Pick up clothes at the cleaners.";
elif [ $1 == "Fri" ]
then
echo "Answer e-mail.";
elif [ $1 == "Sat" ]
then
echo "You should not be here working.";
echo "Finish your work and log off.";
elif [ $1 == "Sun" ]
then
echo "Call Grandma and Grandpa.";
else
echo "Invalid day";
fi

2. The echo.sh script:


#!/ bin/sh
# Echo a line 'n' times
count=$1
shift
message=$*
until test $count -eq 0
do
echo $message
count=`expr $count - 1`
done

3. The lshead.sh script:


#Labeling the output of ls -l command
echo

22 | P a g e
echo "Displaying the output of ls-l"
echo
ls -l | grep [-rwx] > try
echo "FAP Links Owner Group owner Size Date-Time Filename"
cat try | more

23 | P a g e

Potrebbero piacerti anche