Command line parameters,
Positional Parameter
$0
Complete program name
(Basename
Command to remove the path
)
$1
The first parameter, exceeding
9
Hour
$ {10}
$ #
Number of parameters
$ *
All parameters in the command line, as a word
$ @
All parameters in the command line, used as multiple words
$
Current
Shell
Of
PID
$-Shell
Some attributes
$!
The previous background process
PID
Echo
'Basename $0'
#
Obtain the program name
Echo
$ {! #}
#
Obtain the last parameter
For (I = 1;
I <=$ #; ++ I); do
#
Traversal Parameters
Echo $ {! I}
#{}
Cannot be used
$
, Available
!
Substitution
Done
For
In $ @; do
#
Another Traversal method,
$ @
Change
$ *
Yes.
Echo $
Done
Shift
Command
Change the relative position of the command line parameter. The default value is left.
1
Bit, that is
$3
Change
$2
,
$2
Change
$1
,
$1
Discarded
Eg:
Another method for Traversing Parameters
While
[-N "$1"]; do
Echo $1
Shift
Done
Getopts
Analysis parameters
Read
, Get user input
Read
#
Read to variable
$ Reply
Medium
Read name
#
Read
Name
Read-P "input your name:" Name
#
With prompt
Read
Read name age
#
Read multiple variables separated by Spaces
Read-T 5 Name
#
Wait
5
Seconds, return failure
If
Read-T 5 name, then
Read
-N1-P "input [Y/n]" answer
#
Set the number of characters to read-read a single character, so that you do not need to press enter, in
Emacs
But the terminal can
Read
-S-p "input PWD" pwd
#
Do not show input,
Emacs
But the normal terminal can
Read
, Read files
Read once
1
Line, cannot directly take the file name as the parameter, you need to use
Cat
And other commands
Cat
A.txt | while read line
# Read
A redirected application?
Do
Echo $ line
Done
Output
Printf "format" A1 A2...
Eg:
Printf "af 0x % x" 11
#
Same format Controller
C