0% found this document useful (0 votes)
34 views25 pages

UNIT-IV-Linux Notes

The document discusses file access permissions in Linux. It covers viewing and changing permissions using the ls and chmod commands. Permissions are represented by characters for the owner, group, and others. The chmod command uses symbols to grant or revoke permissions for specific users.

Uploaded by

Abdullah Rabban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views25 pages

UNIT-IV-Linux Notes

The document discusses file access permissions in Linux. It covers viewing and changing permissions using the ls and chmod commands. Permissions are represented by characters for the owner, group, and others. The chmod command uses symbols to grant or revoke permissions for specific users.

Uploaded by

Abdullah Rabban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1

II BCA –IV SEMESTER


CORE 7: LINUX AND SHELL PROGRAMMING
UNIT-IV
Staff Name: Dr. J. Santhosh
---------------------------------------------------------------------------------------------------------------------------
UNIT IV: Securing files in LINUX: File access permissions – Viewing File access permissions –
Changing File access permissions. Automating Tasks using Shell Scripts: Introduction –
Variables- Local and Global Shell variables – Command Substitution
---------------------------------------------------------------------------------------------------------------------------
4.1`-Securing files in LINUX: File Access Permissions
Synopsis
1. Introduction: Multiuser environment
2. Linux system
3. Example for Linux system
4. Uses of file access permissions (FAPs)
5. Permission for a file or a directory
6. File Access permissions
INTRODUCTION
 In a multiuser environment, such as Linux, multiple users work on the system simultaneously.
 These users share system resources, such as hard disk, memory and devices attached to the
system
 Therefore, one of the important tasks of an administrator is to manage the files and directories
of the users and groups

Linux system
 Each user on the Linux system is assigned
 User login name
 Password
 Group or groups to which the user belongs

Example for Linux system


 Consider that, your working along with your coworkers working on a project, and all of you
need to
 Read
 Write or Execute a set of files
 Other users on the network should not have access to this set
 To do this, you can create a group and add the users working on the project to this group
 Now you can grant permissions to this group on the set of files so that they can be accessed

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


2

Uses of File Access Permissions (FAPs)


 It help to secure files and folders
 File access permissions refer to the permissions associated with a file with respect to the following
 File owner
 Group owner
 Other users

Permission for a file or a directory


 The following characters or Number are represented for accessing a permission for a file or directory

Table for File or directory Access permissions

4.2 – Viewing File Access Permission


Synopsis
1. Introduction
2. File access permission
3. File access permission in directory

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.

File Access Permission

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


3

 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.

File Access Permission using three characters


 The following three characters are the permissions for the group owner, and last three are the
permissions for other users
 If the permission is available, r, w or x is displayed; else a hyphen is displayed.
 In the above example, for the file a.out, the file owner is assigned the read, write and execute
permissions, while the group owner and other users are assigned the read execute permissions

File Access Permission in Directories


 The read permission allows a user to:
 List the content of a directory
 The write permission allows a user to:
 Copy files to a directory
 Remove files from a directory
 Remove the file in a directory

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


4

 Make a sub directory


 Remove a sub directory from the directory
 Move files to and from the directory
 The execute permission allow a user to:
 Change to a directory
 Display a file in a directory
 Copy a file from a directory

4.3 – Changing File Access Permission


Synopsis
1. Introduction
2. Chmod
3. Syntax for Chmod
4. Specification mode
5. Example
6. Symbolic mode
7. Example

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.

Syntax for chmod


Chmod <mode> <filename>

Specification mode
 You use the chmod command to specify the mode (permission to a type of user) in following ways:

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


5

 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

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


6

 ‘x’ indicate the execution permission


 ‘x.c’ represent the file name
 After the above command is executed, the FAPs for the file x.c will be modified as follows.
[steve@locahoststeve]$ ls -l x.c
-rwxrw-r- l steve IRDTECH 50 sep 22 21:01 x.c

 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

4.5 – Absolute Mode


Synopsis

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


7

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.

Absolute number of permission

Representation of Absolute Number


 The figure below depicts a sample representation of absolute numbers.

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.

Mode and output of the chmod command

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


8

Example
[steve@localhoststeve]$chmod 722 empfeedbk.txt

4.6- The umask Value


Synopsis
1. Introduction
2. File access permission
3. What is umask value
4. Specification of umask value
5. Syntax
6. Example of umask value
7. Value and output of UMASK command on file

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.

File Access Permission


 By default, umask value is given as 002 for users and 022 for root users, which means that each file
that a user creates has the permission
666-002 = 664(rw-rw-r-)
What is umask value

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


9

 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.

Specification of umask value


 The umask value is specified in the /etc/profile file.
 We can use the umask command to print or modify the umask value of files and directories
 To essure that the default file access permissions are same across the various sessions in linux
 The umask command is stored in the .bash_profile file
 When a new bash (Bourne Again Shell) shell is created, the umask command is executed automatically

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.

Value and Input of the Unmask Command On File


 The following table depicts the effect on files that are created by using the various umask values
in the table, the umask values are listed in decreasing order of security.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


10

4.8- Automating Tasks Using Shell Scripts


Synopsis
1. Introduction
2. The shell as a command interpreter
3. Unique features of Linux operating system
4. Process of command interpretation by the shell
5. Example
6. Echo command
7. Example for echo command

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.

Unique features of Linux OS


 All Linux commands exist as utilities.
 These utilities are located in individual files in any of the system directories such as /bin, /etc, or
/user/bin
 The shell enables a user to access all the utilities and resources of the computer.
 Whenever you enter text, the shell reads the first word at the command line to identify if it is an
 Alias
 a function or
 an internal command
 If the first word at the command line is a command, the shell searches the directories specified in the
path for the command
 When the shell finds the file associated with the command, it executes the file

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


11

4.9 - Process of Command Interpretation


 When you logon the system, the shell displays the Linux prompt, indicating that it is ready to
execute the commands.
 When you give a command, the shell perform the following task.
 Read the command
 Search the command
 Load the appropriate utility into memory.

Executes the Utility


 After completing the execution, the shell displays the prompt again, indicating that you can enter the
next command.

Figure: Command Interpretation by the shell

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.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


12

Syntax of Echo commands

$ echo “This is an example of the echo command”


This is an example of the echo command
$
Example

$ echo “this is an echo command”


 The above command display text enclosed between “ “ on this screens.
 By default, the echo command display the text and then places and new line character at the end of it.
 The new line character move the cursor to the next line after the text is displayed.
 To keep the cursor on the same line, you can use the –n option with the echo command.
 For example,
$ echo –n “this is an echo command”
 Notice that $ symbol is displayed on the same line as an output

4.11- Executing the Shell Scripts


Synopsis
1. Introduction
2. Creation of a script file
3. Example
4. Execution of the file
5. Login shell
6. Inserting comments
Introduction
 It can be used to automatically task.
 Consider a situation in which you have a sequential of Linux commands that you use frequently.
 Linux enables you to group the command and execute them at once.
 It can store commands in a file.

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.

Creation of Script File


 To create a script file you to need to write command in the text file by using text editor VI editor.
 After you create script file you need to execute it.

Example
 Consider the following a file magic.sh is created the execution.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


13

#!/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

Execution of the file


 You can execute the above magic file following ways, at the command prompts
$ bash magic
 If there is error in the 1st line of the shell script that you are executing, you might receive the “no such
file or directory” error message
 To resolve this problem, type the where is bash command to locate the path of the bash program, and
use the path in the shell script
 At the command prompts, you can give the following command for the execution.
$ chmod u +x magic change FAP
$./ magic.sh Execute the shell script
 To execute a shell script directly at the $ prompt, you need to change the FAPs for the script file.
 When you acquire the execute permission, you can execute the shell script by typing its name at the $
prompt

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”

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


14

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)

4.12. Inserting comments


 You can include command entries in a shell script by prefixing statement (./ pgm name) with the #
symbol.
 When the shell encounters the # symbol it ignores text following #symbol in a line.
Example
#!/bin/sh
Echo ”hello”
# This is a comment line. This would not display any output
echo “world”
 In the above program 3rd line is command
 The comment will be ignored by a shell and hence no output will be displayed.
 In a shell script, you can add comments about the structure and working of the shell script for future
reference
4.13 - Variables
Synopsis
1. Introduction.
2. Variables
3. Syntax for creating variable.
4. Example for creating variable.
5. Referencing Variable
Introduction
 In the bash shell, you do not need to declare variables explicitly
 Here variable can be created at any point of time by simply assigning a value.
 Here the variable can be created with or without value in a right side of the variable assignment
operator (=) blank.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


15

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.

Cannot declare variables as integers or characters


 In shell scripts, you cannot declare variables as integers or characters
 All the variables in the Linux operating system are treated as character strings
 However you can perform arithmetic operations on variables

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.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


16

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

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


17

 However you can use the echo command to display a prompt


$ echo “Enter your name”
Enter your name
$ read myname
ivin
 In the above example, when the echo command is executed, a message is displayed
 However, after the read myname command is executed, the operating system waits for input
 Let us, enter Ivin and press the <Enter> key.
 The value, Ivin is stored in the variable myname
 We can display the value of the myname variable by using the following command
$ echo $myname
Ivin
Flowchart
 The following flowchart representing the logic of the shellscript

Example program for entering customer data


 Example program for accepting customer name number and stored in a file using shell scripts.
 Program:
#!/bin/sh
echo “enter a name for customer”
read name
echo” enter a name of mobile number”
read number
echo “$name:$number”>>customer data.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


18

Example
 You can execute the above program using the following command.
$ chmod +x program.sh
$ ./program.sh
Output

Enter a name of customer


Francis
Enter a mobile phone number
97876543210.
Customer data
Francis
97876543210
4.14- Local & Global Variables
Synopsis
1. Introduction: Local variables & Global variables
2. Example program - Concept of the Local
Variable
3. Exported Variable
4. Environment Variable
5. The HOME Variable
6. The PATH Variable
7. PS1 (Prompts String1)
8. PS2 (Prompts String2)
9. The SHLVL Variable
10. The LOGNAME Variable

Introduction - Local & Global Variables


 When a new shell is created, it is unaware of the variables created in the parent shell.
 Now the same variable name can be given a different value without the parent shell knowing about
it.Such a variable is called local variable.
 Local variable is a variable that can change the value during the execution of a program is otherwise
known as Identifier.
 When you create a variable, it is accessible only in the shell in which you create it
 The subshells or parent shell of the current shell cannot access the variables of the current shell.
Example program - Concept of the Local Variable.
$ continent = Africa
$ echo “$continent”
Africa

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


19

$ sh Creates a child shell


$ echo “$ continent”
There is no response
$ continent= Asia Givens a new value, Asia to continent
Asia
Press <ctrl>d
$ exit Displays exit and returns to the parent shell
$echo “$continent”
Africa Parent is unaware of Asia
$ sh Creates another child shell
$ echo “$continent” Continent does not have any value
press <ctrl>d
$ exit Displays exit and returns to the parent shell
$_

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

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


20

$ Exit displays exit and returns to the parent shell


$echo “continent”
Africa Parent shell continues to have the value africa
$ sh

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.

Examples -Environment Variable


 Examples for environment variables are
 HOME
 PATH
 PS1
 PS2
 LOGNAME
 SHLVL
 SHELL

The HOME Variable


 In Linux OS, each user as an associated (connected) directory is called HOME DIRECTORY.
 Whenever the user logon the system, by default, the user is taken to the corresponding HOME
DIRECTORY.

Location of HOME directory


 The location of the home directory of a user is stored in the environment variable HOME.
 For e.g, if the home directory for the user ivin is /home/ivin, then the variable HOME will contain this
value.
 We can access the HOME variable the same way as you refer to other user defined variables
 For eg, the following command is used to display the HOME Variable.
$ echo $HOME
The PATH Variable
 It contains the list of colon (:) delimited path names of directories that are to be searched for
executable program.
 for eg, the following command specifies that the directories to be searched for any executable or
command are /user/bin and /bin in that order
$ PATH = /user/bin:/bin
 When we specify the PATH, you need to change to the directory containing the executable file.
 You can execute program, regardless of current working directory.

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


21

PS1 (Prompts String1)


 It is prompts string1 variable contains a shell program, $ symbol.
 You can change the shell prompts by editing the value of the PS1 variable.
 For example following command with change the prompt to HELLO>:
ps1 =”HELLO >” <ENTER>
HELLO> NEW PROMPT
 Note that, while changing the shell prompt you have the use of symbol “=” and give the new prompt
name (“hello”) within quotation marks.
 you can use \w string to display current working direct

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

PS2 (Prompts String2)


 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-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

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


22

The SHLVL Variable


 This variable contains the shell levels that are you are current working in.
 When workings on Linux, you create new shells and forget the level of shell in which you’re working.
 In such a situation, you can use the SHLVL variable.eg
$ echo $SHLVL
$sh
$ echo $SHLVL
2
$ exit
exit
 It stored the default shell of user you can view the value of the variable by using following command.
echo $ SHELL
/bin/sh
The env command
 It is use to view a list of all the exported environment variable and the respective values.
 eg
$ env
pwd =\root
REMOTEHOST = 172.17.68.97
HOSTNAME =server 2.mydomain.com
PVM-RSH=/usr/bin/rsh
QTDIR =/USR/LIB/QT-2.30

OUTPUT
USERNAME = root
HOSTTYPE =1380
OSTYPE=Linux-gnu
HOSTSIZE =1000
LAMHELPFILE = \ect/lam/lam-helpline.

4.15. Command Substitution

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)”

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


23

 The shell replaces $(done) with the output of the date command.

What is Command Substitution?


 Command substitution is an operation with dedicated syntax to both execute a command and to
have this command's output hold (stored) by a variable for later use.

The expr command


 It most shell, you cannot declare variable as integers
 All variables are treated as character strings.
 There for the following declaration, the variable var1 contains the char 2 & 5 and not the number 25.
 Eg
var1 = 25
 However, in Linux you can perform arithmetic operation by using the expr command to simulate
numeric variables and evaluate arithmetic expressions.
 The output expr command is send to standard output file.

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

Operator supported by expr command


 The following operator is use for expression.
1. +
2. -
3. *
4. %
5. \

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

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


24

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

Figure: Flowchart representing the Logic of the Shell Script

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS


25

PROGRAM TO CALCULATE UNANSWERED QUERIES


echo “please enter the total no of queries reported today”
read totalqueries
echo “please enter the no of queries answered”
read answered
pending=$((totalqueries-answered))
echo “number of calls pending = $pending”

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

All the Best

S. Ganeshmoorthy, AP/Dept. of IT, SKACAS

You might also like