0% found this document useful (0 votes)
16 views58 pages

The Linux Shell

Module 3 of CSE 438 covers the Linux shell and file structure, detailing the functionality of shells, their history, and various commands for file and process management. It explains shell invocation, user commands, redirection, wildcards, pipes, and background processing, along with the creation and management of shell scripts. Additionally, it discusses shell variables, quoting, and job control commands.

Uploaded by

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

The Linux Shell

Module 3 of CSE 438 covers the Linux shell and file structure, detailing the functionality of shells, their history, and various commands for file and process management. It explains shell invocation, user commands, redirection, wildcards, pipes, and background processing, along with the creation and management of shell scripts. Additionally, it discusses shell variables, quoting, and job control commands.

Uploaded by

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

Amity School Of Engineering And Technology

CSE 438: Linux For Devices


Module 3
The Linux Shell and File Structure

1
Amity School Of Engineering And Technology

Shell
 Shell is the user interface to the operating system
 Functionality:
 Manage files (wildcards, I/O redirection)
 Manage processes (build pipelines, multitasking)
 Most popular shells:
 The Bourne shell (sh)
 The Korn shell (ksh)
 The C shell (csh)
 The Bourne Again shell (bash)
Amity School Of Engineering And Technology

Shell
 Shell is the user interface to the operating system
 Functionality:
 Manage files (wildcards, I/O redirection)
 Manage processes (build pipelines, multitasking)
 Most popular shells:
 The Bourne shell (sh)
 The Korn shell (ksh)
 The C shell (csh)
 The Bourne Again shell (bash)
Amity School Of Engineering And Technology

A Bit of History
 The Bourne shell /bin/sh ( S. R. Bourne, 1977)
 powerful syntactical language
 strong in controlling input and output
 expression matching facilities
 interactive use: the use of shell functions.
 The C-shell /bin/csh (Bill Joy, UCB, 1978)
 new concepts: job control and aliasing
 much better for interactive use
different input language:
 out went the good control of input and output
 too buggy to produce robust shell scripts
Amity School Of Engineering And Technology

A Bit of History
 /bin/tsch (Ken Greer, Carnegie Mellon University,
late 1970s)
 User –oriented command line editing
 Out most of the bugs
 Korn shell /bin/ksh (David Korn, AT&T, early
1980s)
 Bourne shell language
 C shell’s features for interactive work
You had to pay AT&T for it!
 GNU project: a free shell=> /bin/bash (the Bourne
again shell)
Amity School Of Engineering And Technology

SELECTING A SHELL

[c33225@snowball ~]$ echo $SHELL


/bin/bash
[c33225@snowball ~]$ bash
[c33225@snowball ~]$ exit
exit
[c33225@snowball ~]$ ksh
$ exit
[c33225@snowball ~]$ sh
sh-3.2$ exit
exit
[c33225@snowball ~]$ csh
[c33225@snowball ~]$ exit
exit
Amity School Of Engineering And Technology

Changing a Shell
 To change your default shell use the chsh utility
which requires full pathname of the new shell
Amity School Of Engineering And Technology

Core Shell Functionality


Amity School Of Engineering And Technology

Invoking the Shell

 A shell is invoked, either


 automatically upon login, or
 manually from the keyboard or
script
Amity School Of Engineering And Technology

