The - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

The

Description:

Course: ITSK 1601 Introduction to UNIX. Chapter 10: The Bourne Again Shell ... Thirdly, use mixed case or lowercase letters for all other variables. 26 ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 45
Provided by: ccs47
Category:
Tags: thirdly

less

Transcript and Presenter's Notes

Title: The


1

Chapter 10 The Bourne Again Shell
2
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
CONTENTS
  • BACKGROUND
  • CREATING A SIMPLE SCRIPT
  • COMMAND SEPARATION AND GROUPING
  • REDIRECTING STANDARD ERROR
  • JOB CONTROL
  • DIRECTORY STACK MANIPULATION
  • PROCESSES
  • PARAMETERS AND VARIABLES
  • HISTORY
  • ALIAS
  • COMMAND-LINE EXPANSION
  • COMMAND-LINE EDITING

3
Background
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
The Bourne Again Shell (bash) is based on the
Bourne Shell of earlier UNIX shell and is still
compatible with it. Bash is both a command
interpreter and a high-level programming
language. When you use bash as a programming
language, it processes groups of commands stored
in files called shell scripts.
More complex shell scripts are themselves
programs that do not just run other
programs. Besides user-created variables, the
shell maintains several keyword variables that
control important characteristics of the shell.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
4
Create a Simple Shell Script
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
A shell script is a file that contains commands
that can be executed by the shell. The commands
in a shell script can be any command you enter in
response to a shell prompt. You can use pipes,
redirection, grouping commands, and command
separation within the script.
There are a group of commands that control
structure in shell scripts. They are also called
control flow commands. Control-flow commands
enable you to alter the order of the execution of
commands in a script.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
5
Create a Simple Shell Script
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Create the Script Create a file called whodonit
using the joe editor gtjoe
whodonit Insert the following in the file.
Replace ? with your name. echo Program Started
echo Script Written by ? date echo Users
Currently Logged In who echo Program Ended
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
6
Create a Simple Shell Script
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Making a File Executable Type the list command to
view the permissions
ls al The output from the ls al may look like
this - r w - r -
- r - - 1 thelma users 355 Nov 4 1352
whodonit Change the permissions on whodonit to
allow owners, groups, and all others to read it,
edit it, and execute it chmod 777 whodonit

Type the list command to view the
permissions ls
al The output from the ls al may look like
this - r w x r
w x r w x 1 thelma users 355 Nov 4
1352 whodonit
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
7
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Create a Simple Shell Script
  • Executing the Script
  • Execute the script called whodonit by typing the
    following
    ./whodonit
  • Note
  • The period indicates the working directory.
  • The group and public access privileges were
    changed.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
8
Command Separation
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • You must separate commands from one another.
  • Also, if your command line is long you can use a
    backslash to continue to the next line.
  • You may use a semicolon to separate several
    commands who date ls al
  • You may use a pipe to connect standard output of
    one command directly to standard input of another
    command who sort
  • You may use the task symbol to run a job(s) in
    the background

    who date ls al

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
9
Command Grouping
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
() Parentheses can be used to group commands. It
does this by
  • First, for each group, shell creates a copy of
    itself called a subshell with its own set of
    variables.
  • Second, it treats each group of commands as a
    job.
  • Third, it creates a new process for each command
    executed.

Note that processes are not started when running
built-in shell commands (see page 444) such as cd
and echo. Example (who date ) ls -al
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
10
Redirecting Standard Error
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
You have learned that standard input is data
taken from the screen and standard output is data
displayed on the screen. Now, we will introduce
standard error where error messages are displayed
on the screen.
Unless you direct one or the other, you may not
know the difference between the output that a
command sends to standard output and the output
it sends to standard error. The program itself
does not know where the input comes from or where
the output goes to because the shell takes care
of that.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
11
Redirecting Standard Error
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • When you execute a program, the process running
    the program opens three file descriptors
  • STANDARD INPUT is identified by zero (0) and its
    redirect input symbol is lt or lt0
  • STANDARD OUTPUT is identified by the number 1
    and its redirect output symbol is gt or
    1gt
  • STANDARD ERROR is identified by the number 2 and
    its redirect error symbol is 2gt
  • cat x whodonit
    When you run cat
    with the name of a file that does not exist and
    the name of a file that does exist, it sends an
    error message to standard error and copies the
    file that does exist to standard output.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
12
Redirecting Standard Error
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Demonstrations cat x whodonit gt hold

When you redirect only the standard output, the
error output is still displayed on screen. cat
x whodonit 1gt hold1 2gt hold2
Redirect standard output and standard
error to different files. cat hold1

