Shells
Shells
Slides partially adapted from Kumoh National University of Technology (Korea) and NYU
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).
In the next segment, we will study Bash (Bourne Again Shell) that is
compatible with Bourne shell and borrows best features from the
other shells. Bash is becoming the most popular shell - it is the
default shell in Linux.
• Utilities
chsh kill ps
echo nohup sleep
• Shell Commands
Shell functions
Built-in Scripts Variables Redirection Wildcards Pipes Sequence Subshells Background Command
Commands Processing subsitution
o find out which shell was chosen for you, look at your prom
nother way to find out the full pathname of your login shell
to type the following:
ls
ls
METACHARACTERS
mbol Meaning
ymbol Meaning
Here’s an example:
Output redirection
The sequence
ithout parameters, cat simply copies its standard input --- which,
in this case, is from the keyboard --- to its standard output.
Bash:
et -o noclobber
cho text > test
cho text > test
h: test: cannot overwrite existing file
ho text >| test
e sequence
t << eof
e1
e2
e3
f
1
2
3
Wildcard Meaning
:
e.e
:
g.c
*.c ---> list any text ending in “.c”.
b.c cc.c
?.c ---> list text for which one character is followed by “.c”
b.c
Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 27
UNIX Shells
/?.? ?.? ---> list all files with extensions in “2*” directories
and current directory.
b.c dir2/f.d dir2/g.c
The sequence
$ command1 | command2
n the next example, we pipe the output of the ls utility to the inp
of the wc utility in order to count the number of files in the curr
directory.
e tee utility copies its standard input to the specified files and t
standard output.
e -a option causes the input to be appended to the files rather
than overwriting them.
e -i option causes interrupts to be ignored.
or example:
TE: “grep -l” prints only the name of each input file from which output would
mally have been printed.
re’s an example:
The background process runs concurrently with the parent shell and
does not take control of the keyboard.
- Redirecting Ouput
recting Ouput
Any series of shell commands may be stored inside a regular text fil
for later execution.
Before you can run a script, you must give it execute permission by
using the chmod utility.
When a script is run, the system determines which shell the script
was written for and then executes the shell using the script as it
standard input.
one for the Bash shell and the other for the Korn shell.
“.sh” and “.ksh” extensions of the scripts are used only for clarity
ripts don’t even require an extension.
rent shell
Child shell
ironment
Environment Copied from pa
Local
Local Clean, in
Name Meaning
Name Meaning
There are often times when you want to inhibit the shell’s
wildcard-replacement, variable-substitution, and/or command-
substitution mechanisms.
3) When quotes are nested, it’s only the outer quotes that ha
any effect.
r glass,
ase see me regarding some exciting news!
ass
---> quit out of mail.
here are two utilities and one built-in command that allow
ou to do so:
wait, which allows a shell to wait for one of its child proc
to terminate.
Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954 70
UNIX Shells
rocess Status: ps
tility : ps -efl
olumn Meaning
the state of the process
D the effective user ID of the process
D the ID of the process
ID the ID of the parent process
the percentage of CPU time that the process used
the last minute
umn Meaning
letter Meaning
O running on a processor
R runable
S sleeping
T suspended
Z zombie process
te that only this shell and its child shells would be affect
the change to PATH; all other shells would be unaffected.
n the Bash, Bourne and Korn shells, the special shell variabl
? always contains the value of the previous command’s
xit code.
xit terminates the shell and returns the exit value number
to its parent process.
you try creating a file using the “>” character, you’ll prob
d up with a file that has a permission setting of 644 octal:
r w x r w x r
nal 1 1 0 1 1 0 1 1
0 0 0 0 1 0 0 1 0
1 1 0 1 0 0 1 0
Checklist
- the common functionality of the three major shells
- the common shell metacharacters
- output and input redirection
- filename substitution
- pipes
- command substitution
- command sequences
- grouped commands
- the construction of scripts
- the difference between local and environment variables
- the two different kinds of quotes
- basic job control
- the mechanism that the shell uses to find commands
- several core built-in commands