Introduction To Shell Programming: Operating System Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Operating System Lab

Lab 2
Introduction to Shell Programming

Part I. Changing file mode. Only the owner of a file can use chmod (change file mode) to
change the permission of a file.

1. Create a text file by redirecting the output of l s - 1 command into a text file called files.txt.
i.e. ls - 1 > files . txt .

2. Type ls –l files.txt to check the permissions of this file. (Permissions consist of 10 characters
i.e. - r w - r - - r - - . The first character indicates the file type and the rest indicate the file
access
modes for user, group and all the others respectively)

3. Try the following commands and then observe the difference in the permissions.

Again typing l s – l fi l e s . txt as in 2.


chmod a – rw files .txt
chmod go + rw files . txt

Part II. Displaying information: The echo command can be used to display messages. It
displays its arguments on your terminal.

1. Type e c h o “This is a test” and observe the output.

2. Type this command, echo “Total number of files is:” `l s - 1 | w c – l `.

3. echo command can be used to display text and the values of the shell variables. To access
the value stored in a shell variable, you must precede the name of the variable with a $. Set
age to 25. i.e. age = 25 or age = ”25 ” .

4. Try the following commands and observe the differences:


echo “ You are age year sold ”
echo “ You are $ age year sold ”

5. Observe the outputs of the following commands.


echo $ HOME

Submitted By: Ali Murad


Submitted To: Muhib Khan
Operating System Lab

echo ~

Part III.
1. Write a simple shell script to display number of users in the system by using p i c o utility
as shown below and save it as won.

2. You can use sh command to execute script files. To run won script, type s h w o n .

Part IV. Executable shell scripts by chmod command.


1. The second method of executing a shell program is to make the script file executable. In
this case, all you do is to type the name of the script program; just as you do for any other
shell commands. This is preferred method. Try the following set of commands:

Part V. The cases construct.


1. The shell provides you with the case construct, which enables you to selectively execute
a set of commands from the list of commands. An example is given below; use pico
utility to write the following shell script. Script name should be MENU.

2. Make M E NU an executable script. i.e. chmodu+ x M E NU


3. Type. / M E NU and try to understand the shell script that you have written.

Carefully read the description of the corresponding lab. The lab outline contains background for
the lab and directions for doing the lab procedure. There may also be handouts or other materials
Submitted By: Ali Murad
Submitted To: Muhib Khan
Operating System Lab

you have access to. By the help of a Live CD of Ubuntu you can study to the corresponding lab
without making any changes to your machine. Answers of the preliminary Lab Questions must
be handled at the beginning of the laboratory. A full printable version of this sheet is available
online.

1. What is the overall purpose of the lab?

A Unix shell is a command-line interpreter or shell that provides a traditional Unix-like


command line user interface. Users direct the operation of the computer by entering
commands as text for a command line interpreter to execute, or by creating text scripts of
one or more such commands.

2. What is the meaning of the following command: “chmod go+rw files.txt”

NAME
chmod - change file mode bits

SYNOPSIS
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

DESCRIPTION
This manual page documents the GNU version of chmod. chmod changes the
file mode bits of each given file according to mode, which can be
either a symbolic representation of changes to make, or an octal number
representing the bit pattern for the new mode bits.

The format of a symbolic mode is [ugoa...][[+-=][perms...]...], where


perms is either zero or more letters from the set rwxXst, or a single
letter from the set ugo. Multiple symbolic modes can be given, sepa‐
rated by commas.

A combination of the letters ugoa controls which users' access to the


file will be changed: the user who owns it (u), other users in the
file's group (g), other users not in the file's group (o), or all users
(a). If none of these are given, the effect is as if a were given, but
bits that are set in the umask are not affected.

The operator + causes the selected file mode bits to be added to the
existing file mode bits of each file; - causes them to be removed; and
= causes them to be added and causes unmentioned bits to be removed
except that a directory's unmentioned set user and group ID bits are
not affected.

The letters rwxXst select file mode bits for the affected users: read

Submitted By: Ali Murad


Submitted To: Muhib Khan
Operating System Lab

(r), write (w), execute (or search for directories) (x), execute/search
only if the file is a directory or already has execute permission for
some user (X), set user or group ID on execution (s), restricted dele‐
tion flag or sticky bit (t). Instead of one or more of these letters,
you can specify exactly one of the letters ugo: the permissions granted
to the user who owns the file (u), the permissions granted to other users who are
members of the file's group (g), and the permissions
granted to users that are in neither of the two preceding categories
(o).

A numeric mode is from one to four octal digits (0-7), derived by


adding up the bits with values 4, 2, and 1. Omitted digits are assumed
to be leading zeros. The first digit selects the set user ID (4) and
set group ID (2) and restricted deletion or sticky (1) attributes. The
second digit selects permissions for the user who owns the file: read
(4), write (2), and execute (1); the third selects permissions for
other users in the file's group, with the same values; and the fourth
for other users not in the file's group, with the same values.

chmod never changes the permissions of symbolic links; the chmod system
call cannot change their permissions. This is not a problem since the
permissions of symbolic links are never used. However, for each sym‐
bolic link listed on the command line, chmod changes the permissions of
the pointed-to file. In contrast, chmod ignores symbolic links encoun‐
tered during recursive directory traversals.

3. What is the function of “sh” command?

NAME
dash — command interpreter (shell)

SYNOPSIS
dash [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name] [+o option_name]
[command_file [argument ...]]
dash -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name] [+o option_name]
command_string [command_name [argument ...]]
dash -s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name] [+o option_name]
[argument ...]

DESCRIPTION
dash is the standard command interpreter for the system. The current version of dash is
in the process of being changed to conform

Submitted By: Ali Murad


Submitted To: Muhib Khan
Operating System Lab

with the POSIX 1003.2 and 1003.2a specifications for the shell. This version has many
features which make it appear similar in some
respects to the Korn shell, but it is not a Korn shell clone (see ksh(1)). Only features
designated by POSIX, plus a few Berkeley
extensions, are being incorporated into this shell. This man page is not intended to be a
tutorial or a complete specification of the
shell.

4. Write the command necessary to make script file “xyz” an executable file for the user?

Man chmod u+x xyz


./xyz

5. What is the output of the following command: “echo”?

echo command can be used to display text and the values of the shell variables.

Signature >:_________________

Submitted By: Ali Murad


Submitted To: Muhib Khan

You might also like