cat hold2

You can display the contents of each file. cat
x whodonit 1gt hold 2gt1
Redirect standard output and standard
error to the same file descriptor 1. Please
Note 1 means file descriptor 1 or redirected
standard output. 2 means file descriptor 2 or
redirected standard error messages.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
13
Job Control
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
You run a simple job whenever you give Linux a
command. You can create several jobs with
multiple commands on a single command line

find . print sort lpr grep 1
alex /tmp/k gt alexfiles
The portion up to the first is one job that
includes three processes. The second job is a
single process, running grep.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
14
Job Control
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Use the command jobs to list jobs
jobs You can also
move a background job into the foreground by
using the builtin command called fg with a
percent sign followed by the job number
fg
2 You can also move a foreground job into the
background by using the builtin command called
bg. But you must first press either CONTROL-Z
(stops the job immediately) or CONTROL-Y (causes
the job to continue to run until it tries to read
input from the terminal at which point is
suspended)
bg
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
15
DIRECTORY STACK MANIPULATION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
With bash you can store a list of directories you
are working with. The list is called a stack.
Use the dirs to display the contents of the
stack dirs

Initially dirs displays the name of the working
directory. Dirs builtin uses a tilde () to
represent the name of the users home directory.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
16
DIRECTORY STACK MANIPULATION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
The pushd (push directory) builtin is used to
change directories and at the same time add a new
directory to the top of the stack
pushd
When you use pushd without an argument, it swaps
the top two directories on the stack and makes
the new top directory the new working directory.
The popd (pop directory) builtin is used to
remove directories from the stack
popd
When you use popd without an argument, it removes
the top directory from the stack and changes the
working directory to the new top directory.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
17
PROCESSES
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • A process is the execution of a command by Linux.
    Processes are created when you
  • Log in (a shell script starts up)
  • Execute a Linux utility on the command line
  • Run a script
  • Execute a Linux utility in a script
  • Remember A process is not started for builtin
    commands.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
18
Process Structure
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
The process structure is hierarchical like the
filesystem with parents, children, and even a
root. One of the first things Linux does to begin
execution when a machine is started up is to
start a single process called init.
This process holds the same position in the
process structure as the root directory in the
filesystem structure.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
19
Process Structure
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
A hierarchical data structure frequently takes
the shape of a pyramid
If terminals are attached, the init process will
fork (the name of an operating system routine or
system call) a getty process for each terminal
that waits for a user to log in. The action of
logging in transforms the getty process into a
login process, and then finally into the users
shell process.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
20
Process Identification
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • When you run a process, two numbers are assigned
  • Shell assigns a number to the job called a
    job-number, displayed in brackets.
  • Operating system assigns a process-identification
    -number (PID), displayed after the job-number.
  • When the job finish execution, shell displays a
    message giving both the job-number and the
    command line used to start the command.
  • EXAMPLE
  • ls al gt pgm01

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
21
Process Identification
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
To list processes running use the ps utility
(page 826) ps ef Remember You can
not abort a job (process, command, utility,
program) running in the background. You must use
the kill utility (page 764)
Kill (pid) Use the ps utility with the l
option that displays a long listing of
information about each process
ps -l F S UID
PID PPID C PRI NI ADDR SZ WCHAN TTY
TIME CMD 100 S 1608 16919 16917 0 70 0
- 580 wait4 pts/0 000000
bash 000 R 1608 17173 16919 0 73 0 -
631 - pts/0 000000 ps
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
22
Executing a Command
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • When you give the shell a command, it forks
    (spawns, gives birth, begot, etc) a child process
    to execute the command.
  • While the child process is executing the command,
    the parent process sleeps.
  • When the child process finishes executing, it
    dies.
  • The parent process wakes up and prompts you for
    another command.
  • When you request a job to execute in the
    background, the shell forks a child process
    without going to sleep and without waiting for
    the child process to run.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
23
Invoking a Shell Script
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • When you give the shell a command, it forks
    (spawns, gives birth, begot, etc) a child
    process, called a subshell, to execute the
    command.
  • The subshell attempts to exec, or execute the
    command.
  • Like fork, exec is a routine executed by the
    operating system (a system call).
  • If the command is an executable program, (such as
    a compiled C program), exec succeeds and the
    system overlays the newly created subshell with
    the executable program.
  • If the command is a shell script, exec fails.
  • When exec fails, the command is assumed to be a
    shell script and the subshell runs the commands
    in the script.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
