Linux shell scripts and linuxshell scripts
[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]
Variables and replace operations, the most common mistake when performing variable replacement in the script is the reference error. Therefore, it is necessary to take a look at the meaning and role of various quotation marks.
Reference:
Double quotation marks ""
Double quotation marks can be used to reference any character or string except the characters $, character, and. As follows:
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo -e "hello $SHELL '\n* jesson`echo 20121020`"hello /bin/bash '* jesson20121020
Single quotes''
The single quotes are similar to double quotes. The difference is that shell ignores any reference values. In other words, if the special meaning is blocked (\ is not blocked), all characters in quotation marks, including quotation marks, are considered as a string.
We will replace the double quotation marks with single quotation marks.
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo -e 'hello $SHELL \n* jesson`echo 20121020`'hello $SHELL * jesson`echo 20121020`
We can see that all other special symbols except \ n are blocked.
''
Backticks are used to set the output of system commands to variables. Shell uses the content in the quotation marks as a system command and executes the content.
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo "hello `echo 20121020`"hello 20121020
Backslash \
The backslash can block special meanings. The following characters have special meanings: & * + ^ $ '"| ?.
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo *helloworld.sh parm.sh searchfile.shjesson@jesson-HP:~/develop/workspace/shell_workspace$ echo \**
OPERATOR:
An operator is a command sent to a computer.
Calculation object:
-Number, Character
-Variable
-Expression: the combination of operators and operation objects
Operator type:
& [] Tells the shell to evaluate the expression in the square brackets.
Bitwise OPERATOR:
~ (Inverse operator), <(left shift), >>( right shift), & (and), | (OR), ^ (exclusive or)
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ ~2 ]-3jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2<<1 ]4jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2>>1 ]1jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2&3 ]2jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2|3 ]3jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 2^3 ]1
Logical operators:
& (Logical and), | (logical or), >,=, <,! =
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1&1 ]1jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1&0 ]0jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1|0 ]1jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 1|1 ]1jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[ 0|0 ]0
Assignment operator:
=, + =,-=, * =,/=, % =, & =, ^ =, |=, <=, >>=
Let $ count = $ count + $ change
Let $ count + = $ change
jesson@jesson-HP:~/develop/workspace/shell_workspace$ var=10jesson@jesson-HP:~/develop/workspace/shell_workspace$ let var+=4jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $var14
Expression replacement
$ [] And $ () are used to evaluate expressions.
$ [] Numbers with different base numbers are acceptable:
-[Base # n] n indicates any base number from 2 to 36.
jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $((8+6))14jesson@jesson-HP:~/develop/workspace/shell_workspace$ echo $[10#8+1]9