What does the shell do?


 The following takes place:
 (1) reads a special startup file (.cshrc for
csh in the user's home directory) and
executes all the commands in that file
 (2) displays a prompt and waits for a user
command
 (3) If user enters CTRL-D (end of input)
the shell terminates, otherwise it
executes the user command(s)
Amity School Of Engineering And Technology

User Commands
 ls (list files), ps (process info),
 \ continues line, lp (send to printer)

$ ls

$ ps -ef | sort | ul -tdumb | lp

$ ls | sort | \
lp
Amity School Of Engineering And Technology

UNIX Commands vs Built-in Shell Commands


Shell locate the execute program in the “/bin”
directory and executes it
 Example: ls

Shells have built-in commands, which it


recognizes and executes internally
 Example: echo, cd
Amity School Of Engineering And Technology

echo
 Built-in shell command
 Displays its arguments to standard output
 By default, appends a newline to the output
 Subtle behavior may vary among different
shells
Amity School Of Engineering And Technology

Metacharacters
• Metacharacters are characters that specially
processed by a shell.
• When you enter a command, shell
• scans for metacharacters,
• specially processes them,
• then executes command.
• To turn off the meaning of a metacharacter: \
[c33225@snowball ~]$ echo hi>file
[c33225@snowball ~]$ cat file
hi
[c33225@snowball ~]$ echo hi \>file
hi >file
[c33225@snowball ~]$
Amity School Of Engineering And Technology

Shell Metacharacters
Amity School Of Engineering And Technology

Output Redirection
 command > file
 Creates file if it doesn’t exist; Overwrites if file
exists
 If file exists and hasn’t write permission, an
error occur
 command >> file
 Creates file if it doesn’t exist, Appends to file if
file exists [c33225@snowball ~]$ echo hi>file
[c33225@snowball ~]$ cat file
hi
[c33225@snowball ~]$ echo Hello. world >> file
[c33225@snowball ~]$ cat file
hi
Hello. world
Amity School Of Engineering And Technology

Input Redirection
 command < file
 If file doesn’t exist or hasn’t read permission,
an error occur
 command << word
 Copies its standard input up to (not including)
the line starting with word into buffer which is
used as input
Amity School Of Engineering And Technology

Wildcards: Filename Substitution


 Globbing is process of replacement of the pattern
with metacharacters by an alphabetically sorted
list of all matching filenames.

 If string is surrounded by single (double) quotes,


shell doesn’t process wildcards in the string.
Amity School Of Engineering And Technology

Wildcards Example
[c33225@snowball ~]$ ls *.script
awk2.script awk.script
[c33225@snowball ~]$ ls aw?.script
awk.script
[c33225@snowball ~]$ ls [ef]*
ex1.tar ex1.txt ex2.txt file file1.tar file.tar
[c33225@snowball ~]$ ls *.perl
ls: *.perl: No such file or directory
Amity School Of Engineering And Technology

Pipes
 Pipe metacharacter : |
 command1 | command 2 | command 3
 Such sequence of commands is called pipeline
 Large problem can be often solved by a chain
of smaller processes,
 each performed by a relatively small, reusable
utility
 The standard error channel is not piped through a
standard pipeline (some shell supports it).
 ls | wc -w
Amity School Of Engineering And Technology

tee
 Causes standard input to be copied to file and
also sent to standard output.

 tee –ia {fileName}+


 -a: appends the input to the file instead of
overwriting
 -i: ignores interrupts

https://linuxize.com/post/linux-tee-command/​
Amity School Of Engineering And Technology

tee Example
[c33225@snowball ~]$ who
c33225 pts/1 2010-06-29 22:13 (adsl-92-195-16.asm.bellsouth.net)
sou pts/2 2010-06-29 11:46 (ascsc-1408-w7.cs.gsu.edu)
c33201 pts/3 2010-06-29 20:51 (c-98-219-41-48.hsd1.ga.comcast.net)

[c33225@snowball ~]$ who | tee who.capture | sort


c33201 pts/3 2010-06-29 20:51 (c-98-219-41-48.hsd1.ga.comcast.net)
c33225 pts/1 2010-06-29 22:13 (adsl-92-195-16.asm.bellsouth.net)
sou pts/2 2010-06-29 11:46 (ascsc-1408-w7.cs.gsu.edu)
Amity School Of Engineering And Technology

Command Substitution
 A command in grave accents (`) is executed
 Its standard output is inserted in the command
in its place
 Any newlines in the output are replaced with
spaces.
Amity School Of Engineering And Technology

Sequences
 Commands or pipelines separated by semi-colons
 Executed from left to right

[c33225@snowball ~]$ date > date.txt; ls; pwd > pwd.txt; who | sort >
sort.txt; cat date.txt; cat pwd.txt; cat sort.txt
awk2.script date.txt file practice script.ksh temp
awk.script ex1.tar file1.tar pwd.txt script.sh testif.sh
databook1.txt ex1.txt file.tar readme.sh sort.txt who.capture
databook.txt ex2.txt letter script.csh temo.txt.4014
Tue Jun 29 23:27:27 EDT 2010
/home/c33225
c33225 pts/1 2010-06-29 22:13 (adsl-92-195-16.asm.bellsouth.net)
sou pts/2 2010-06-29 11:46 (ascsc-1408-w7.cs.gsu.edu)
Amity School Of Engineering And Technology

Conditional Sequences
 Every Unix process terminates with an exit
value:
 0 means successful completion
 Non zero means failure
 Built-in shell commands return 1 if they fail.
 &&: executes the next commands only if the
previous command returns 0.
 ||: executes the next command only if the
previous command returns a nonzero exit code
Amity School Of Engineering And Technology

Conditional Sequences
Amity School Of Engineering And Technology

Grouping Commands
 Commands can be grouped by placing them
between ()
 Grouped commands are executed by a child
shell (subshell).
 Grouped commands share standard input,
standard output and standard error channels.
 Group can be redirected and piped.
Amity School Of Engineering And Technology

Grouping Example
Amity School Of Engineering And Technology

Background Processing
 Command, or pipeline, or sequence, or group
of commands is followed by &:
 A subshell is created to execute the
commands as a background process
 Runs concurrently with a parent shell
 Does not take control over keyboard
 Returns user job number and system
process number
[c33225@snowball ~]$ find . -name '*.sh' -print &
[1] 4901
[c33225@snowball ~]$ ./readme.sh
./testif.sh
./script.sh
Amity School Of Engineering And Technology

BACKGROUND PROCESSING
 Redirect the output to a file (if desired)
 prevents background output on terminal
 find . – name ‘*.c’ –print > find.txt &

 Background process cannot read from


standard input
 If they attempt to read from standard input; they
terminate.
Amity School Of Engineering And Technology

Shell Programming: Scripts


• Script is a text file containing series of shell commands for later execution.
• Execute permission (chmod +x scriptName)
• When script is run
• System determines which shell should be used
• Executes the shell with script as input
• If the first line is
• #: current shell
• #!pathName: the executable program pathName
• otherwise: Bourne shell
Amity School Of Engineering And Technology

Shell Programming: Scripts


Subshells
Amity School Of Engineering And Technology

• Several ways a parent shell can create a child


shell (subshell)
• Group of commands are executed
• Script is executed
• Background job (concurrent execution)
• Parent shell sleeps till child shell terminates
(execution in background can be useful)
• A subshell has its own working directory
• cd commands in subshell do not change working directo ry of parent shell
Amity School Of Engineering And Technology

Shell’s Data Areas

 Every shell has two data Parent shell


Child shell
areas Environment
- environment space Environment
Local
- local-variable space Local: clean

 Child shell gets a copy of


the parent's environment
space
- starts with an empty local-
variable space.
Variables Amity School Of Engineering And Technology
 User defined variables within a shell are known as Shell Variables
 A shell supports two kinds of variables:
 Local variables
 Environment variables
 Both hold data in string format
 Every shell has a set of pre-defined environment variables and local
variables.
 A variable name may be any set of alphabetic characters including
underscore.
 A name may also include a number , but the number cannot be the first character
in the name.
 A name may not have any other type of character, such as ?, & or even space
 A name may not include more than one word
 Shell uses spaces on the command line to distinguish different components of a
command such as options, arguments and name of the command
 A Value to a variable is assigned using assignment operator
 Poet=Virgil
 Accessing variables in all shells is done by prefixing the name with a $ sign.
Variables Amity School Of Engineering And Technology
• Some pre-defined environment variables available in all shells:
• $HOME : full pathname of home directory
• $PATH : list of directories to search for commands
• $MAIL : the full pathname of mailbox
• $USER : your username
• $SHELL : the full pathname of login shell
• $TERM : the type of your terminal
Amity School Of Engineering And Technology

Assigning values to variables


• Depends on shell:
• sh, bash, ksh:
• variable=value
• variable="value"
• Notice no spaces around equal sign
• csh:
• set variable=value
• set variable="value"
• Access the variable: append prefix $ to the name of a variable.
• $variableName
Amity School Of Engineering And Technology

Assigning Variables

 set command is used to obtain list of all defined variables and their values.
 The unset command undefines a variables
Amity School Of Engineering And Technology

Built-In Variables
 $$ the process ID of the shell

 $0 the name of the shell script

 $1..$9 $n refers to the nth command line argument

$* a list of all command-line arguments


Example
Amity School Of Engineering And Technology
Quoting Amity School Of Engineering And Technology

 -Single quotes(‘) inhibit wildcard replacement, variable


substitution and command substitution.
 -Double quotes (“) inhibit wildcard replacement only.
 -When quotes are nested, only the outer quotes have
any effect.
Amity School Of Engineering And Technology
Amity School Of Engineering And Technology

Job Control
 ps : allows to monitor a status of
processes.
 kill: allows you to terminate a process on
the basis of its ID number
 wait: allows a shell to wait for one of its
child processes to terminate
 sleep seconds: sleeps for the specified
number of seconds, then terminates
Amity School Of Engineering And Technology

ps Command
 $ ps -efl
• - e: include all running processes
• - f: include full listing
• - l: include long listing
 PID : process ID
Amity School Of Engineering And Technology

Signaling Processes: kill


• kill [signalId] {pid}+
• kill –l
• -l: list valid signal names
• Sends the signal with code signal-Id to the list of
numbered processes.
• signalId is the number or name of the signal
• if signal is not specified the default signal is SIGTERM (15)
• SIGKILL (9) is useful if the process refuses to die
Amity School Of Engineering And Technology

Signaling Processes: kill


 Processes may protect themselves from all signals except KILL
 Process wouldn’t be allowed to clean up and terminate normally, as it would
do when it receives TERM
 The kill utility allows you to specify 0 as pid
 All processes associated with shell would be terminated.
Waiting for a Child Process: wait
Amity School Of Engineering And Technology

wait pid
 -Causes the shell to suspend operation until the child
process with the specified process ID terminates.
 -If no arguments are specified, shell waits for all of its
child processes to terminate.
Finding a Command: $PATH
Amity School Of Engineering And Technology
-Shell checks if command
is built-in.
 -If yes, executes it
directly.
-Otherwise, checks
whether it begins with /
 -If yes, shell assumes
that it is the absolute
pathname of a
command and tries to
execute the file with
such absolute
pathname.
 -If there is no such file
or it’s not executable,
an error occur.
Amity School Of Engineering And Technology

Finding a Command: $PATH


 Else, shell searches the directories (from left to right) in
the PATH environment variable.
 If match is found, the file is executed.
 If match is not found, or it’s not an executable, an error
occurs.
 If PATH is not set or is empty string, only current directory
is searched.
Amity School Of Engineering And Technology

Termination
• Bourne, Korn and Bash shells: special variable $? Which contains
the value of the previous command’s exit code.
• C shell: $status holds the exit code.

• Any script written by you should contain the exit command:


• exit <number>
• If the script does not exit with a exit code, the exit code of the last command is returned by
default.
Amity School Of Engineering And Technology

Core Built-In Commands: eval


 eval is a built-in linux command which is used to execute
arguments as shell Command.
 Combines arguments into a single string and uses it as an input to the
shell and execute the commands
 Syntax: eval [arg ...]
Amity School Of Engineering And Technology

Core Built-In Commands: exec


 Causes the shell’s image to be replaced with
command in the process’ memory space.
 The exec command does not spawn a new
process. Instead, exec command is executed in
place of the current shell .
 If commands is successfully executed, the shell
that performed the exec ceases to exist.
 If it was login shell, the login session is terminated
when command terminates.
Amity School Of Engineering And Technology

Common Core Built-in commands


 exec command
 The exec shell command causes the
shell's image to be replaced with the
command in the process' memory space.
 As a result, if the command terminates,
the shell also ceases to exist; If the shell
was a login shell, the login session
terminates.
Amity School Of Engineering And Technology

Core Built-In Commands: shift


 This command causes all of the positional
parameters $2..$n to be renamed $1..$(n-1)
and $1 is lost.
 Useful in processing command line
parameters. $ cat script3.csh
» #!/bin/csh
» echo first argument is $1, all args are $*
» shift
» echo first argument is $1, all args are $*
»
» $ script3.csh a b c d
» first argument is a, all args are a b c d
» first argument is b, all args are b c d
Umask Amity School Of Engineering And Technology

 On Linux and Unix operating system, all new files are created
with a default set of permissions.
 The umask utility allows you to view or to set the file mode
creation mask, which determines the permissions bits for
newly created files or directories.
 It is used by mkdir, touch, tee , and other commands that
create new files and directories.
 Every linux file is associated with an owner and a group and
assigned with permission access rights for three different
classes of users:
 The file owner.
 The group members.
 Everyone else.
 There are three permissions types that apply to each class. 55
Amity School Of Engineering And Technology

umask Command
 Linux does not allow a file to be created with execute
permissions
 Default permission values are
 File 666 => read and write permission to users, group and others
 Directory 777 => read, write and execute permission to users, group
and others
 Default creation permissions can be modified
using umask utility
 Every Unix process has a special quantity called umask value.
 Default value: 022 octal

https://linuxize.com/post/umask-command-in-linux/
umask Command
Amity School Of Engineering And Technology

 To calculate the umask value, simply subtract the desired


permissions from the default one:
 Umask value: 777-750 = 027
 E.g. made by vi or by redirection
 File permissions (usually 666) masked with umask value
 Example: 022 to produce the permission 644
 To see current umask value:
 $ umask
 To change umask value:
 $ umask octalValue
Amity School Of Engineering And Technology

Summary
• Covered core shell functionality
• Built-in commands
• Scripts
• Variables
• Redirection
• Wildcards
• Pipes
• Subshells
• Background processing

You might also like