UNIT-IV-Linux Notes
UNIT-IV-Linux Notes
Linux system
Each user on the Linux system is assigned
User login name
Password
Group or groups to which the user belongs
Introduction
As mentioned earlier, you use the ls command to display the name of the file.
When you use the -l option with the ls command, the command displays the
Names of files with the date
Time of creation, size, user name, group name, and Access permission for the file.
Let us understand how you can assign permissions in Linux by using the following sample directory
list:
The first character indicates the type of file, such as an ordinary file, a hidden file, or a directory.
The next set of three character display the read and write and execute permissions, in that order for
the file owner.
Introduction
You can modify the access permission associated with a file or a directory by using the
chmod command.
However only the owner of a file can change the permission associated with it.
Chmod
You can use the chmod command to grant or revoke (cancel) permission for files and
directories.
Permission can be granted to the,
File owner, represented by the letter “u”
Group owner, represented by the letter “g”
Other users, represented by the letter “o”
The permission for a file or directory indicates the actions that can be performed on the file
or directory.
When using the chmod command, you use the “+” sign to grant the permission.
Specification mode
You use the chmod command to specify the mode (permission to a type of user) in following ways:
Symbolic: the permission and the user type are specified as symbols.
Absolute: the permission is specified as a numbers.
Example
[steve@localhoststeve]$ chmod g+x trial.rpt
[steve@localhoststeve]$ chmod o-w cust.rpt
In the first example, the group owner is granted the permission to execute the file trial.rpt.
In the second example, the permission to write the cust.rpt file is revoked from other users.
Symbolic Mode
In the symbolic mode, you use symbols to specify the permissions and the type of users, such as a file
owner OR others, to whom the permissions are to be granted or invoked.
The following table explains the symbolic representation of entries
FILE ACCESS PERMISSION
SYMBOLIC MODE
To modify the permission for a file, the file owner must specify:
The type of user for whom the permissions to be modified.
The type of permission that is to be modified.
Whether the permission is to be granted or revoked.
Linux allows you to change FAPs for a specific user type.
Let us now look at same examples that demonstrate the user of FAPs
4.4- EXAMPLES
[steve@localhoststeve]$ ls -l x.c
-rw-rw-r- l steve INDTECH 50 sep 22 21:01 x.c
[steve@localhoststeve]$ chmod u+x x.c
Here,
‘u’ indicate the file owner
‘+’ indicate the permission is to be given
To remove the read permission from the group owner, type the following command.
[steve@localhoststeve]$ chmod g-r x.c
Here,
‘g’ indicate the group owner
‘-‘ indicate the permission is to be removed
The permission will new appear as follows.
[steve@localhoststeve]$ ls –l x.c
-rw-w-r- l steve IRDTECH 50 sep 22 21:01 x.c
To remove the read permission assigned to other users, you need to type the following command.
[steve@localhoststeve]$ chmod o-r x.c
Here, ‘o’ indicate other users
The permission will new appears are follows:
[steve@locahoststeve]$ ls -l x.c
-rwx-w- l steve - IRDTECH 50 sep22 21:01 x.c
Linux also allows the file owner to change permissions for all the categories of users.
To remove the execute permission for all the users, type the following command
[steve@localhoststeve]$ chmod a-x x.c
Here, ‘a’ indicate all users
The FAP for the file x.c will now appear as follows:
[steve@locahoststeve]$ ls -l x.c
-rwx-w- l steve - IRDTECH 50 sep22 21:01 x.c
Note that all the instances of x have been replaced by hyphens
To add the execute permission again, the file owner must type the following command
[steve@localhoststeve]$ chmod a+x x.c
After specifying the above command, the FAPs for the file x.c appear as follows
[steve@localhoststeve]$ ls -l x.c
-rwx-wx-w - l steve - IRDTECH 50 sep22 21:01 x.c
1. Introduction
2. Absolute number for permission
3. Representation of absolute number
4. Example
5. Mode and output of the chmod
command
6. Example
Introduction
In the absolute mode, you use a series (sequence) of digits to represent file permission using the
absolute mode is more convenient than using the symbolic mode because you can specify different
permission for all the type of users in one statement.
Examples
Consider that you need to assign the read permission to users in the order category on all of the file
with the extension .txt in your home directory.
You can assign permission by using the following commands:
[steve@localhoststeve]$ chmod 664 ~|x.txt
Here, the first octet is for the file owner, the second octet is for the group owner, and third octet is for
other users.
The following table explains some of permissions that are allocated when you specify a absolute
mode with the chmod command.
Example
[steve@localhoststeve]$chmod 722 empfeedbk.txt
Introduction
When you create a 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 file and 777 for directories and executable files.
To derive file access permissions, the value of umask is subtracted from the mode value.
The umask (UNIX shorthand) stands for User file creation Mode Mask.
It is four digit octal number that UNIX uses to determine the file permission for newly created files.
Every process has its own umask, inherited from its parent process.
The umask (UNIX shorthand) stands for User file creation Mode Mask.
It is four digit octal number that UNIX uses to determine the file permission for newly created files
Every process has its own umask, inherited from its parent process.
Syntax of umask
umask [value]
Here,
value is an octal number of up to three digits. If you do not specify a value, the UMASK command
returns the current UMASK value.
If you specify an octal number less than three digits, the operating system interprets the number as
a number that contain leading zeros.
For example, if you specify the value of 77, the UMASK command is interpreted by the operating
system as 077.
4.7-Example of umask
Example
Here, the first digit, 0 is called the sticky bit, it is a special security feature.
The next three digits represent the octal values of the umask for a file or directory.
Introduction
The shell is an intermediary program, which interprets (read) the commands that are typed at the
command prompt.
The shell translates (change) the commands that you type into the commands that are understood by
the kernel.
The shell act as an interface b/w the user and the kernel and eliminates the need for a programmer to
communication directly to the kernel.
Example
If the shell cannot locate a file corresponding to the command that you enter, the shell displays an error
message and then displays the $ sign
[steve@localhost steve]$ 1st file1
bash : 1st : command not found
[steve@localhost steve]$
4.10 - Echo commands
Echo command is used to display the message on the screen.
Script File
The shell can read the file in which command are stored and execute them
Such file is called a script file.
You can also manipulate variable shell scripts.
The shell scripts allow you to use iteration.
Example
Consider the following a file magic.sh is created the execution.
#!/bin/sh
echo “this is an echo command”
In magic file, the first line of the file informs the shell to execute the script file by using the bash shell.
You can also use other shells such as sh, tcsh, or ksh to execute shells scripts.
When you create a script file, the # and ! Characters should be the first two characters of the script file
followed by the interpreter that should be used to execute the file.
For example, if you need /bin/ksh as the interpreter for the script file, you need to include #!/bin/ksh
at the beginning of the shell file
login shell
When you logon to Linux, you begin work in a copy of the shell, known as login shell
The shell is an utility, so you can create a shell by executing the shell command
For example, you need to execute the bash command to create the bash shell
The new shell is known as subshell or child shell of the current shell
When you execute a shell script directly at the $ prompt, the current shell creates a child shell to
execute a shell script
The creation of the child shell ensure that the script does not affect the current shell
The shell script is passed to the child shell for execution
The child shell is terminated as soon as the shell script completes execution
Example
The following file is name hello:
# ! /bin/ksh
echo “hello”
echo “world”
Change FAPs for the script file using the following command
$ chmod u+x hello
S ./hello
hello
World
In the above example, the first line specifies tksh)hat the shell script should be interpreted by using the
Korn shell (ksh)
Variables
Variables is a name which is given to the input value for allocating the memory.
It is otherwise known as identifier
Syntax for creating variable
<Variable name> = <value>
If the value that you assign to a variable contains a delimiter, such as an embedded space, you need to
enclose the value within either single or double quotes
Example for creating variable
name = “Ivin Francis”
Explanation
However, you need not use the quotation marks if the value does not have any embedded space.
Example
Name=paul
Ctr=1
Explanation
In the above example, ctr does not contain a numeric variable
The ctr variable contains a character string
Therefore, the ctr variable contains the character “1” and not the number 1
We can create variables either in shell scripts or the shell prompt
If we create a variable within a shell script, the variable ceases to exist when the script completes
execution
In contrast, if we create a variable at the shell prompt, the variable exists until you log off
When we log off from a Linux session, the Linux operating system closes all the applications that are
running, including the login shell
Referencing Variables
Here $ is used to refer the content of a variable.
Here we are using assignment operator (=) to assign the value of one variable to another variable.
Syntax
Variable1 = $ {variable2}
Explanation
In the above example, the value of variable2 is assigned to variable1.
The braces are used to delimit the variable name
For example, consider a variable named today that contains the value 20 and another variable named x
You need to assign the value 20th to variable x by concatenating the value of the variable today with the
string th.
To concatenate the values, you will use the following command
Example
$ x = $ {today}th
Now, x would contain the value 20th
However, the following command will not produce the desired result because the shell would look for
a variable named todayth
Example
$ x = $ todayth
The braces are optional in case you are not concatenating variables
Therefore, the following two commands will have the same result
$ x = $ today
$ x = $ {today}
Reading a Value into a Variable
It allows you to assign a value to a variable.
The shell use to specify value for a variable by using a keyword “read”.
We can enter the value into a variable during the execution of a shell script by using the read
command.
Syntax
$ read fname
The read command can be used at the shell prompt but is usually used in shell scripts.
On execution, the read command waits for you to enter a value for the variable
When you press the <Enter> key after entering a value, the remaining part of the shell script, if any is
executed
Example
The read command does not prompt you to enter data
Example
You can execute the above program using the following command.
$ chmod +x program.sh
$ ./program.sh
Output
EXPORTED VARIABLE
A variable that may available to all the child shell of shell is known as exported variable.
• $ continent = Africa
• $ Export continent
• $ echo “$ continent”
Example-Exported Variable
$ continent = Africa
$ Export continent
$ echo “$ continent”
Africa
$ sh creates a new child
$ echo “$ continent”
Africa child shell has the variable continent
$
$ Continent = Asia gives a new value to continent
$ Echo “$ continent”
Asia
Press <ctrl>d
ENVIRONMENT VARIABLE
In a multiuser OS such as Linux, each user works on a copy of a shell.
Each shell has a set of special variables called environment variable.
Here user can change the value of variable using environment variables.
Example-PS1
$PS1 =”\w”
~ > cd \user\doc
/usr/doc
In the above example, after you change the value of the PS1 variable, the prompt display the tilde (~)
symbol, denoting your HOME directory
Example-PS2
$ PS1 =‘$
>
>‘
$_
In the above example, the value of the PS1 variable is enclosed within quotation marks but the
quotation marks are not closed
As a result, the command is considered incomplete
Notice that the > prompt is displayed because of the incomplete command
We can use the following command to change the PS2 prompt
$ PS2=^
$echo “This is incomplete
^close the quotes
^”
The LOGNAME VARIABLE
The user LOGNAME is stored in a log name variable .
Eg:
$ echo “${LOGNAME}”
You might or might not enclosed the LOGNAME variable in quotation mark (“) and braces {}.
Alternatively, you can given the following command.
$ echo $ LOGNAME
OUTPUT
USERNAME = root
HOSTTYPE =1380
OSTYPE=Linux-gnu
HOSTSIZE =1000
LAMHELPFILE = \ect/lam/lam-helpline.
Introduction
We 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 the following command
echo “The date is ‘date”
The command date is enclosed is a single backward quotation mark or grave accent.
While executing the command, the shell first replaces the enclosed command with the output of the
command.
Then the shells execute the entire command.
The following command would also display the same output.
echo “The date is $ (date)”
The shell replaces $(done) with the output of the date command.
Example-expr command
$a=4
$b=5
$ expr $a + $b
9
The expr command does not support decimals
The following command will display 2 and not 2.5 because the numeric value after the decimal point is
ignored
$ expr 5/2
Similarly, the following command will result in a syntax error because the decimal point in 2.5 will be
treated as a period
$var1=2.5
$expr $var1 +5
Therefore, the expr command will treat the value of the variable var1 as numeric
Example
1. $a=8
2. $b=4
3. $ expr $a-$b =4zz
4. $ expr $a+ $b =12
5. $ expr $a*$b =32
6. $ expr $a%$b =2
ARITHMETIC EXPRESSION
To calculate the value of an expression, we have to enclose expression in $((…………………)).
Syntax:
$((expression))
Example:
$echo $((45+55)
100.
The following flow chart representing the logic of the shell script.
EXAMPLE-ARITHMETIC EXPRESSION
$ a=25
$ b=56
$ echo $((a+b)
81
The following flow chart represents the logic required to solve the given problem
Example – Problem
1. Consider a scenario in which you need to create a shell script that calculates the number of
unanswered queries at the call center of ABC Telecommunications. The shell script should accept the total
number of queries reported in a day and record the number of queries answered to calculate the number
of unanswered queries
You can execute the shell script by using the following command
$ bash callpending
Alternatively, you can get execute permission for the shell script and execute it in the following manner
$ chmod +x callpending
$ ./callpending
Output
$ bash callpending
please enter the total no of queries reported today
120
please enter the no of queries answered
109
number of calls pending = 11