The role of Linux in special symbols

1 $? $# $* $n $0

  1. $?Once on output, it said if it is 0: that success; instead 0, indicates failure
  2. $#It represents the script parameters个数
  3. $*It represents the value of the corresponding parameter
  4. $nExpressed as ( n>=1) parameter
  5. $0Indicates that the script name
###这是测试脚本
#!/bin/sh
echo 这是脚本名字:$0
echo 总共有$#个人,分别是$*
echo 第一个人是$1,第二个人是$2
### 这是运行结果
这是脚本名字:sympol.sh
总共有2个人,分别是李四 张三
第一个人是李四,第二个人是张三

$?Test:
Here Insert Picture Description

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 ( tabkey top) the same function: Replace command
Here Insert Picture Description

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 testoperators, 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}bis just take the avalue 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'
Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/104310372