cpelecii prelim lecture 2 part 2 - special built-in shell commands
TRANSCRIPT
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
1/23
Objectives
We shall learn in this unit about some special built-inShell commands:
The Bash Shell echo command
Special printing characters
The read command
Option and argument processing with getopts
Command line re-evaluation with eval
History manipulations with fc
The set command
Shell options with set
Shell invocation
Built-in commands
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
2/23
The echo command is the Bash Shell output mechanism:
echo argument ... prints arguments to standard outputseparated by spaces
echo-n argument ... no trailing newline after output
echo-e argument ... interpret \char escapes
echo-E argument ... do not interpret \char escapes
The echo Command
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
3/23
\a Alarm - ring the terminal bell
\b Backspace\c Print without trailing newline (same as print -n)
\f Form feed
\n Newline
\r Return
\t Tab
\v Vertical tab
\\ Backslash\xxx Character with octal code xxx (up to three octal digits)
Special echo Characters
Backslash character sequences have special meaning
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
4/23
echo Examples
When you use the echo command, here's what you get$ echo -e "Line 1\n\tLine2"
Line1
$ echo -e 'One quarter = \274'
One quarter =
$ echo -e 'Backslash = \134'
Backslash = \$ echo 'hi\\\\there 1'
hi\\\\there 1
$ echo hi\\\\there 2
hi\\there 2$ echo -e 'hi\\\\there 3'
hi\\there 3
$ echo -e hi\\\\there 4
hi\there 4$ _
Line2
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
5/23
To get input while a Shell Script is running, use read:
readvariable ...
The read command reads a line from its standard input
Assigns input words to the variables
Set remaining variables to null if too few words
Set last variable to the remainder of the words if too few variables
For the Bash Shell, if no variables are specified, the REPLY variable is setto the whole input line
There is also one option, -r
The read Command
read -r variable ... raw mode \ is not taken as a linecontinuation character
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
6/23
You can use read from the Shell prompt as well
$ readvar1 var2
123 456 789
$ echo -e "var1 = $var1 \tvar2 = $var2"var1 = 123 var2 = 456 789
$ readvar1 var2
abc
$ echo -e "var1 = $var1 \tvar2 = $var2"var1 = abc var2 =
$ read
hi there
$ echo $REPLY
hi there
$ _
read Examples
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
7/23
#!/bin/bash
# Usage: readraw
# Read & print text_file in raw mode until EOF.
while read -r line
doprint -R "$line"
done < text_file
$ readraw
The first line of \ttext_file
-now the second
The last line of \ttext_file\t-\tend of file!\a
$ _
read Options Example
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
8/23
Processing Options
Parameters on a script command line are of two types
arguments used in script
options used to tell the script things
General parameter/argument processing is difficult
Consider$ myscript -a -f optionfile argfile$ myscript -foptionfile -va argfile
Shell provides getopts as a solution
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
9/23
The getopts command processes options and associatedarguments from a parameter list
getopts optionstring variableparameter...
Each invocation ofgetopts processes the next option in theparameterlist
usually called within a loop
The optionstring lists expected option identifiers
if an option identifier requires an associated argument, add acolon (:)
a leading colon in the list suppresses "invalid option"messages by getopts
The getopts Command
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
10/23
How are options processed when passed to a script?
Assume
The possible options are a, b and c
Option b is to have an associated argumentSuppress normal Linux/system error messages
Inside the script getopts will be used early on:
A correct command line to the script might be
$ prog.sh +c -ab barg -- arg1 arg2
What about?$ prog.sh -c -b -a -- arg1 arg2
while getopts ':ab:c' flag arguments
do
identify the values set by getopts
done
getopts Syntax Example
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
11/23
getopts Example
#!/bin/bash
# Example of getopts
USAGE="usage: example.getopts.sh [+-c] [+-v] [-a argument]"
while getopts :a:cv arguments
do
case $arguments in
a) argument=$OPTARG ;;
c) compile=on ;;
+c) compile=off ;;
v) verbose=on ;;
+v) verbose=off ;;
: ) echo "You forgot an argument for the switch called a."; exit ;;
\?) echo "$OPTARG is not a valid switch" ; print "$USAGE" ; exit ;;
esac
done
echo "compile is $compile; verbose is $verbose; argument is $argument "
#END
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
12/23
The Shell processes each command line read before invoking therelevant command(s).
If you want to re-read and process a command line, use eval:
Eval processes its arguments as normal
The arguments are formed into a space separated string
The Shell then executes that string as a command line
The return value is that of the executed command line
The eval Command
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
13/23
$ eval echo '*sh'
getopts.example.ksh eval.ksh try.sh
$ message1=Goodbye print the message
$ message10=Hello named by $variable$ variable=message10
$ eval echo '$'$variable
Hello
$ echo "ls | sort -r" > cmd_file
$ read -r line < cmd_file read a cmd_file line
$ eval "$line" - run as a commandzfile
afile
$ cmd='ps -ef | grep tommy' run a string command
$ eval $cmd to list tommy's processes
...$ _
Here are some eval command lines...
eval Examples
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
14/23
The Bash Shell fc command interactively edits and thenre-executes portions of your command history file:
To re-execute a single command with automatic editing:
fc start end edits and executes a command range
startdefaults to the last commandenddefaults to the value ofstart
-e editor to specify an editor other than
$FCEDIT - Shell default is /bin/vi
fc -e - old=new
command
old=newto replace string oldwith string newcommandto specify a command default last
The fc Command
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
15/23
$ fc pwd cc
edit the last command with the$FCEDIT editor, and then re-execute
$ fc
edit with $FCEDIT from the most
recent command starting with pwd, toone beginning with cc
$ fc 10 20 use vi to edit history lines 10 to 20
$ fc -e ex -3 -1 edit the last three commands with ex
Ranges may be strings, absolute or relative numbers
re-run most recent cc command
swap 3 for 2 in command 10
$ fc -e- re-execute last command as was
$ fc -e - cc
$ fc -e - 2=3 10
$ fc -e - s=\? -2 change "s" into "?" in the commandbefore last
Automatic editing can specify a command in a similar way
fc Examples - Edit and Execute
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
16/23
The Bash Shell fc command lists portions of your command history file:
For example
fc -l start end list the specified command range the default is the last 16 commands
-n suppress command numbers in list
-r reverses the order of commands
$ fc -l pg grep lists commands from the last pg to a grep
$ fc -l 15 20 lists commands 15 to 20
$ fc -l -5 -1 lists the last five commands
fc Examples Lists
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
17/23
We have seen three functions performed by the set command:
This last form sets a Bash Shell option. There are several more optionsto set:
Bash Shell options and settings are listed by set -o
Turn option on with set -ooption orset -L (where L is anoption identifier)
Turn option off using set +o option orset +L
The set Command
set lists set variables with their values
set value ... re-sets the positional parameters
set -o vi enables Korn Shell line recall and editingusing vi
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
18/23
Option: L Description:
allexport a automatically export each variable set
ignoreeof stops an interactive Shell exiting on Ctrl-d you must use the exit command
noclobber C stops the Shell overwriting existing files with
> re-direction ( >| works instead)
noexec n for a non-interactive Shell to check syntax withoutexecuting commands
noglob f disables metacharacter pathname expansion
monitor m enable job control (on by default for interactive shells)
Shell Options with Set
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
19/23
Shell Options with set (Cont.)
Option L Description
notify b to notify asynchronously of background job completions
verbose v to display input on standard error as it is read
vi turns on history line recall and vi editing
xtrace x the debug option the Shell displays PS4 with eachprocessed command line
errexit e exits if any command returns a non-zero return code
nounset u displays an error message when an unset variable isused
posix change behavior of bash to match POSIX 1003.2
S Q
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
20/23
1. What command would you use to re-set the positionalparameters to "one" "two" "three"?
2. What lists the Shell options with settings?
3. Which setoption ensures that each variable assignmentwill be inherited by a sub-Shell?
4. What would stop from logging you out?
5. How can you use setto protect your files from beingoverwritten by output re-direction?
Set Quiz
Sh ll b ilti C d
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
21/23
We have seen the following builtin Shell commands:
You will see those below in later units
All builtin commands run in current environment
Shell builtin Commands
. : bg break
cd continue declare echo
eval exec exit export
fc fg getopts hash
history jobs kill local
logout pwd read readonly
return set shift source
test trap typeset umask
unset wait [ ]
alias builtin command enable
help let times type
unlimit unalias
Ch k i t
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
22/23
Checkpoint
1. What is wrong with the following command?echo "Please supply a number:\c" num
2. What getopts statement would allow you to process options
p, and a, with option t expecting an associated value?3. What command would print out the first and last positional
parameters?
4. Which set option disables metacharacter pathnameexpansion?
5. Which set options would be most useful in helping to debug a
shell script?
S
-
7/29/2019 CpELECII Prelim Lecture 2 Part 2 - Special Built-In Shell Commands
23/23
Summary
The Bash Shell echo command
Special printing characters
The readcommandOption and argument processing with getopts
Command line re-evaluation with eval
History manipulations with fc
The setcommand
Shell options with set
Shell invocation
Builtin commands