Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
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.
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"
3|Page
Ans.
The command to display the release version of the operating system is:
$ uname -r
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
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
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
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
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|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 {} \;
8|Page
f. Type vi <file name> at the shell prompt, press 15G in the escape mode.
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.
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
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
10 | P a g e
The command to listing all the running processes is:
$ 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.
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.
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
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
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.
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
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
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
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