24
Startup Files
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
When a new shell is started, certain files with
commands in them may be used to initialize the
shell. If bash is started as a login shell or if
you use the login option when starting a bash
shell, bash first reads /etc/profile for
commands. After that, bash login shell looks in
your home directory, in the following order
  • If bash finds .bash_profile, it is executed.
  • If bash finds .bash_login exists, it is executed.
  • If bash finds .profile, it is executed.

When logging out, this same shell reads and
executes commands from the bash_logout file in
your home directory, if it exists.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
25
PARAMETERS
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
A shell parameter is associated with a value that
is accessible to the user. There are several
kinds of shell parameters. Parameters whose
names consist of letters, digits, and underscores
are often referred to as variables. One naming
convention is that parameters cannot start with a
number. Secondly,use only uppercase letters for
names of variables that are exported (environment
variables). Thirdly, use mixed case or lowercase
letters for all other variables.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
26
User-Created Variables
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
User-created variables are variables that you can
name and assign values
personalex Because of the leading ,
the shell recognizes that person is the name of
a variable, substitutes the value of the
variable, and passes that value to the echo
utility. You can prevent the shell from
substituting the value of a variable by Putting
quotes around the leading as in echo person
. Using single quotation marks as in echo
person Using a backslash as in echo \person
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
27
User-Created Variables
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
A variable exist as long as the shell in which it
was created exists. To remove the value of a
variable but not the variable itself, you must
set it to null
person To remove the variable, you must use the
unset builtin command

unset person To make the variable not changeable
or read only, use the readonly builtin command
readonly
person
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
28

Keyword Variables
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • Keyword variables are initialized by shell
  • HOME by default is your home directory and is
    established when your account was created.
  • PATH is assigned by /etc/profile when you
    log in and tells the system what directory to
    search for your commands, normally directories
    /bin and /usr/bin.
  • MAIL contains the name of the file that your
    mail is stored in (mailbox). Usually the
    directories are /var/mail or /usr/mail.
    (MAILPATH, MAILCHECK).
  • PS1 is the primary shell prompt (prompt string
    1).
  • PS2 is the secondary shell prompt (prompt
    string 2) that indicates shell is waiting for the
    user to continue the command-line.
  • CDPATH is usually set in .bash_profile, takes on
    the value of a colon-separated list of directory
    pathnames.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
29
The Dot Builtin Command
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • You can put the changes you make to the
    .bash_profile in one of three ways
  • You can wait until the next time you log in for
    the changes to take effect.
  • You can use the . builtin command to immediately
    run the .bash_profile.
    .
    .bash_profile
  • You can run the .bash_profile as a regular shell
    script. This will cause the new variables to be
    in effect only in the subshell running the
    script.
    .bash_profile
  • Note you can use . builtin to run any shell
    script, but there may be undesirable side effects.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
30
Positional Parameters
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
When you execute a utility with options, the
command-line will usually include the command
name and arguments. When you call a shell
script, the command-line may include the command
name and arguments. But the arguments in this
case they may be positional parameters. They are
called positional parameters because you refer to
them by their position on the command-line. Althou
gh you can reference them, you cannot
assign values to positional parameters.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
31
Positional Parameters
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Shell stores the name of the command you used to
call a program in parameter 0.

cat abc

echo The name of the
command used
echo to
execute this shell script was 0

abc

The name of the
command used

to execute this shell script was abc The
first argument on the command-line is represented
by the parameter 1, the second argument by the
parameter 2, and so on.

cat display_5args

echo The first five command line

echo arguments are 1 2
3 4 5

display_5args jenny alex helen
The
first five command line

arguments are jenny alex helen
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
32
Positional Parameters
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
The shift builtin promotes each of the
command-line arguments. The second argument
becomes the first, the third argument becomes the
second, the fourth becomes the third, and so
on. When you call the set builtin with one or
more arguments, it uses the arguments as values
for positional parameters, starting with 1. The
following script uses set to assign values to the
positional parameters 1, 2, and 3.
cat set_it

set this is it

echo 3 2 1

set_it

it is this
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
33
Special Parameters
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
  • Special parameter in shell are referenced by
    following the character with a special
    character
  • and _at_ represents all the command-line
    arguments.
  • represents the number of command-line
    arguments.
  • represents the process identification (PID)
    number of the process that is executing it.
  • ? Represents the exit status of the last
    command.
  • When a process stops executing for any reason, it
    returns an exit status (also called a condition
    code or return code) to its parent process.

