Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Fossexp 9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CSE (R13@II.

B-Tech II-Sem) EXP-9

a) Write a shell script that displays a list of all the files in the current directory to which the
user has read, write and execute permissions.
b) Develop an interactive script that asks for a word and a file name and then tells how
many times that word occurred in the file.
c) Write a shell script to perform the following string operations:
i. To extract a sub-string from a given string.
ii. To find the length of a given string.

a) Write a shell script that displays a list of all the files in the current directory to which
the user has read, write and execute permissions.
Program:
[CSESTAFF@localhost ex9]$ vi perm.sh
echo "The list of File Names in the curent directory."
echo "Which have Read,Write and Execute permisions. "
for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
ls -l $file
fi
fi
done
Output:
[CSESTAFF@localhost ex9]$ ls -l
-rw-rw-r--. 1 CSESTAFF CSESTAFF 16 Jan 31 20:03 t1.txt
[CSESTAFF@localhost ex9]$ chmod 777 t1.txt
[CSESTAFF@localhost ex9]$ ls -l
-rwxrwxrwx. 1 CSESTAFF CSESTAFF 16 Jan 31 20:03 t1.txt
[CSESTAFF@localhost ex9]$ sh perm.sh
The list of File Names in the curent directory.
Which have Read,Write and Execute permisions.
-rwxrwxrwx. 1 CSESTAFF CSESTAFF 16 Jan 31 20:03 t1.txt

bphanikrishna.wordpress.com
FOSSLAB Page 1 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9

b) Develop an interactive script that asks for a word and a file name and then tells how
many times that word occurred in the file.
Program:
[CSESTAFF@localhost ex9]$ cat f1.txt
day by day week by end
week by week month by end
month by month year by end
but friendship is never end

[CSESTAFF@localhost ex9]$ vi wcount.sh


echo " Enter the word to be searched"
read word
echo "Enter the filename to be used"
read flname
echo "the no. of times the word ['$word'] occured in the file."
grep -o $word $flname|wc -l
echo " the no of line that contains ['$word'] is"
grep -c $word $flname

Output:
[CSESTAFF@localhost ex9]$ sh wcount.sh
Enter the word to be searched
by
Enter the filename to be used
f1.txt
the no. of times the word ['by'] occured in the file.
6
the no of line that contains ['by'] is
3

bphanikrishna.wordpress.com
FOSSLAB Page 2 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9

c) Write a shell script to perform the following string operations:


