linux tools shell script.. computer science
UNIX Shells
“UNIX for Programmers and Users”
Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES
UNIX Shells
UNIX Shells
*
• A shell is a program that sits between you and the raw UNIX
operating system.
There are four shells that are commonly supported by UNIX vendors:
the Bourne shell(sh), the Korn shell(ksh), the C shell(csh) and Bourne Again Shell (bash).
Bash is becoming the most popular shell - it is the default shell in Linux.
*
UNIX Shells
*
INTRODUCTION
A shell is a program that is an interface between a user and
the raw operating system.
It makes basic facilities such as multitasking and piping easy
to use, and it adds useful file-specific features such as wildcards
and I/O redirection.
There are four common shells in use:
· the Bourne shell
· the Korn shell
· the C shell
· the Bash shell (Bourne Again Shell)
UNIX Shells
*
SHELL FUNCTIONALITY
- This chapter describes the common core of functionality that
all four shells provide.
Here is a diagram that illustrates the relationship among the
four shells:
Common
core
Common
core
Bourne shell
Korn shell
C shell
Bourne Again Shell
UNIX Shells
*
SELECTING A SHELL
When you are provided with a UNIX account,
the system administrator chooses a shell for you.
To find out which shell was chosen for you, look at your prompt.
If you have a $ prompt, you’re probably in a Bash, Bourne or a
Korn shell.
If you have a % prompt, you’re probably in a C shell.
$ echo $SHELL ---> display the name of current login shell.
/bin/bash ---> full pathname of the Korn shell.
UNIX Shells
*
Utility : chsh
- chsh allows you to change your default login shell.
It prompts you for the full pathname of the new shell,
which is then used as your shell for subsequent logins.
- In order to use chsh, you must know the full pathnames of
the three shells. Here they are:
Shell Full pathname
Bourne /bin/sh
Bash /bin/bash
Korn /bin/ksh
C /bin/csh
UNIX Shells
*
SELECTING A SHELL
In the following example, we change the default login shell from
a Bourne shell to a Bash shell:
$ chsh ---> change the login shell from sh to bash.
Changing login shell for glass
Old shell : /bin/sh ---> pathname of old shell is displayed.
New shell: /bin/bash ---> enter full pathname of new shell.
$ ^D ---> terminate login shell.
login : glass ---> log back in again.
password : ---> secret.
$ _ ---> this time we are in a bash shell.
UNIX Shells
*
SHELL OPERATIONS
When a shell is invoked, either automatically during a login or
manually from a keyboard or script, it follows a preset sequence:
1. It reads a special startup file, typically located in the user’s
home directory, that contains some initialization information.
2. It displays a prompt and waits for a user command.
3. If the user enters a Control-D character on a line of its own,
this command is interpreted by the shell as meaning “end of
input”, and it causes the shell to terminate;
otherwise, the shell executes the user’s command and
returns to step 2.
UNIX Shells
*
Change your password
$ passwd
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
UNIX Shells
*
SHELL OPERATIONS
Commands range from simple utility invocations like:
$ ls -
to complex-looking pipeline sequences like:
$ ps -ef | sort | lp
ps: provide information about the currently running processes
The -e option generates a list of information about every process currently running.
The -f option generates a listing that contains fewer items of information for each process than the -l option
.
- a command with a backslash(\) character, and the shell will
allow you to continue the command on the next line:
$ echo this is a very long shell command and needs to \
be extended with the line-continuation character. Note \
that a single command may be extended for several lines.
$ _
UNIX Shells
*
EXECUTABLE FILES VERSUS BUILT-IN COMMANDS
Most UNIX commands invoke utility programs that are stored
in the directory hierarchy.
Utilities are stored in files that have execute permission.
For example, when you type
$ ls
the shell locates the executable program called “ls”, which is
typically found in the “/bin” directory, and executes it.
UNIX Shells
*
Displaying Information : echo
The built-in echo command displays its arguments to standard
output and works like this:
Shell Command: echo {arg}*
echo is a built-in shell command that displays all of its
arguments to standard output.
By default, it appends a new line to the output.
UNIX Shells
*
Changing Directories : cd
The built-in cd command changes the current working directory
of the shell to a new location.
METACHARACTERS
Some characters are processed specially by a shell and
are known as metacharacters.
All four shells share a core set of common metacharacters,
whose meanings are as follow:
UNIX Shells
*
METACHARACTERS
Symbol Meaning
> Output redirection; writes standard output to a file.
>> Output redirection; appends standard output to a file.
< Input redirection; reads standard input from a file.
* File-substitution wildcard;
matches zero or more characters.
? File-substitution wildcard;
matches any single character.
[…] File-substitution wildcard;
matches any character between the brackets.
UNIX Shells
*
Symbol Meaning
| Pipe symbol; sends the output of one process to the
input of another.
; Used to sequence commands.
|| Conditional execution;
executes a command if the previous one fails.
&& Conditional execution;
executes a command if the previous one succeeds.
(…) Groups commands.
& Runs a command in the background.
# All characters that follow up to a new line are ignored
by the shell and program(i.e., used for a comment)
$ Expands the value of a variable.
\ Prevents special interpretation of the next character.
<<tok Input redirection; reads standard input from script up to tok.
UNIX Shells
*
- When you enter a command,
the shell scans it for metacharacters and processes them specially.
When all metacharacters have been processed,
the command is finally executed.
To turn off the special meaning of a metacharacter,
precede it by a backslash(\) character.
Here’s an example:
$ echo hi > file ---> store output of echo in “file”.
$ cat file ---> look at the contents of “file”. “concatenate“
hi
$ echo hi \> file ---> inhibit > metacharacter.
hi > file
$ cat file ---> look at the file again.
hi ---> > is treated like other characters.
$ _
UNIX Shells
*
• Redirection
The shell redirection facility allows you to:
1) store the output of a process to a file ( output redirection )
2) use the contents of a file as input to a process ( input redirection )
Output redirection
To redirect output, use either the “>” or “>>” metacharacters.
The sequence
$ command > fileName
sends the standard output of command to the file with name fileName.
The shell creates the file with name fileName if it doesn’t already exist
or overwrites its previous contents if it does already exist.
UNIX Shells
*
- If the file already exists but doesn’t have write permission,
an error occurs.
In the next example, we create a file called “alice.txt” by redirecting
the output of the cat utility.
Without parameters, cat simply copies its standard input --- which,
in this case, is from the keyboard --- to its standard output.
$ cat > alice.txt ---> creates a text file.
In my dreams that fill the night,
I see your eyes,
^D ---> end of input.
$ cat alice.txt
In my dreams that fill the night, ---> look at its contents.
I see your eyes,
$ _
UNIX Shells
*
- The sequence
$ command >> fileName
appends the standard output of command to the file with name fileName.
The shell creates the file with name fileName if it doesn’t already exist.
In the following example, we appended some text to the existing ‘alice.txt’
file:
$ cat >> alice.txt ---> append to the file.
And I fall into them,
Like Alice fell into Wonderland.
^D ---> end of input.
$ cat alice.txt ---> look at the new contents.
In my dreams that fill the night,
I see your eyes,
And I fall into them,
Like Alice fell into Wonderland.
$ _
UNIX Shells
*
The Bash, C and Korn shells also provide protection against accidental
overwriting of a file due to output redirection.
In Bash:
$ set -o noclobber
$ echo text > test
$ echo text > test
bash: test: cannot overwrite existing file
$ echo text >| test
$ _
you can explicitly override the setting of noclobber with the >| redirection operator - the redirection will work, even if noclobber is set.
UNIX Shells
*
• Input Redirection
Input redirection is useful because it allows you to prepare a process
input beforehand and store it in a file for later use.
To redirect input, use either the ‘<‘ or ‘<<‘ metacharacters.
The sequence
$ command < fileName
executes command using the contents of the file fileName
as its standard input.
If the file doesn’t exist or doesn’t have read permission,an error occurs.
UNIX Shells
*
- In the following example,
we send the contents of “alice.txt” via the mail utility:
$ mail glass < alice.txt ---> send myself mail.
$ mail ---> look at my mail.
Mail version SMI 4.0 Sat Oct 13 20:32:29 PDT 1990 Type ? for help.
>N 1 glass@utdallas.edu Mon Feb 2 13:29 17/550
& 1 ---> read message #1.
From: Graham Glass <glass@utdallas.edu>
To: glass@utdallas.edu
In my dreams that fill the night,
I see your eyes,
And I fall into them,
Like Alice fell into Wonderland
& q ---> quit mail.
$ _
UNIX Shells
*
- When the shell encounters a sequence of the form
$ command << word
- it copies its standard input up to, but not including,
the line starting with word into a buffer and then executes command
using the contents of the buffer as its standard input.
- that allows shell programs( scripts ) to supply the standard input to
other commands as in-line text,
$ cat << eof
> line 1
> line 2
> line 3
> eof
line 1
line 2
line 3
$ _
$ cat > myf <<eof
- 1
- 2
- 3
- eof
$ cat myf
1
2
3
UNIX Shells
*
• FILENAME SUBSTITUTION( WILDCARDS )
- All shells support a wildcard facility that allows you to select files
that satisfy a particular name pattern from the file system.
- The wildcards and their meanings are as follows:
Wildcard Meaning
* Matches any string, including the empty string.
? Matches any single character.
[..] Matches any one of the characters between the brackets.
A range of characters may be specified by separating
a pair of characters by a hyphen.
UNIX Shells
*
- Prevent the shell from processing the wildcards in a string
by surrounding the string with single quotes(apostrophes) or double
quotes.
- A backslash(/) character in a filename must be matched explicitly.
Here are some examples of wildcards in action:
$ ls -FR ---> recursively list the current directory.
a.c b.c cc.c dir1/ dir2/
dir1:
d.c e.e
dir2:
f.d g.c
$ ls *.c ---> list any text ending in “.c”.
a.c b.c cc.c
$ ls ?.c ---> list text for which one character is followed by “.c”.
a.c b.c
UNIX Shells
*
$ ls [ac]* ---> list any string beginning with “a” or “c”.
a.c cc.c
$ ls [A-Za-z]* ---> list any string beginning with a letter.
a.c b.c cc.c
$ ls dir*/*.c ---> list all files ending in “.c” files in “dir*”
---> directories ( that is, in any directories beginning
with “dir” ).
dir1/d.c dir2/g.c
$ ls */*.c ---> list all files ending in “.c” in any subdirectory.
dir1/d.c dir2/g.c
$ ls *2/?.? ?.? ---> list all files with extensions in “*2” directories
and current directory.
a.c b.c dir2/f.d dir2/g.c
$ _
UNIX Shells
*
• PIPES
- Shells allow you to use the standard output of one process
as the standard input of another process by connecting the processes
together using the pipe(|) metacharacter.
- The sequence
$ command1 | command2
causes the standard output of command1 to “flow through” to
the standard input of command2.
- Any number of commands may be connected by pipes.
A sequence of commands chained together in this way
is called a pipeline.
UNIX Shells
*
- Pipelines support one of the basic UNIX philosophies, which is that
large problems can often be solved by a chain of smaller processes,
each performed by a relatively small, reusable utility.
- In the next example, we pipe the output of the ls utility to the input
of the wc utility in order to count the number of files in the current
directory.
$ ls ---> list the current directory.
a.c b.c cc.c dir1 dir2
$ ls | wc -w
5
$ _
UNIX Shells
*
$ head -4 /etc/passwd ---> look at the firt 4 lines of the password file.
root:eJ2S10rVe8mCg:0:1:Operator:/:/bin/csh
nobody:*:65534:65534::/:
daemon:*:1:1::/:
sys:*:2:2::/:/bin/csh
$ cat /etc/passwd | awk -F: ‘{ print $1 }’ | sort
audit
bin
daemon
glass
ingres
news
nobody
sync
sys
tim
uucp
$ _
AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines
-F fs : Use fs for the input field separator
Examples:
if fs is : then then colon is field separator
if fs is , then the comma is field separator
No fs then space is field separator
The /etc/passwd file is a colon-separated file that contains the following information:
-User name
-Encrypted password
-User ID number (UID)
-User's group ID number (GID)
-Full name of the user
-User home directory
-Login shell
UNIX Shells
*
- Here’s an illustration of the pipeline that we just used:
ls Pipe awk Pipe sort Terminal
In the example, we pipe the contents of the “/etc/passwd” file
into the awk utility to extract the first field of each line.
The output of awk is then piped to the sort utility,
which sorts the lines alphabetically.
The result is a sorted list of every user on the system.
UNIX Shells
*
• Utility : tee -ia -{fileName}+
The tee utility copies its standard input to the specified files and to its
standard output.
The -a option causes the input to be appended to the files rather
than overwriting them.
The -i option causes interrupts to be ignored.
- In the following example, we copy the output of who to a file called
“who.capture” and also let it pass through to sort:
$ who | tee who.capture | sort
ables ttyp6 May 3 17:54 ( waterloo.com )
glass ttyp0 May 3 18:49 ( bridge05.utdalla )
posey ttyp2 May 23 17:44 ( blackfoot.utdall )
posey ttyp4 May 23 17:44 ( blackfoot.utdall )
$ cat who.capture ---> look at the captured data.
glass ttyp0 May 3 18:49 ( bridge05.utdalla )
posey ttyp2 Apr 23 17:44 ( blackfoot.utdall )
posey ttyp4 Apr 23 17:44 ( blackfoot.utdall )
ables ttyp6 May 3 17:54 ( waterloo.com )
UNIX Shells
*
• COMMAND SUBSTITUTION
A command surrounded by grave accents (‘) - back quote - is executed,
and its standard output is inserted in the command’s place in the entire
command line.
Any new lines in the output are replaced by spaces.
For example:
$ echo the date today is ‘date`
the date today is Mon Feb 2 00:41:55 CST 1998
$ _
UNIX Shells
*
- By piping the output of who to the wc utility,
it’s possible to count the number of users on the system:
$ who ---> look at the output of who.
posey ttyp0 Jan 22 15:31 ( blackfoot:0.0 )
glass ttyp3 Feb 3 00:41 ( bridge05.utdalla )
huynh ttyp5 Jan 10 10:39 ( atlas.utdallas.e )
$ echo there are ‘who | wc -l` users on the system
there are 3 users on the system
$ _
UNIX Shells
*
• SEQUENCES
If you enter a series of simple commands or pipelines separated by
semicolons, the shell will execute them in sequence, from left to right.
This facility is useful for type-ahead(and think-ahead) addicts who like
to specify an entire sequence of actions at once.
Here’s an example:
$ date; pwd; ls ---> execute three commands in sequence.
Mon Feb 2 00:11:10 CST 1998
/home/glass/wild
a.c b.c cc.c dir1 dir2
$ _
UNIX Shells
*
- Each command in a sequence may be individually I/O redirected as well:
$ date > date.txt; ls; pwd > pwd.txt
a.c b.c cc.c date.txt dir1 dir2
$ cat date.txt
Mon Feb 2 00:12:16 CST 1998
$ cat pwd.txt ---> look at output of pwd.
/home/glass
$ _
UNIX Shells
*
• Conditional Sequences
- Every UNIX process terminates with an exit value.
By convention, an exit value of 0 means that the process completed
successfully, and a nonzero exit value indicates failure.
- All built-in shell commands return a value of 1 if they fail.
You may construct sequences that make use of this exit value:
1) If you specify a series of commands separated by “&&” tokens,
the next command is executed only if the previous command returns
an exit code of 0.
2) If you specify a series of commands separated by “||” tokens,
the next command is executed only if the previous command returns
a nonzero exit code.
UNIX Shells
*
- For example,
if the C compiler cc compiles a program without fatal errors,
it creates an executable program called “a.out” and returns an exit
code of 0;
otherwise, it returns a nonzero exit code.
$ cc myprog.c && ./a.out
- The following conditional sequence compiles a program
called “myprog.c” and displays an error message if the compilation
fails:
$ cc myprog.c || echo compilation failed.
UNIX Shells
*
• GROUPING COMMANDS
- Commands may be grouped by placing them between parentheses,
which causes them to be executed by a child shell(subshell).
- The group of commands shares the same standard input,
standard output, and standard error channels and may be redirected
and piped as if it were a simple command.
- Here are some examples:
$ date; ls; pwd > out.txt ---> execute a sequence.
Mon Feb 2 00:33:12 CST 1998 ---> output from date.
a.c b.c ---> output from ls.
$ cat out.txt ---> only pwd was redirected.
/home/glass
$ ( date; ls; pwd ) > out.txt ---> group and then redirect.
$ cat out.txt ---> all output was redirected.
Mon Feb 2 00:33:28 CST 1998
a.c b.c
/home/glass
$ _
UNIX Shells
*
• Background Processing
- If you follow a simple command, pipeline, sequence of pipelines,
or group of commands by the “&” metacharacter, a subshell is
created to execute the commands as a background process
- The background process runs concurrently with the parent shell and
does not take control of the keyboard.
Background processing is therefore very useful for performing several
tasks simultaneously, as long as the background tasks do not require
input from the keyboard.
UNIX Shells
*
• Background Processing
$ find . -name a.c -print --->search for “a.c”
./wild/a.c
./reverse/tmp/a.c
$ find . -name b.c -print & --->search in the background.
27174 --->process ID number.
$ date --->run “date” in the foreground.
./wild/b.c --->output from background “find”.
Mon Feb 2 18:10:42 CST 1998 --->output from date.
$ ./reverse/tmp/b.c --->more output from background “find”
--->came after we got the shell prompt,
---> but we don’t get another prompt.
- You may specify several background commands on a single line by separating
each command by an ampersand.
$ date & pwd & ---> create two background processes.
27310 ---> process ID of “date”.
27311 ---> process ID of “pwd”.
/home/glass ---> output from “pwd”.
Mon Feb 2 18:37:22 CST 1998 ---> output from “date”.
$ _
UNIX Shells
*
• REDIRECTIONAL BACKGROUND PROCESSES
- Redirecting Ouput
To prevent the output from a background process from arriving
at your terminal, redirect its output to a file.
In the following example, we redirect the standard output the find
command to a file called “find.txt”.
As the command was executing, we see it grow using the ls command.
UNIX Shells
*
• REDIRECTIONAL BACKGROUND PROCESSES
- Redirecting Ouput
$ find . -name a.c -print > find.txt &
27188 ---> process ID of “find”.
$ ls -l find.txt ---> look at “find.txt”.
-rw-r--r-- 1 glass 0 Feb 3 18:11 find.txt
$ ls -l find.txt ---> watch it grow.
-rw-r--r-- 1 glass 29 Feb 3 18:11 find.txt
$ cat find.txt ---> list “find.txt”.
./wild/a.c
./reverse/tmp/a.c
$ _
UNIX Shells
*
• REDIRECTIONAL BACKGROUND PROCESSES
- Another alternative is to mail the output of the background
process to yourself:
$ find . -name a.c -print | mail glass &
27193
$ cc program.c ---> do other useful work.
$ mail ---> read my mail.
Mail version SMI 4.0 Sat Oct 13 20:32:29 PDT 1990 Type ? For help.
>N 1 glass@utdallas.edu Mon Feb 3 18:12 10/346
&1
From: Graham Glass <glass@utdallas.edu>
To : glass@utdallas.edu
./wild/a.c ---> the output from “find”.
./reverse/tmp/a.c
& q
$ _
UNIX Shells
*
• Redirecting Input
- When a background process attempts to read from a terminal,
the terminal automatically sends it an error signal that causes it
to terminate.
- In the following example, we run the chsh utility in the background.
It immediately issues the “Login shell unchanged” message and
terminates, never bothering to wait for any input.
$ chsh & ---> run “chsh” in background.
27201
Changing NIS login shell for glass on csservr1.
Old shell : /bin/sh
New shell : Login shell unchanged. ---> didn’t wait for the input.
- If you run the mail utility in the background,
it issues the message “No message!?!”:
$ mail glass &
27202
No message !?! ---> didn’t wait for keyboard input.
UNIX Shells
*
• SHELL PROGRAMS: SCRIPTS
- Any series of shell commands may be stored inside a regular text file
for later execution.
A file that contains shell commands is called a script.
Before you can run a script, you must give it execute permission by
using the chmod utility.
to run it, you need only to type its name.
- Scripts are useful for storing commonly used sequences of commands,
and they range in complexity from simple one-liners to fully blown
programs.
- When a script is run, the system determines which shell the script
was written for and then executes the shell using the script as its
standard input.
UNIX Shells
*
• SHELL PROGRAMS: SCRIPTS
- The system decides which shell the script is written for by examining
the first line of the script.
- Here are the rules that it uses to make this decision:
1) If the first line of the script is just a pound sign(#),
then the script is interpreted by the shell from which you executed
this script as a command.
2) If the first line of the script is of the form #! path name,
then the executable program pathName is used to interpret the script.
3) If neither rule1 nor rule2 applies,
then the script is interpreted by a Bourne shell (sh).
Note: Bash on Linux, MacOS X is positioned as /bin/sh.
UNIX Shells
*
• SHELL PROGRAMS: SCRIPTS
- Here is an example that illustrates the construction and execution of
two scripts,
one for the Bash shell and the other for the Korn shell.
$ cat > script.csh ---> create the bash script.
#! /bin/bash
# This is a sample bash script.
echo -n the date today is # in bash, -n omits new line
date # output today’s date.
^D ---> end of input.
$ cat > script.ksh ---> create the Korn-shell script.
#!/bin/ksh
#This is a sample Korn shell script.
echo “the date today is \c” # in ksh, \c omits the new line
date # output today’s date.
^D ---> end of input
$ chmod +x script.csh script.ksh ---> make the scripts executable.
UNIX Shells
*
• SHELL PROGRAMS: SCRIPTS
$ ls -lF script.csh script.ksh ---> look at the attributes of the
---> scripts.
-rwxr-xr-x 1 glass 138 Feb 1 19:46 script.csh*
-rwxr-xr-x 1 glass 142 Feb 1 19:47 script.ksh*
$ ./script.csh ---> execute the C-shell script.
The date today is Sun Feb 1 19:50:00 CST 2004
$ ./script.ksh ---> execute the Korn-shell script.
The date today is Sun Feb 1 19:50:05 CST 2004
$ _
- The “.csh” and “.ksh” extensions of the scripts are used only for clarity;
scripts don’t even require an extension.
To append an indicator of the file type to a directory listing pass the -F option
- indicates a file
d indicates a directory.
UNIX Shells
*
SUBSHELLS
When you log into a UNIX system, you execute an initial login
shell.
This initial shell executes any simple commands that you enter.
- current(parent) shell creates a new(child) shell to perform
some tasks:
1) When a grouped command,
such as ( ls; pwd; date ), is executed, the parent shell creates
a child shell to execute the grouped commands.
If the command in not executed in the background,
the parent shell sleeps until the child shell terminates.
UNIX Shells
*
2) When a script is executed,
the parent shell creates a child shell to execute the commands
in the script.
If the script is not executed in the background,
the parent shell sleeps until the child shell terminates.
3) When a background job is executed,
The parent shell creates a child shell to execute the background
commands.
The parent shell continues to run concurrently with the child
shell.
UNIX Shells
*
- A child shell is called a subshell.
Just like any other UNIX process,
a subshell has its own current working directory;
thus cd commands executed in a subshell do not affect
the working directory of the parent shell:
$ pwd ---> display my login shell’s current directory.
/home/glass
$ ( cd /; pwd ) ---> the subshell moves and executes pwd.
/ ---> output comes from the subshell.
$ pwd ---> my login shell never moved.
/home/glass
$ -
UNIX Shells
*
- Every shell contains two data areas:
an environment space and a local-variable space.
A child shell inherits a copy of its parent’s environment space
and a clean local-variable space:
Parent shell
Child shell
Environment
Environment Copied from parent
Local
Local Clean, initialized
Example of Environment:
PATH=/usr/local/bin:
LOGNAME=admin
HZ=100
TERM=vt10
SHELL=/bin/csh
HZ is the frequency with which the system's timer hardware is programmed to interrupt the kernel.
UNIX Shells
*
• VARIABLES
- A shell supports two kinds of variables:
local and environment variables.
Both kinds of variables hold data in a string format.
the child shell gets a copy of its parent shell’s environment
variables, but not its local variables.
Environment variables are therefore used for transmitting
useful information between parent shells and their children.
UNIX Shells
*
• VARIABLES
- Here is a list of the predefined environment variables that are
common to all shells:
Name Meaning
$HOME the full pathname of your home directory
$PATH a list of directories to search for commands
$MAIL the full pathname of your mailbox
$USER your username
$SHELL the full pathname of your login shell
$TERM the type of your terminal
UNIX Shells
*
• VARIABLES
- The syntax for assigning variables differs between shells,
but the way that you access the variables is the same:
If you precede the name of a variable with a $,
this token sequence is replaced by the shell with the value of
the named variable.
To create a variable, simply assign it a value;
variable does not have to be declared.
- the syntax for assigning a variable in the Bourne, Bash and Korn
shells is as follows:
variableName=value ---> place no spaces around the value
or
variableName=“ value ” ---> here, spacing doesn’t matter.
UNIX Shells
*
• VARIABLES
- In the following example,
we display the values of some common shell environment
variables:
$ echo HOME = $HOME, PATH=$PATH ---> list two variables.
HOME =/home/glass, PATH=/bin:/usr/bin:/usr/sbin
$ echo MAIL = $MAIL
MAIL=/var/mail/glass
$ echo USER = $USER, SHELL = $SHELL, TERM=$TERM
USER = glass, SHELL = /bin/sh, TERM=vt100
$ _
UNIX Shells
*
• VARIABLES
- The next example illustrates the difference between local and
environment variables.
In the following, we assign values to two local variables and
then make one of them an environment variable by using the
Bourne shell export command.
Note that the value of the environment variable is copied
into the child shell, but the value of the local variable is not.
Finally, we press Control-D to terminate the child shell and
restart the parent shell, and then display the original
variables:
UNIX Shells
*
$ firstname=Graham ---> set a local variable.
$ lastname=Glass ---> set another local variable.
$ echo $firstname $lastname ---> display their values.
Graham Glass
$ export lastname ---> make “lastname” an
---> environment variable.
$ sh ---> start a child shell; the parent sleeps.
$ echo $firstname $lastname ---> display values again.
Glass
$ ^D ---> note that firstname was’t copied.
$ echo $firstname $lastname ---> they remain unchanged.
Graham Glass
$ _
UNIX Shells
*
- several common built-in variables that have special meanings:
Name Meaning
$$ The process ID of the shell.
$0 The name of the shell script( if applicable ).
$1..$9 $n refers to the nth command line argument
( if applicable ).
$* A list of all the command-line arguments.
UNIX Shells
*
- The first special variable is especially useful for creating
temporary filenames, and the rest are handy for accessing
command-line arguments in shell scripts.
- Here’s an example of a script that illustrates all of the common
special variables:
UNIX Shells
*
$ cat > script.sh ---> list the script.
echo the name of this script is $0
echo the first argument is $1
echo a list of all the arguments is $*
echo this script places the date into a temporary file
echo called $1.$$
date > $1.$$ # redirect the output of date.
ls $1.$$ # list the file.
rm $1.$$ # remove the file.
^D
$ chmod +x script.sh
$ ./script.sh paul ringo george john ---> execute the script.
the name of this script is script.sh
the first argument is paul
a list of all the arguments is paul ringo george john
this script places the date into a temporary file
called paul.24321
paul.24321
$ _
UNIX Shells
*
• QUOTING
- There are often times when you want to inhibit the shell’s
wildcard-replacement, variable-substitution, and/or command-
substitution mechanisms.
The shell’s quoting system allows you to do just that.
- Here’s the way that it works:
1) Single quotes(‘) inhibit wildcard replacement,
variable substitution, and command substitution.
2) Double quotes(“) inhibit wildcard replacement only.
3) When quotes are nested, it’s only the outer quotes that have
any effect.
UNIX Shells
*
• QUOTING
- The following example illustrates the difference between the
two different kinds of quotes:
$ echo 3 * 4 = 12 ---> remember, * is a wildcard.
3 a.c b b.c c.c 4 = 12
$ echo “3 * 4 = 12” ---> double quotes inhibit wildcards.
3 * 4 = 12
$ echo ‘3 * 4 = 12’ ---> single quotes inhibit wildcards.
3 * 4 = 12
UNIX Shells
*
- By using single quotes(apostrophes) around the text,
we inhibit all wildcarding and variable and command
substitutions:
$ name=Graham
$ echo ‘my name is $name - date is `date`’
my name is $name - date is ‘date’
$ _
- By using double quotes around the text, we inhibit wildcarding,
but allow variable and command substitutions:
$ echo “my name is $name - date is `date`”
my name is Graham - date is Mon Feb 2 23:14:56 CST 1998
$ -
UNIX Shells
*
• HERE DOCUMENTS
- Scripts that use “<<“ are sometimes called here documents.
- Here’s an example of a here document.
$ cat > here.sh ---> look at an example of a “here” document.
mail $1 << ENDOFTEXT
Dear $1,
Please see me regarding some exciting news!
- $USER
ENDOFTEXT
echo mail sent to $1
^D
$ chmod +x here.sh
$ ./here.sh jomer ---> send mail to jomer using the script.
mail sent to jomer
$ ./here.sh glass ---> send mail to glass using the script.
mail sent to glass
$1
UNIX Shells
*
Mail version SMI 4.0 Sat Oct 13 20:32:29 PDT 1990 Type ? for help.
>N 1 glass@utdallas.edu Mon Feb 2 13:34 12/384
&1 ---> read message #1.
From: Graham Glass <glass@utdallas.edu>
To: glass@utdallas.edu
Dear glass,
Please see me regarding some exciting news!
-glass
&q ---> quit out of mail.
$ _
UNIX Shells
*
• JOB CONTROL
- Convenient multitasking is one of UNIX’s best features,
so it’s important to be able to obtain a listing of your current
processes and to control their behavior.
1) ps, which generates a list of processes and their attributes,
including their names, process ID numbers,
controlling terminals and owner.
2) kill, which allows you to terminate a process based on its
ID number.
3) wait, which allows a shell to wait for one of its child processes
to terminate.
UNIX Shells
*
• Process Status: ps
- The ps utility allows you to monitor the status of processes and
works as follows:
Utility : ps -efl
ps generates a listing of process-status information.
By default, the output is limited to processes created by your
current shell.
The -e option instructs ps to include all running processes.
The -f option causes ps to generate a full listing.
The -l option generates a long listing.
UNIX Shells
*
- Here, we use of the sleep utility to delay a simple echo
statement and place the command in the background, then
we execute the ps utility to obtain a list of the shell’s
associated processes.
- Each “sh” process is a Bourne-shell process;
one of them is the login shell,
and the other one is the subshell created to execute
the command group.
$ ( sleep 10; echo done ) & ---> delayed echo in background.
27387 ---> the process ID number.
$ ps
PID TTY TIME CMD
27355 pts/3 0:00 -sh ---> the login shell.
27387 pts/3 0:00 -sh ---> the subshell.
27388 pts/3 0:00 sleep 10 ---> the sleep.
27389 pts/3 0:00 ps ---> the ps command itself!
$ done ---> the output from the background process.
UNIX Shells
*
- For the record, here’s a description of the sleep utility:
Utility : sleep seconds
The sleep utility sleeps for the specified number of seconds
and then terminates.
The meaning of the common column headings of ps output are
as follows:
Column Meaning
S the state of the process
UID the effective user ID of the process
PID the ID of the process
PPID the ID of the parent process
C the percentage of CPU time that the process used in
the last minute
UNIX Shells
*
Column Meaning
PRI the priority of the process
SZ the size of the process’ data and stack, in kilobytes
STIME the time the process was created, or the date,
if the process was created before the current day
TTY the controlling terminal
TIME the amount of CPU time used so far(MM:SS)
CMD the name of the command
ADDR Memory address of the process
WCHAN Memory address of the event the process is waiting for
NI Nice value
A negative nice value means higher priority, whereas a positive nice value
means lower priority. Zero in this field simply means priority will not be adjusted in determining a task's
UNIX Shells
*
• Process Status: ps
- The S field encodes the stat of the process as follows:
letter Meaning
O running on a processor
R runable
S sleeping
T suspended
Z zombie process
UNIX Shells
*
• Process Status: ps
- Here’s an example of some user-oriented output from ps:
$ ( sleep 10; echo done ) &
27462
$ ps -f ---> request user-oriented output.
UID PID PPID C STIME TTY TIME CMD
glass 731 728 0 21:48:46 pts/5 0:01 -ksh
glass 831 830 1 22:27:06 pts/5 0:00 sleep 10
glass 830 731 0 22:27:06 pts/5 0:00 -ksh
$ done ---> output from previous command
UNIX Shells
*
- The Bash, Bourne and Korn shells automatically terminate
background processes when you log out,
whereas the C shell allows them to continue.
- If you’re using a Bash, Bourne or Korn shell and you want to
make a background process immune to this effect,
use the nohup utility to protect it.
Utility : nohup command
The nohup utility execute command and makes it immune to
the hangup(HUP) and terminate(TERM) signals.
The standard output and error channels of command are
automatically redirected to a file called “nohup.out,” and
the process’ priority value is increased by 5,
thereby reducing its priority.
UNIX Shells
*
- This utility is ideal for ensuring that background processes
are not terminated when your login shell is exited.
If you execute a command using nohup, logout, and then log
back in again, you won’t see the command listed in the output
of a regular ps.
To include a list of all of the current processes without control
terminals in a ps output, use the -x option.
UNIX Shells
*
- Here’s an example of this effect:
$ nohup sleep 10000 & ---> nohup a background process.
27406
Sending output to ‘nohup.out’ ---> message from “nohup”.
$ ps ---> look at processes.
PID TT STAT TIME COMMAND
27399 p3 S 0:00 -sh(sh)
27406 p3 S N 0:00 sleep 10000
27407 p3 R 0:00 ps
$ ^D ---> logout.
UNIX Shells
*
Unix® System V Release 4.0
Login: glass ---> log back in.
Password: ---> secret.
$ ps ---> the background process is not
---> listed.
PID TT STAT TIME COMMAND
27409 p3 S 0:00 -sh(sh)
27411 p3 R 0:00 ps
$ ps -x ---> the background process is listed.
PID TT STAT TIME COMMAND
27406 ? IN 0:00 sleep 10000
27409 p3 S 0:00 -sh ( sh )
27412 p3 R 0:00 ps -x
$ _
UNIX Shells
*
• Signaling Processes:kill
- If you wish to terminate a process before it completes,
use the kill command.
The Bash, Korn and C shells contain a built-in command called
kill, whereas the Bourne shell invoke the standard utility instead.
Both versions of kill supports the following functionality:
Utility/Shell Command : kill [-signalId] {pid}+
kill -l
kill sends the signal with code signalId to the list of numbered
processes.
signalId may be the number or name of a signal.
UNIX Shells
*
- By default, kill sends a TERM signal ( number 15 ),
which causes the receiving processes to terminate.
$kill 23456 -- no signal sent (default is signal 15)
To obtain a list of the legal signal names, use the -l option.
To send a signal to a process,
you must either own it or be a super-user.
Processes may protect themselves from all signals except for
the KILL signal ( number 9 ).
Therefore, to ensure a kill, send signal number 9.
The kill utility ( as opposed to the shell built-in
commands ) allows you to specify 0 as the pid,
which causes all of the processes associated with the shell
to be terminated.
$kill 0
UNIX Shells
*
- In the following example,
we create a background process and then kill it.
To confirm the termination, we obtain a ps listing:
$ ( sleep 10; echo done ) & ---> create background process
27390 ---> process ID number.
$ kill 27390 ---> kill the process.
$ ps ---> it’s gone!
PID TT STAT TIME COMMAND
27355 p3 S 0:00 -sh(sh)
27394 p3 R 0:00 ps
$ _
UNIX Shells
*
- The use of the -l option and a named signal.
The signal names are listed in numerical order,
starting with signal #1.
$ kill -l ---> list the signal names.
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
9) KILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR1
17) SIGUSR2 18) SIGCHLD 19) SIGPWR 20) SIGVTALRM
21) SIGPROF 22) SIGIO 23) SIGWINCH 24) SIGSTOP
25) SIGTSTP 26) SIGCONT 27) SIGTTIN 28) SIGTTOU
29) SIGURG 30) SIGLOST 32) SIGDIL 33) SIGXCPU
34) SIGXFSZ 35) SIGCANCEL 37) SIGRTMIN 38) SIGRTMIN+1
39) SIGRTMIN+2 40) SIGRTMIN+3 41) SIGRTMAX-3 42) SIGRTMAX-2
43) SIGRTMAX-1 44) SIGRTMAX
$ (sleep 10; echo done) &
27490 ---> process ID number.
$ kill -KILL 27490 ---> kill the process with signal #9.
or
$ kill -9 27490
UNIX Shells
*
- Finally, here’s an example of the kill utility’s ability to kill all of
the processes associated with the current shell:
$ sleep 30 & sleep 30 & sleep 30 & ---> create three processes.
27429
27430
27431
$ kill 0 ---> kill them all.
27431 Terminated
27430 Terminated
27429 Terminated
$ _
UNIX Shells
*
- Waiting For Child Processes: wait
A shell may wait for one or more of its child processes
to terminate by executing the built-in wait command,
which works as follows:
Shell Command: wait[ pid ]
wait causes the shell to suspend until the child process with
the specified process ID number terminates.
If no arguments are supplied, the shell waits for all of its child
processes.
UNIX Shells
*
- In the following example, the shell waited until both background
child processes had terminated before continuing:
$ ( sleep 30; echo done 1 ) & ---> create a child process.
24193
$ ( sleep 30; echo done 2 ) & ---> create a child process.
24195
$ echo done 3; wait; echo done 4 ---> wait for children.
done 3
done 1 ---> output from first child.
done 2 ---> output from second child.
done 4
$ _
UNIX Shells
*
- FINDING A COMMAND: $PATH
When a shell processes a command,
it first checks to see whether it’s a built-in command;
if it is, the shell executes it directly.
echo is an example of a built-in shell command:
$ echo some commands are executed directly by the shell
some commands are executed directly by the shell
$ _
UNIX Shells
*
- If the file doesn’t exist or isn’t an executable, an errors occurs:
$ /bin/ls ---> full pathname of the ls utility.
script.csh script.ksh
$ /bin/nsx ---> a nonexistent filename.
/bin/nsx: not found
$ /etc/passwd ---> the name of the password file.
/etc/passwd: Permission denied ---> it’s not executable.
$ _
- OVERLOADING STANDARD UTILITIES
$ mkdir bin ---> make my own personal “bin” directory.
$ cd bin ---> move into the new directory.
$ cat > ls ---> create a script called “ls”.
echo my ls
^D ---> end of input.
$ chmod +x ls ---> make it executable.
UNIX Shells
*
- OVERLOADING STANDARD UTILITIES
$ echo $PATH ---> look at the current PATH setting.
/bin:/usr/bin:/usr/sbin
$ echo $HOME ---> get pathname of my home directory.
/home/glass
$ PATH=/home/glass/bin:$PATH ---> update.
$ ls ---> call “ls”.
my ls ---> my own version overrides “/bin/ls”.
$ _
Note that only this shell and its child shells would be affected
by the change to PATH; all other shells would be unaffected.
UNIX Shells
*
- TERMINATION AND EXIT CODES
Every UNIX process terminates with an exit value.
By convention, an exit value of 0 means that the process
completed successful, and a nonzero exit value indicates failure.
All built-in commands return an exit value of 1 if they fail.
In the Bash, Bourne and Korn shells, the special shell variable
$? always contains the value of the previous command’s
exit code.
In the C shell, the $status variable holds the exit code.
UNIX Shells
*
- In the following example, the date utility succeeded, whereas
the cc and awk utilities failed:
$ date ---> date succeeds.
Mon Feb 2 22:13:38 CST 1998
$ echo $? ---> display its exit value.
0 ---> indicates success.
$ cc prog.c ---> compile a nonexistent program.
cpp: Unable to open source file ‘prog.c’.
$ echo $? ---> display its exit value.
1 ---> indicates failure.
$ awk ---> use awk illegally.
awk: Usage: awk [-Fc] [-f source | ‘cmds’] [files]
$ echo $? ---> display its exit value.
1 ---> indicates failure.
$ _
UNIX Shells
*
- Any script that you write should always explicitly return an exit
code.
To terminate a script, use the built-in exit command,
which works as follows:
Shell Command: exit number
exit terminates the shell and returns the exit value number
to its parent process.
If number is omitted, the exit value of the previous command
is used.
UNIX Shells
*
- If a shell doesn’t include an explicit exit statement,
the exit value of the last command is returned by default.
The script in the following example returned an exit value of 3:
$ cat script.sh ---> look at the script.
echo this script returns an exit code of 3
exit 3
$ script.sh ---> execute the script.
this script returns an exit code of 3
$ echo $? ---> look at the exit value.
3
$ _
UNIX Shells
*
- COMMON CORE BUILT-IN COMMANDS
There are a large number of built-in commands that are
supported by the four shells, of which only a few commands
are common to all three shells.
This section describes the most useful common core built-in
commands.
eval
Shell Command: eval command
The eval shell command executes the output of a command as
a regular shell command.
It is useful for processing the output of utilities that generate
shell commands.
UNIX Shells
*
- In the following example, we execute the result of the echo
command, which causes the variable x to be set:
$ echo x=5 ---> first execute an echo directly.
x=5
$ eval ‘echo x=5` ---> execute the result of the echo.
$ echo $x
5
$ _
UNIX Shells
*
- Exec
Shell command: exec command
The exec shell command causes the shell’s image to be replaced
with command in the process’ memory space.
If command is successfully executed, the shell that performed
the exec ceases to exist.
If this shell was a login shell, then the login session is
terminated when command terminates.
UNIX Shells
*
- In the following example, we exec the date command from
the login shell, which causes the date utility to run and then the
login process to terminate:
$ exec date ---> replace shell process by date process.
Sun Feb 1 18:55:01 CDT 1998 ---> output from date.
login: _ ---> login shell is terminated
---> and starts a new for the next user.
UNIX Shells
*
- Shift
Shell Command: shift
The shift shell command causes all of the positional parameters
$2…$n to be renamed $1..$(n-1), and $1 to be lost.
It’s particularly handy in shell scripts when cycling through
a series of command-line parameters.
If there are no positional arguments left to shift,
an error message is displayed.
UNIX Shells
*
- In the following example,
a shell script displays its arguments before and after a shift.
$ cat shift.sh ---> list the script.
#!/bin/sh
echo first argument is $1, all args are $*
shift
echo first argument is $1, all args are $*
$ ./shift.sh a b c d ---> run with four arguments.
first argument is a, all args are a b c d
first argument is b, all args are b c d
$./ shift.sh a
first argument is a, all args are a
first argument is , all args are
$./ shift.sh
first argument is , all args are
shift: No more words ---> error message.
$ _