Thelma Jones
Technology is a way of life. You can't do it by
yourself."
34
HISTORY
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
A feature adapted from the C shell is the history
mechanism that maintains a list of recently
issued command lines called events.
It can be helpful when you have made a mistake,
not sure what you did, or if you just want to
keep a record of a executed commands
- HISTSIZE

-
HISTFILE

- HISTFILESIZE
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
35

HISTORY
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
A sequential event number is assigned to each
command-line. You can display this event number
as part of the prompt. The command history will
display the events in the history list which is
ordered with the oldest events at the top of the
list and the most recent at the bottom

history Give the following command to establish
a history list of the 100 most recent events

HISTSIZE100 Give the following
command to establish a history list of the 100
most recent events across login sessions
HISTFILESIZE100
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
36
ALIAS
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Bash alias mechanism allows you to define new
commands by letting you substitute a string of
your choice in place of a command. The syntax of
the alias builtin is
alias
namevalue Double quotes and single quotes
mean different things when used with the alias
builtin. If you enclose value within double
quotes, then any shell variables ARE expanded
when the alias is created. If you enclose value
within single quotes, then any shell variables
are NOT expanded until the alias is used.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
37
COMMAND-LINE EXPANSION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Command-line expansion is a transformation the
shell makes before passing on to the program
being called. One of the first things that it
does, before executing the command, is to parse
(isolate strings of characters in) the
command-line into tokens or words. Another thing
it does, is to scan each token for the appearance
of special characters and patterns that instruct
the shell to take certain actions
cp /letter
. These actions often involve substituting one
word or words for another. The copy statement
above is broken down into three tokens cp,
/letter , and .
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
38
COMMAND-LINE EXPANSION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Brace Expansion is a convenient way to specify
filenames when pathname expansion does not apply
echo chap_one, two,
three.txt chap_one.txt chap_two.txt
chap_three.txt Tilde Expansion is a shorthand
notation to specify your home directory or the
home directory of another user
cp
/letter . Parameter Expansion is when a single
digit, or an integer enclosed in braces follows a
dollar sign ().
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
39
COMMAND-LINE EXPANSION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Variable Expansion results when the shell
processes a token consisting of a dollar sign ()
followed by a variable name. Shell replaces the
token with the value of the variable. Command
substitution makes it possible to use standard
output of a command in a shell script. It occurs
after the tokens on the command line have been
identified. You can use or single quotes
(bourne shell) (pwd) pwd
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
40
COMMAND-LINE EXPANSION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Arithmetic Expansion occurs when shell evaluates
an arithmetic expression and then replaces it
with the result
expression Create a file named
letter.txt with the following info
1a This is a
letter created for the arithmetic expansion
exercise. 2b It will show how to
use arithmetic expressions and logic.
3c You can use use the Bourne Shell
syntax with many bash expressions. 4d It
is common to assign the result of arithmetic
expansion to a variable. wc 1 letter.txt

wc l lt letter.txt (does
not display filename)
num-pages wc l lt letter.txt)66 1
Is this C programming?...
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
41
COMMAND-LINE EXPANSION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Let Builtin allows you to evaluate arithmetic
expressions without using arithmetic expansions
let
num-pages wc l lt letter.txt)66 1
let a53 b72

echo a b

8 9 Word Splitting. Use with caution.
It is done with the Internal Field Separator
(IFS) to separate arguments on a command line

awxyz

cat a
No
such file or directory

IFS

cat a
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
42
COMMAND-LINE EXPANSION
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
Pathname Expansion is the process of interpreting
ambiguous file references and substituting the
appropriate list of filenames is called pathname
expansion (page 106). Shell does this function
when it encounters an ambiguous file reference as
in a token containing any of the characters , ?
, , or . ls al echo extest rm
extest echo extest
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
43
COMMAND-LINE EDITING
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
The Bourne Again Shell provides an interactive
command-line editing feature that makes it easy
to correct mistakes you have typed on a command
line prior to its execution. It is implemented
through a package developed by the Free Software
Foundation called the Readline Library. This
library is available to application writers using
the C programming language for use in their
applications. Any application that makes use of
the Readline Library would support line editing
consistent with that provided in bash.
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
44
Course ITSK 1601 Introduction to
UNIXChapter 10 The Bourne Again Shell
The End Instructor Thelma Jones Room
TEC-105 Phone (770) 961-3636 Email
thelmajones_at_mail.clayton.edu Website
http//newcollege.clayton.edu/tjones/
Thelma Jones
Technology is a way of life. You can't do it by
yourself."
Write a Comment
User Comments (0)
About PowerShow.com