Unit 4
Unit 4
FILES IN LINUX
● These users share system resources such as the hard disk, memory and devices
attached to the system.
● Each user on the Linux system is assigned a user Login, a password, and a group or
groups to which the user belongs.
● Example:
Project- workers
● In Linux file access permissions (FAPs) help to secure files and folders.
● File access permissions refer to the permissions associated with a file with respect to
the following.
3. Other users
● The permissions that can be granted or revoked are symbolically represented by the
letters r, w, and x and are grouped together as rwx for different users in Linux.
1
Access Denoted by Action Permitted on Action permitted on a
Type a file Directory
- Lets us understand how we can assign permissions in linux by using the following
sample directory list:
Example:
[steve@localhost steve]$ ls -1
Total 21
2
drwxrwxr -x 2 steve steve 1024 sep 22 02:11baseball
● The first character indicated the type of file, such as an ordinary file, a hidden file or a
directory.
● The next sets of three characters display the read, writes, and execute permissions in
order for the file owner.
● The following three characters are the permissions for the group owner, and the last
three are the permissions for the others users.
● Change to a directory
● Display a file from directory
● Copy a file from a directory
3
● You can modify the access permissions associated with a file or directory by using the
chmod command.
● However only the owner of a file can change the permissions associated with it.
● You can use the chmod command to grant or revoke permissions for files and
directories.
● The permissions for a file or directory indicate the actions that can be performed on
the file or the directory.
● When using the chmod command, you use the +sign to grant permissions, and the –
sign to revoke the permissions.
● You use the chmod command to specify the mode in the following ways:
● Symbolic- The permission and the user type are
specified as symbols.
Symbolic mode
● In the symbolic mode you use symbols to specify the permissions and the types of
users, such as file owner or order to whom the permission are to be granted are to be
granted or revoked.
Symbol Meaning
u Owner of the file or directory
g Members of the same group
4
o All other users
a All users
● To modify the permissions for a file ,the file owner must specify:
1. The type of user for whom the permission is to be modified.
Example:
● Here ‘u’ indicates the file owner ,’+’ indicates that the permission is to be given, ‘x’
indicates the execute permission, and ‘x.c’ represents the file name.
Absolute mode
● In the absolute mode, you use a series of digits to present file permissions.
● Using the absolute mode is more convenient than using the symbolic mode because
you can specify different permissions for all the types of users in one statement.
● The following table explains the number that denotes each type of permissions:
Absolute Numbers for permissions
Number Permission
4 Read
5
2 write
1 Execute
● When you create as file the umask value and the mode value determine the initial file
permissions.
● The mode value indicates that all the users have read and write permissions for a file.
● By default the mode value is 666 for files and 777 for directories and executable files.
● To derive file access permissions the value of umask is subtracted from the mode
value.
By default the umask value is given as 002 for user and 002 for root user which means
that each file that a user create has the permission 666-002=664
6
AUTOMATING TASK USING SHELL SCRIPTS
• The shell acts as an interface between the user and the kernel and eliminates the need
for a programmer to communicate directly with the kernel.
• The command interpretation of shell as follows:
• When you log on linux prompt, indicating that it is ready to execute commands.
• When you give a command, the shell perform the following task:
• Reads the command
• The following figure depiets the process of command interpretation:
7
$ echo “this is an example of the echo command ”
This is an example of an echo command
$
• The echo command displays text enclosed between”” on the screen
Example:
#!/bin/bash
Echo”hello”
Echo”world”
Variables:
Creating variables
• Variables can be created at any point of time by simply assign a value.
• A variable can be created without a value by leaving the right hand side of assignment
8
Operator(=) blank.
Syntax:
<variable name=><value>
Example :
Name=”john lennon”
Referencing variable
Angela
Start
Accept name
9
Accept number
Stop
• You can use the following shell script store data to accept details of customer:
#!/bin/bash
Echo “enter the name of the customer.”
Read name
Echo “enter the mobile number.”
Read number
Echo “$name: $number”>> customerdata
• You can execute the store data shell script by using the command:
$ bash storedata
• You can execute it as follows:
$chmod *x storedata
./store data
• Execute the shell script, the following screen is displayed:
$ ./storedata
Enter the name of the customer.
David
Enter the mobile number.
932 1232233
• You can use the cat command:
$ cat customerdata
Angela :993212342
Steve :9423178643
Martin:756352190
• The same variable name can be give different value without the parent shell knowing
About it. Such variable is called a local variable.
• When you create a variable, it is accessible only in which you create it.
• $ continent = Africa
• $ echo “continent”
10
• Africa
• $ sh create a child shell
• $ echo “$continent”
• There is no response
• $ continent = asia give a new value
• $ echo “continent”
• Asia
• Press <ctrl> d
• $ exit display exit and return to parent shell
• $ echo “continent”
• Africa parent is unware of asia
• $ sh create a another child
• $ echo “continent”
• Continent does not have any value
• Press <ctrl> d
• $ exit
• _ display exit and return to parent
• A variable that is made available to all the child shell of a shell is known as an
exported variables. Consider the following command:
• $ continent = Africa
• Export continent
• $ echo “continent”
• Africa
• $ sh create a another child
• $ echo “continent”
• Africa
• $
• $ continent = asia give a new value
• $ echo “continent”
• Asia
• Press <ctrl> d
• $ exit display exit and return to parent shell
• $ echo “continent”
• Africa
• $ _
• The variable created in a shell are local to the shell. In other words , variables created
in a shell are available only in the shell in which you create variables.
• You can use the export command to make a variable a global or accessible to child
shells.
11
Environment variable
• In a multiuser operating system such a linux each user work on a copy of the
shell.each shell as a set of special variables
• Which uer can edit to the shell environment.
• In a linux operating system each user has an associated with directory called home
directory.
• The user to taken to the corresponding home directory
• The location of the directory of the user is stored in the environment variable HOME.
$ echo $HOME
The path variable
• The path variables contained a list of colon delimited path name of the directories that
are searched for any executable program.
$ PATH=/usr/bin:/bin
• Example :
• PATH=/bin:/usr/bin: searches in the order:/bin,/usr, /bin,and the current directory
• PATH =:/bin:/usr/bin searches in order : the current directory /bin, /usr/bin
• PATH=/bin: , ;/usr/bin searches in order: /bin, the directory and /usr/bin
1. The PS1 or Prompt String 1 variable contains the shell prompt ,the $ symbol.
2. You can change the shell prompt by editing the value of the PS1 variable.
● The PS2 variable specifies that the value for the secondary prompt.
● By default the secondary prompt is the >symbol .
● The Linux operating system displays the secondary prompt when you type an
incomplete command on the command line.
● Example:
$ PS1 = ^
12
>
>’
$............
● The SHLVL variable contains the shell level that you are currently working in.
● When working on Linux you might create new shells and forget the level of
the shell in which you are working.
● In such a situation , you can use the SHLVL environment variable to know the
level of the shell in which you are working.
● In the Linux operating system the login shell is assigned the number 1.
● When you create a new shell the value of the variable SHLVL is incremented
by one.
● Example
$ echo $ SHLVL
$ echo $ SHLVL
exit
13
$ echo $ SHLVL
● The SHELL environment variable stores the default shell of the user.
● You can view the value of the variable by using the following command:
● $ echo $ SHELL
● /bin/bash
● You use the env command to view a list of all the exported environment variables
and their respective values.
● Example
● Lab Program 2
Command Substitution
● In addition to pipes you can use command substitution to combine more than one
command in a command line.
● Suppose that you need to display the following message on the screen:
● The date is(output of the date command)
● To do so, you can enter any the following command:
● Echo “The date is ‘date’ “
The command date is enclosed in single backward quotation or a grave accent
● var1=25
● Example
● $ expr 4+5
14
● You can use variables in the expr command:
$ a=5
$ b=4
$ expr $a+$b
b=5
(i) a= ‘expr $ a - 7’
a=‘expr $a /3’
Arithmetic Expansion
● Syntax:
● $ (( expression))
● Example
● Example
● $ a=25
● $b=56
● $echo $((a+b))