a. To extract a sub-string from a given string.
b. To find the length of a given string.
Program:
[CSESTAFF@localhost ex9]$ vi substr.sh
echo "enter the string"
read str
strlen=${#str}
echo "The length of the given string '$str' is:$strlen "
echo "enter the string possibion in main str"
read s1
echo "ending possition"
read f1
echo $str | cut -c$s1-$f1
[CSESTAFF@localhost ex9]$
Output:
[CSESTAFF@localhost ex9]$ sh substr.sh
enter the string
krishnasai
The length of the given string 'krishnasai' is:10
enter the string possibion in main str
8
ending possition
10
sai
[CSESTAFF@localhost ex9]$

bphanikrishna.wordpress.com
FOSSLAB Page 3 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9
File ownership is an important component of UNIX that provides a secure method for
storing files. Every file in UNIX has the following attributes:

Users
A UNIX system serves many users. Users are an abstraction that denotes a logical entity for
assignment of ownership and operation privileges over the system. A user may correspond to
a real-world person, but also a type of system operation. So, in my system, I have user 'nick'
that corresponds to me, but I also have user 'www' which corresponds to the privileges
necessary to operate the local webserver. UNIX doesn't care about what the user means for
me. It just knows what belongs to any given user and what each user is allowed to do with
any given thing (file, program, device, etc) on the system. UNIX identifies each user by a
User ID (UID) and the username (or login) such as 'nick' and 'www' is just an alias to the UID
that makes humans more comfortable.

Groups
Users can be organized in groups. A user may belong to one or more groups of users. The
concept of groups serves the purpose of assigning sets of privileges for a given resource and
sharing them among many users that need to have them. (perhaps because they are all
members of a project working team and they all need access to some common project files)
So, on my system user 'nick' and user 'www' both belong to the group 'perlfect'. This way,
they can have some shared privileges over the files for this site. User 'nick' needs them to edit
the site, and user 'www' needs them to manage the webserver that will be publishing the site.

Ownership
Every file in UNIX has an owner user and an owner group. So, for any file in the system,
user 'nick' may have one of the following ownership relations:
 nick owns the file, i.e. the file's owner is 'nick'.
 nick is a member of the group that owns the file, i.e. the file's owner group is
'perlfect'.
 nick is neither the owner, nor belonging to the group that owns the file
Permissions
Every file on the system has associated with it a set of permissions. Permissions tell UNIX
what can be done with that file and by whom. There are three things you can (or can't) do
with a given file:
 read it,
 write (modify) it and

bphanikrishna.wordpress.com
FOSSLAB Page 4 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9
 execute it.

Unix permissions specify which of the above operations can be performed for any ownership
relation with respect to the file. In simpler terms, what can the owner do, what can the owner
group do, and what can everybody else do with the file. For any given ownership relation, we
need three bits to specify access permissions: the first to denote read (r) access, the second to
denote (w) access and the third to denote execute (x) access. We have three ownership
relations: 'owner', 'group' and 'all' so we need a triplet for each, resulting in nine bits. Each bit
can be set orclear. (not set) We mark a set bit by it's corresponding operation letter (r, w or x)
and a clear bit by a dash (-) and put them all on a row. An example might be rwxr-xr-x.What
this means is that the owner can do anything with the file, but group owners and the rest of
the world can only read or execute it. Usually in UNIX there is also another bit that precedes
this 9-bit pattern. You do not need to know about it, at least for the time being.
So if you try ls -l on the command prompt you will get something like the

following: [nick@thekla src]$ ls -l

-rwxr-xr-x 1 nick users 382 Jan 19 11:49 bscoped.pl

drwxr-xr-x 3 nick users 1024 Jan 19 11:19 lib/

-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl

The first column here shows the permission bit pattern for each file. The third column shows
the owner, and the fourth column shows the owner group. By the time, the information
provided by ls -l should be enough for you to figure out what each user of the system can do
with any of the files in the directory.
Directories
Another interesting thing to note is that lib/ which is a directory has permissions, too.
Permissions take a different meaning for directories. Here's what they mean:
 read determines if a user can view the directory's contents, i.e. do ls in it.
 write determines if a user can create new files or delete file in the directory. (Note
here that this essentially means that a user with write access toa directory can delete
files in the directory even if he/she doesn't have write permissions for the file! So be
careful with this.)
 execute determines if the user can cd into the directory.

bphanikrishna.wordpress.com
FOSSLAB Page 5 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9

chmod
To set/modify a file's permissions you need to use the chmod program. Of course, only the
owner of a file may use chmod to alter a file's permissions. chmod has the following
syntax: chmod [options] mode file(s)
The 'mode' part specifies the new permissions for the file(s) that follow as arguments. A
mode specifies which user's permissions should be changed, and afterwards which access

types should be changed. Let's say for example: chmod a-x socktest.pl This means that the

execute bit should be cleared (-) for all users. (owner, group and the rest of the world) The
permissions start with a letter specifying what users should be affected by the change, this
might be any of the following:
 u the owner user
 g the owner group
 o others (neither u, nor g)
 a all users
This is followed by a change instruction which consists of a +(set bit) or -(clear bit) and the
letter corresponding to the bit that should be changed.

Let's see some examples: $ ls -l socktest.pl

-rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl*

$ chmod a-x socktest.pl

$ ls -l socktest.pl

-rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl

$ chmod g+w socktest.pl

$ ls -l socktest.pl

-rw-rw-r-- 1 nick users 1874 Jan 19 10:23 socktest.pl

$ chmod ug+x socktest.pl

$ ls -l socktest.pl

-rwxrwxr-- 1 nick users 1874 Jan 19 10:23 socktest.pl*

bphanikrishna.wordpress.com
FOSSLAB Page 6 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9

$ chmod ug-wx socktest.pl

$ ls -l socktest.pl

-r--r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl

Strange numbers...
You might have encountered things like chmod 755 somefile and of course you will be
wondering what this is. The thing is, that you can change the entire permission pattern of a
file in one go using one number like the one in this example. Every mode has a corresponding
code number, and as we shall see there is a very simple way to figure out what number
corresponds to any mode.
Every one of the three digits on the mode number corresponds to one of the three permission
triplets. (u, g and o) Every permission bit in a triplet corresponds to a value: 4 for r, 2 for w, 1
for x. If the permission bit you add this value to the number of the permission triplet. If it is
cleared, then you add nothing. (Some of you might notice that in fact, the number for a triplet
is the octal value corresponding to the three-bit pattern - if you don't know what an octal
value is, it doesn't really matter, just follow the intstructions) So if a file has rwxr-xr-
x permissions we do the following calculation:
Triplet for u: rwx => 4 + 2 + 1 =7
Triplet for g: r-x => 4 + 0 + 1 =5
Tripler for o: r-x => 4 + 0 + 1 =5
Which makes : 755
So, 755 is a terse way to say 'I don't mind if other people read or run this file, but only I
should be able to modify it' and 777 means 'everyone has full access to this file'

chmod is used to change the permissions of files or directories.


In general, chmod commands take the form:
chmod options permissions filename

If no options are specified, chmod modifies the permissions of the file specified
byfilename to the permissions specified by permissions.
permissions defines the permissions for the owner of the file (the "user"), members of the
group who owns the file (the "group"), and anyone else ("others"). There are two ways to
represent these permissions: with symbols (alphanumeric characters), or withoctal numbers
(the digits 0 through 7).
Let's say you are the owner of a file named myfile, and you want to set its permissions so
that:

bphanikrishna.wordpress.com
FOSSLAB Page 7 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9
1. the user can read, write, and execute it;
2. members of your group can read and execute it; and
3. others may only read it.
This command will do the trick:
chmod u=rwx,g=rx,o=r myfile

This is an example of using symbolic permissions notation. The letters u, g, and o stand for
"user", "group", and "other". The equals sign ("=") means "set the permissions exactly like
this," and the letters "r", "w", and "x" stand for "read", "write", and "execute", respectively.
The commas separate the different classes of permissions, and there are no spaces in between
them.
Here is the equivalent command using octal permissions notation:
chmod 754 myfile
Here the digits 7, 5, and 4 each individually represent the permissions for the user, group, and
others, in that order. Each digit is a combination of the numbers 4, 2, 1, and0:
 4 stands for "read",
 2 stands for "write",
 1 stands for "execute", and
 0 stands for "no permission."
So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1(read, no
write, and execute), and 4 is 4+0+0 (read, no write, and no execute).
Following are the symbolic representation of three different roles:
 u is for user,
 g is for group,
 and o is for others.
Following are the symbolic representation of three different permissions:
 r is for read permission,
 w is for write permission,
 x is for execute permission.
Following are few examples on how to use the symbolic representation on chmod.

1. Add single permission to a file/directory


Changing permission to a single set. + symbol means adding permission. For example, do the
following to give execute permission for the user irrespective of anything else:

$ chmod u+x filename

bphanikrishna.wordpress.com
FOSSLAB Page 8 of 9
CSE (R13@II.B-Tech II-Sem) EXP-9
2. Add multiple permission to a file/directory
Use comma to separate the multiple permission sets as shown below.

$ chmod u+r,g+x filename

3. Remove permission from a file/directory


Following example removes read and write permission for the user.

$ chmod u-rx filename

4. Change permission for all roles on a file/directory


Following example assigns execute privilege to user, group and others (basically anybody
can execute this file).

$ chmod a+x filename

5. Make permission for a file same as another file (using reference)


If you want to change a file permission same as another file, use the reference option as
shown below. In this example, file2′s permission will be set exactly same as file1′s
permission.

$ chmod --reference=file1 file2

6. Apply the permission to all the files under a directory recursively


Use option -R to change the permission recursively as shown below.

$ chmod -R 755 directory-name/

7. Change execute permission only on the directories (files are not affected)
On a particular directory if you have multiple sub-directories and files, the following
command will assign execute permission only to all the sub-directories in the current
directory (not the files in the current directory).

$ chmod u+X *

bphanikrishna.wordpress.com
FOSSLAB Page 9 of 9

You might also like