10. Command Line Components
● A Command (must present):
What to do?
● Options (zero or more):
How to do?
●Arguments (zero or more):
Which to do with?
11. Internal Field Separator (IFS)
● Function:
To separate command line components.
● Speak in general:
To cut a command line into words(fields).
● Generated by:
● <Space>
● <Tab>
● <Enter> (*note: CR also)
12. A Command Line Format
Command<IFS>[Options...]<IFS>[Arguments...]
13. Option Format
● Preceding Character:
+
●Full Format:
Starting with
● Short Format:
Starting with
Combinable
14. Option Example
● Find the difference:
ls a l l
ls al l
ls all
ls all
ls all
15. A Simple Command: echo
● Function:
To display all arguments to STDOUT(screen),
plus an ending <newline> character.
16. A Simple Command: echo
● Major options:
n : disable the trailing <newline>
e : enable interpretation of escapes ()
17. Escaped Characters in echo
● Most Frequently Used:
backslash
b backspace
c produce no further output
n new line
r carriage return
t horizontal tab
v vertical tab
0NNN byte with octal value
xHH byte with hexadecimal value
18. Examples of echo
● Using n option:
$ echo first line
first line
$ echo n first line
first line $
19. Examples of echo
● Using escape character:
$ echo e "atbtcndtetf"
a b c
d e f
20. Examples of echo
● Using escape with octal value:
$ echo e
"141011142011143012144011145
011146"
a b c
d e f
21. Examples of echo
● Using escape with hex value:
$ echo e
"x61x09x62x09x63x0ax64x09x65
x09x66"
a b c
d e f
23. Character Type in Command Line
● Literal Character:
Plain text, no function
123456 abcdefg …
● Meta Character:
Reserved with functions
24. Frequently Used Meta Characters
= : set variable value
$ : variable substitution
> : redirect to STDOUT
< : redirect from STDIN
| : pipe line
& : background running
() : run commands in nested sub-shell
{} : command grouping
; : run commands in frequency
&& : run command while TRUE
|| : run command while FALSE
! : re-run command in history
47. Process Hierarchy
● Every command issues a process
when running.
● A command process is a child, and
the shell is the parent.
● Child process returns a value($?) to
parent when exists.
shell
command
Return
Value
48. Process Environment
● Child process inherit it's environment
from parent.
● However, any changing of
environment in a child will NEVER
effect the parent!
50. Script Running
● Using a sub shell to run commands:
Environment changing only effects the sub
shell.
● Interpreter is defined at the first line:
#!/path/to/shell
shell
sub shell
commands
51. Source Running
● Using source command to run script:
● There is no sub shell, interpreter is ignored.
● Environment changing effects the current
shell.
shell
commands
52. Exec Running
● Using exec command to run script:
● The current shell is terminated when script
starting.
● The process is hanged over to interpreter.
shell
commands
interpreter
53. Practice and Understand
● Write a shell script (my.sh):
pwd
cd /tmp
pwd
sleep 3
● Make it executable:
$ chmod +x my.sh
54. Practice and Understand
● Run the script in different ways:
$ ./my.sh
$ pwd
$ . ./my.sh
$ pwd
$ cd
$ exec ./my.sh
Identical to:
source ./my.sh
56. Sequence Running
● Using the ; symbol:
$ cmd1 ; cmd2; cmd3
● Equivalent to:
$ cmd1
$ cmd2
$ cmd3
57. Command Grouping Method
● Using {} to run command group in
current shell:
$ { cmd1 ; cmd2; cmd3; }
● Using () to run command group in a
nested sub shell:
$ ( cmd1 ; cmd2; cmd3 )
58. Command Grouping Effect
● Using {} :
Environment changing effects the
current shell.
● Using ():
Environment changing does NOT
effects the current shell.
59. Named Command Group
● Also known as function
Command group is run when calling the
function name.
$ my_function () {
cmd1
cmd2
cmd3
}
$ my_function
77. Script Parameter
● Assigned by command line:
script_name par1 par2 par3 ...
● Reset by set command:
set par1 par2 par3 ...
● Separated by <IFS> :
set “par1 par2” par3 ...
78. Parameter Gathering
● Substituted by $n (n=position) :
$0 $1 $2 $3 ...
● Position:
$0 : script_name itself
$1 : the 1st parameter
$2 : the 2nd parameter
and so on...
79. Parameter Substitution
● Reserved variables:
${nn} : position greater than 9
$# : the number of parameters
$@ or $* : All parameters individually
“$*” : All parameters in one
“$@” : All parameters with position reserved
83. File Descriptor (FD)
● Processes use File Descriptors to
input or output (I/O) data with system
● Each process has 256 FD
●The first 3 FD are standard
0 : Standard Input (STDIN)
1 : Standard Output (STDOUT)
2 : Standard Error Output (STDERR)
84. Standard FD
● By default, each standard FD is
connected to an I/O device:
STDIN : Keyboard
STDOUT : Screen
STDERR : Screen
processFD0
FD2
FD1
keyboard screen
85. IO Redirection
● Standard FD can be changed in
command line:
STDIN : < file
STDOUT : > file
STDERR : 2> file
processFD0
FD2
FD1
keyboard screen
86. IO Redirection Example
● Command default:
$ mail -s test root
this is a test mail.
please skip.
^d
91. Output Appending
● Keep the existing content in target
file by using >> :
$ find /etc >/dev/null 2>> std.out
92. HERE Document
● Multiple line input by using << TAG :
$ mail -s test root << .
this is a test mail.
please skip.
.
93. Output Combination
● Save STDOUT and STDERR to a same
file by using 2>&1 :
$ cat std.out std.none > std.both 2>&1
Or
$ cat std.out std.none &> std.both
● Note:
Order is important! cmd1
FD2
FD1
FD0
94. Pipe Line
● Connect STDOUT of left command to
STDIN of right command:
$ cmd1 | cmd2
cmd1
FD2
FD1
FD0
screen
cmd2
FD2
FD1
FD0
95. Pipe Line
● Combine STDERR into STDOUT in
pipe line:
$ cmd1 2>&1 | cmd2
cmd1
FD2
FD1
FD0
screen
cmd2
FD2
FD1
FD0
96. Output Splitting
● Tap an output copy to a file by using
command tee :
$ cmd1 | tee file | cmd2
$ cmd1 | tee -a file | cmd2
cmd1
FD2
FD1
FD0
tee file
97. Xargs
● Change the STDIN to be as argument
by using xargs :
$ cmd1 | xargs cmd2
cmd1
FD2
FD1
FD0
cmd2 --opt args...
FD0
99. Return Value (RV)
● Every command has a Return Value
when exists, also called Exist Status.
● Specified by exit in script:
exit [n]
Or, inherited from last command
● Using $? to have RV of last command
123. The for Loop
● Syntax:
for VAR in values ...
do
commands ...
done
124. The for Loop
● Tips:
● A new variable is set when running a for loop
● The value sources are vary.
● The times of loop depends on the number of
value.
● Each value is used once in the dodone
statement in order.
126. The while Loop
● Tips:
● The cmd1 is run at each loop cycle.
● The dodone statement only be run while cmd1
returns TRUE value.
● The whole loop is terminated once cmd1 returns
FALSE value.
● Infinity loop may be designed in purpose, or
accidentally.
128. The until Loop
● Tips:
● The cmd1 is run at each loop cycle.
● The dodone statement only be run while cmd1
returns FALSE value.
● The whole loop is terminated once cmd1 returns
TRUE value.
129. Using break In Loop
● Loop flow:
loop
condition
do
cmd ...
break
cmd ...
done
130. Using break In Loop
● Tips:
● The loop is terminated once break runs.
● A number can be specified to break the Nth
outbound loop.
1
2
3
131. Using continue In Loop
● Loop flow:
loop
condition
do
cmd ...
continue
cmd ...
done
132. Using continue In Loop
● Tips:
● The remained lines in current loop cycle are
omitted once continue runs, script goes straight
to continue next cycle.
● A number can be specified to continue the Nth
outbound loop.
133. Using sleep In Loop
● Tips:
● The script is temporally stopped when the
sleep runs.
● A number of second can be specified to stop the
script in how long.
● Useful for periodical jobs with infinity loop.
135. Regular Expression (RE)
● Tips:
● Processing in line base.
● Meta characters may conflict with shell, mu be
quoted.
● Commonly used in text filtering:
grep, sed, awk, perl, php, etc...
136. Regular Expression
● Character set:
abc : individual character
(abc) : a set of character
(abc|xyz) : one set of
[abc] : one character of list
[^abc] : one character of non-list
137. Regular Expression
● Anchor Characters:
^ : the beginning of line
$ : the end of line
< : the beginning of word
> : the end of word
138. Regular Expression
● Modifier
To modify the preceding character or set:
* : zero or more times
? : zero or one times
+ : one or more times
{n} : n times
{n,m} : n to m times