Article Directory
1 $? $# $* $n $0
$?
Once on output, it said if it is0
: that success; instead0
, indicates failure$#
It represents the script parameters个数
$*
It represents the value of the corresponding parameter$n
Expressed as (n>=1
) parameter$0
Indicates that the script name
###这是测试脚本
#!/bin/sh
echo 这是脚本名字:$0
echo 总共有$#个人,分别是$*
echo 第一个人是$1,第二个人是$2
### 这是运行结果
这是脚本名字:sympol.sh
总共有2个人,分别是李四 张三
第一个人是李四,第二个人是张三
$?
Test:
2 semicolon
分号
It is among more than one statement 分隔符号
, when only a statement 末尾无需分号
, after the last statement is also 无需分号
, otherwise an error
3 quotes
单引号
Is the original string
双引号
can be extended for special characters
generally do not write is that when using double quotes
a=bcdef
echo $a # 输出bcdef
echo "$a" #双引号将进行变量扩展 ,输出bcdef
echo '$a' #单引号直接输出$a
4 brackets role
4.1 Command substitution
$( )
And `backtick ( tab
key top) the same function: Replace command
4.2 numerical operations
$(( ))
Is an integer of numerical operations, can also be used(( ))
in place of- Another
$[ ]
is performing mathematical operations;
4.3 test operation
[ ]
In place of test
operators, it can also be used [[ ]]
instead of using the double brackets, more rigorous logic
4.4 ${}
4.4.1 delimiters
${}
Delimiters, such as $ab
, the equivalent of ${ab}
, and ${a}b
is just take the a
value of
using linux symbolic example, you can refer to my game to become familiar with linux
4.4.2 take the path, file name, suffix
#假设一个变量名为file
file=/dir1/dir2/dir3/my.file.txt
${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:删掉最后一个 / 及其左边的字符串:my.file.txt
${file#*.}:删掉第一个 . 及其左边的字符串:file.txt
${file##*.}:删掉最后一个 . 及其左边的字符串:txt
${file%/*}:删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:删掉第一个 / 及其右边的字符串:(空值)
${file%.*}:删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
Way to remember are:
#
Counting from the left represents the first (on the keyboard#
at$
the left, pay attention to the use of#
time, remove the symbols*
on the flag symbol (/或.
) to the left)##
: Indicates the last date from the left%
It represents the first counting from the right (on the keyboard%
at$
the right side, pay attention to the use of%
time, remove the symbols*
on the flag symbol (/或.
) to the right)%%
: Indicates the last date from the right单一
It is the symbol最小匹配
;两个
the symbol is最大匹配
*
: Indicates to delete the contents of, for#
and##
circumstances, it位于
specified character (in the example'/'和'.'
) of左边
, and its contents, remove all the characters on the left; for%
and%%
the circumstances, it位于
specified character (in the example'/'和'.'
) is右边
expressed delete the specified character and its contents on the right. Here the*
position not interchangeable, i.e., not the*
number on#
or##
right, and vice versa.
5 (&), or (|)
5.1 and &
&
: Indicates the task in the background, running in the background,redis-server &
&&
: That the previous command executes successfully, only after the implementation of a command, such as:echo '1' && echo '2'
5.2 or |
|
: A piping character,上一条命令的输出
as下一条命令参数
||
: This represents a command execution失败
only after the next command, such as:cat nofile || echo '1'