linux os
linux os
Class : II B.Sc. CS
Semester : IV
Prepared by : Dr.R.Aswanandini
Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - IV
FILE ACCESS PERMISSIONS
INTRODUCTION
• Each file and directory has three user based permission groups:
• owner - The Owner permissions apply only the owner of the file or
directory, they will not impact the actions of other users.
• group - The Group permissions apply only to the group that has been
assigned to the file or directory, they will not effect the actions of other
users.
• all users - The All Users permissions apply to all other users on the
system, this is the permission group that you want to watch the most.
PERMISSION TYPES
• Each file or directory has three basic permission types:
• read - The Read permission refers to a user's capability to read the
contents of the file.
• write - The Write permissions refer to a user's capability to write or
modify a file or directory.
• execute - The Execute permission affects a user's capability to execute
a file or view the contents of a directory.
Viewing the Permissions
• You can view the permissions by checking the file or directory permissions in your
favorite GUI File Manager (which I will not cover here) or by reviewing the output of
the \"ls -l\" command while in the terminal and while working in the directory which
contains the file or folder.
The permission in the command line is displayed as: _rwxrwxrwx 1 owner:group
1.User rights/Permissions
1. The first character that I marked with an underscore is the special permission flag that can vary.
2. The following set of three characters (rwx) is for the owner permissions.
3. The second set of three characters (rwx) is for the Group permissions.
4. The third set of three characters (rwx) is for the All Users permissions.
2.Following that grouping since the integer/number displays the number of hardlinks to the
file.
3.The last piece is the Owner and Group assignment formatted as Owner:Group.
Unit - IV
CHANGING FILE ACCESS
PERMISSIONS
CHANGING FILE ACCESS PERMISSIONS
• To explicity define permissions you will need to reference the Permission Group and
Permission Types.
The Permission Groups used are:
• u - Owner
• g - Group
• o or a - All Users
• The potential Assignment Operators are + (plus) and - (minus); these are used to tell the
system whether to add or remove the specific permissions.
The Permission Types that are used are:
• r - Read
• w - Write
• x - Execute
CHANGING FILE ACCESS PERMISSIONS-
SYMBOLIC MODE
• To make this modification you would invoke the command:
chmod a-rw file1
To add the permissions above you would invoke the command:
chmod a+rw file1
To change directory permissions in Linux, use the following:
• chmod +rwx filename to add permissions.
• chmod -rwx directoryname to remove permissions.
• chmod +x filename to allow executable permissions.
• chmod -wx filename to take out write and executable permissions.
CHANGING FILE ACCESS PERMISSIONS
• The command for changing directory permissions for group
owners is similar, but add a “g” for group or “o” for users:
• chmod g+w filename
• chmod g-wx filename
• chmod o+w filename
• chmod o-rwx foldername
CHANGING FILE ACCESS PERMISSIONS
To change directory permissions for everyone, use “u” for users, “g” for
group, “o” for others, and “ugo” or “a” (for all).
• chmod ugo+rwx foldername to give read, write, and execute to
everyone.
• chmod a=r foldername to give only read permission for everyone.
ABSOLUTE MODE
You may need to know how to change permissions in numeric code in
Linux, so to do this you use numbers instead of “r”, “w”, or “x”.
• 0 = No Permission
• 1 = Execute
• 2 = Write
• 4 = Read
Basically, you add up the numbers depending on the level of permission
you want to give.
ABSOLUTE MODE
Permission numbers are:
• 0 = ---
• 1 = --x
• 2 = -w-
• 3 = -wx
• 4 = r-
• 5 = r-x
• 6 = rw-
• 7 = rwx
ABSOLUTE MODE
For example:
• chmod 777 foldername will give read, write, and execute permissions
for everyone.
• chmod 700 foldername will give read, write, and execute permissions
for the user only.
• chmod 327 foldername will give write and execute (3) permission for
the user, w (2) for the group, and read, write, and execute for the users.
THE UMASK VALUE
• Umask, or the user file-creation mode, is a Linux command that is
used to assign the default file permission sets for newly created folders
and files. The term mask references the grouping of the permission
bits, each of which defines how its corresponding permission is set for
newly created files. The bits in the mask may be changed by invoking
the umask command.
When using the term Umask, we are referring to one of the following
two meanings:
• The user file creation mode mask that is used to configure the default
permissions for newly created files and directories
• The command “umask” which is used to set the umask value
VALUES AND OUTPUT OF THE
UMASK COMMAND ON FILES
Unit - IV
Automating Tasks using Shell
Scripts
• The shell is the Linux command line interpreter. It provides an
interface between the user and the kernel and executes programs called
commands.
• For example, if a user enters ls then the shell executes
the ls command. The shell can also execute other programs such as
applications, scripts, and user programs (e.g., written in c or the shell
programming language).
The shell is a type of program called an interpreter. An interpreter operates in a simple loop:
It accepts a command, interprets the command, executes the command, and then waits for another
command.
The shell displays a "prompt," to notify you that it is ready to accept your command.
The echo command
• echo command in linux is used to display line of text/string that are
passed as an argument . This is a built in command that is mostly used
in shell scripts and batch files to output status text to the screen or a
file.
• Syntax:
echo [option] [string]
• \b : it removes all the spaces in between the text
• \c : suppress trailing new line with backspace interpretor ‘-e‘ to
continue without emitting new line.
• \n : this option creates new line from where it is used.
Executing a Shell Script
• Open the terminal. Go to the directory where you want to create your
script.
• Create a file with .sh extension.
• Write the script in the file using an editor.
• Make the script executable with command chmod +x <fileName>.
• Run the script using ./<fileName>.
Note: In the last step you have to mention the path of the script if your
script is in other directory.
INSERTING COMMENTS
• Comments can be added at the beginning on the line or inline with other code:
• EXAMPLE :
# This is a Bash comment.
"This is Code" # This is an inline Bash comment.
The blank space after the hash mark is not mandatory, but it will improve the comment’s readability.
If your text editor supports syntax highlighting, comments are usually represented in green.
Comments are also useful when testing a script. Instead of deleting some lines or blocks, you can comment
them out
Writing comments is a good practice and helps other developers, including future self, to understand the
shell script. In Bash, everything after the hash mark (#) and until the end of the line is considered to be a
comment.
VARIABLES
• A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign,
and delete variables.
• The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the
underscore character ( _).
CREATING VARIABLES
SYNTAX :
variable_name=variable_value
To access the value stored in a variable, prefix its name with the dollar sign ($)
EXAMPLE :
$!/bin/sh
NAME="Zara Ali"
echo $NAME
READ ONLY VARIABLE
• Shell provides a way to mark variables as read-only by using the read-only
command. After a variable is marked read-only, its value cannot be changed.
• For example, the following script generates an error while trying to change the
value of NAME −
#!/bin/sh
NAME="Zara Ali“
readonly
NAME
RESULT:
• /bin/sh:NAME:This variable is read only.
REFERENCING VARIABLES
• The $ symbol is used to refer to the contents of a variable.
$ x =$Goodmorning
$ x = $ { Goodmorning}
Both are same.
Reading a value into a variable
$Echo “Enter your name “
Enter your name
$ read myname
Kumar
$ Echo $myname
Kumar
LOCAL AND GLOBAL SHELL VARIABLES
• When a shell is running, three main types of variables are present −
• Local Variables − A local variable is a variable that is present within the
current instance of the shell. It is not available to programs that are started
by the shell. They are set at the command prompt.
• Environment Variables − An environment variable is available to any
child process of the shell. Some programs need environment variables in
order to function correctly. Usually, a shell script defines only those
environment variables that are needed by the programs that it runs.
• Shell Variables − A shell variable is a special variable that is set by the
shell and is required by the shell in order to function correctly. Some of
these variables are environment variables whereas others are local variables.
LOCAL SHELL VARIABLE EXAMPLE
$ vech=Bus
$ echo $vech
Bus
$ /bin/bash
$ echo $vech
NOTE:-Empty line printed
$ vech=Car
$ echo $vech
Car
$ exit
$ echo $vech
Bus
LOCAL SHELL VARIABLE EXAMPLE
GLOBAL SHELL VARIABLE
To set global varible you have to use export command.
Syntax:
export variable1, variable2,.....variableN
Examples:
$ vech=Bus
$ echo $vech
Bus
$ export vech
$ /bin/bash
$ echo $vech
Bus
$ exit
$ echo $vech
Bus
GLOBAL SHELL VARIABLE EXAMPLE
ENVIRONMENT VARIABLE
• Environment variables are variables that are available system-wide
and are inherited by all spawned child processes and shells.
• In Linux and Unix based systems environment variables are a set of
dynamic named values, stored within the system that are used by
applications launched in shells or subshells. In simple words, an
environment variable is a variable with a name and an associated
value.
• Environment variables allow you to customize how the system works
and the behavior of the applications on the system.
THE HOME VARIABLE
• The location of the home directory of a user is stored in the
environment variable HOME.
• $ echo $ home
THE PATH VARIABLE
• The path variable contains a list of colon-delimited path names of
directories that are to be searched for an executable program.
• By default, the Linux os does not search the current directory. Only
the directories specified in the path variable are searched.
THE PS1 VARIABLE
• The Prompt String 1 variable contains the shell prompt, the $ symbol.
We can change the shell prompt by editing the value of the PS1
variable.
• $ PS1 =“HELLO>”
• HELLO>
• No spaces should be there before and after = sign.
• $ ps1 = ‘/w>’
• This command displays the current working directory.
THE PS2 VARIABLE
• The PS2 variable specifies the value for secondary prompt.
• By default, the secondary prompt is > symbol.
• The Linux OS displays the secondary prompt when we type an
incomplete command on the second line.
We can use the following command to change the PS2 prompt.
$ PS2 = /
$ Echo “,,,
/
LOGNAME variable
• Users login name is stored in the LOGNAME variable
• $echo “$[LOGNAME]”
The SHLVL Variable
• The $SHLVL environment variable holds the number of shell levels
the current shell is running on top of.
• In a new terminal window, executing the following command will
produce different results based on the Linux distribution in use.
$ echo $SHLVL
THE SHELL VARIABLE
• A shell variable is a special variable that is set by the shell and is
required by the shell in order to function correctly.
echo $ SHELL
THE ENV COMMAND
Here are some common environmental variables
•SHELL: This describes the shell that will be interpreting any commands you type in.
In most cases, this will be bash by default, but other values can be set if you prefer other options.
•TERM: This specifies the type of terminal to emulate when running the shell.
• Different hardware terminals can be emulated for different operating requirements. You usually won’t need to worry about this though.
•USER: The current logged in user.
•PWD: The current working directory.
•OLDPWD: The previous working directory. This is kept by the shell in order to switch back to your previous directory by runningcd -.
•LS_COLORS: This defines color codes that are used to optionally add colored output to the ls command.
This is used to distinguish different file types and provide more info to the user at a glance.
•MAIL: The path to the current user’s mailbox.
•PATH: A list of directories that the system will check when looking for commands.
When a user types in a command, the system will check directories in this order for the executable.
•LANG: The current language and localization settings, including character encoding.
•HOME: The current user’s home directory.
•_: The most recent previously executed command.
Unit - IV
Command Substitution
Command Substitution
• the command substitution is used to combine more than one command in a command line. To display the date
message on the screen:
• The shell replaces $ (date) with the output of the date command.
The expr Command
• In most shells, we cannot declare variables as integers. All variables are treated as character strings.
Therefore, in the following declaration, the variable var1 contains the characters 2 and 5, and not the number
25:
• var1=25
• However, in Linux, can perform arithmetic operations by using the expr command to simulate numeric
variables and evaluate arithmetic expressions. The output of the expr command is sent to the standard output
file.
Example:
$ expr 4 + 5
The command in the above example will display 9 on the screen, need to add
a space on either side of the + operator.
Example:
$ a=5
$ b=4
$ expr $a + $b
9
Arithmetic Expansion
• Syntax
• $ ( ( expression) )
• Example
• $ echo $ ( ( 45 + 43 ) )
• 79
• To calculate the sum of the numbers, 45 and 34, enclose the numbers in (( )) along with the + operator.
While enclosing the expression in (( …)), can also specify variables instead of numbers in the expression.
• Example
• $ a = 25
• $ b = 56
• $ echo $ ( ( a + b ) )
• 81
THANK YOU