first, the application of shell variables
1. Types of Shell variables
·User-defined variables: defined, modified, and used by the user
·Pre-defined variables: Bash predefined special variables that cannot be modified directly
·Positional variables: Passing execution parameters to a program from the command line
2. Assignment and reference of variables
·Define a new variable variable name to start with an English letter or underscore, case-sensitive: Variable name = variable Value
·View the value format of the variable: ECHO $ variable name echo "My name is $name" echo "My name is ${NAME}BD"
·Variable assignment format from keyboard input: Read [-P ' info '] variable name
·Assign a variable quotation mark "": Allow references to other variable values by $ symbol single quote ': Disallow reference to other variable values, $ as normal character anti-apostrophe · : Output The result of a command execution to a variable note: The default single, double quotes cannot parse an escape character (such as/n) for example a:
Cat Demo. SH #!/bin/"Enter you name:" nameecho"my Name is $name" [ [email protected]]#. /demo. SH Enter name:tommy name is Tom
Example B:
Cat Demo. SH #!/bin/"Enter you name:" nameecho'my Name is $name'sh demo. SH Enter name:user44my name is $name
Example C:
Cat Demo. SH #!/bin/"Enter you name:" nameecho'my Nameis' $name [[email protected]]#. /demo. SH Enter name:user55my name is User55
Example D:
[[Email protected]]#CatDemo.SH#!/bin/Bashread-P"Enter you name:"nameEcho '$name/n $name/n $name'Echo "$name/n $name/n $name"[[Email protected]]#SHDemo.SHEnter you name:user$name/n $name/N $nameuser/n user/n User
Example e:
[Email protected] scripts]#CatTest.SH#!/bin/BASHSTR1=`Echo 123' str2=`Date`Echo$str 1;Echo "today is: $str 2";Echo "today is: $STR 2!!!"; [Email protected] scripts]#./test.SH123today is:2016 May 24 Week 221: -: -csttoday is:2016 May 24 Tuesday +: -: -Cst!!!
Vim edit file Tip: After the ESC key: Set NU can set the display line number;: Set Nonu can set the line number to disappear
3. Delete Variables
·Clear user-defined variable format: unset variable name example:
Cat Test. SH #!/bin/bash#test. SH name='user123'echo $name unset nameecho sh test. SH user123
Note: The second act is empty
5. Position variable
·Represents a number between $n,n and 1~9
[Email protected] scripts]#CatDemo.SH#!/bin/BashEcho$1Echo$2Echo$3Echo$4Echo$5Echo$6Echo$7Echo$8Echo$9Echo$Ten[email protected] scripts]#./demo.SHa b c d e F g h i jabcdefghia0
Example:
[Email protected] scripts]#CatDemo.SH#!/bin/Bash Case$1 inchstart)Echo 'start ...';; Stop)Echo 'stop ...';;Esac[email protected] scripts]#./demo.SHStartStart ... [Email protected] scripts]#./demo.SHstopstop ...
4. Pre-defined variables
·The representation is as follows: $#: The number of positional arguments in the command line $*: The contents of all positional parameters $?: The state returned after the previous command was executed, when the return status value is 0, which indicates that execution is normal, and that a value other than 0 indicates an exception or an error $: The currently executing process/program name example a:
$ A displays the name of the current script
Cat A.sh#!/bin/bashecho" the name of the script you are currently executing is:" $0Echo $0Echo $0
[[email protected] scripts]#./a.sh
当前您正在执行的脚本名称是:./a.sh
./a.sh
./a.sh
Example B:
Cat A.sh#!/bin/bashecho" a total of ${#} parameters " The contents of the echo "parameter are: $*"[[email protected] scripts]#. /A.sh a b c d e F g h i j k l m n o p q A total of 17 parameter parameters are: a b c d e F g h i j k l m n o p q
Example C:
cat B.sh#!/bin/for in $*; do Echo $i Done [email protected] scripts]#. /b.sh a cabc
Example D:
Cat Test. SH #!/bin/bashtot=0 for in $*; Do tot=$ (($tot +$i))doneecho $tot [[email protected] scripts]#. /test. SH 12345 the
Example e:
Cat Temp. SH #!/bin/ls -l-echo $? [email protected] scripts]#. /temp. SH Total dosage 8-rwxr-xr-x. 1 root root 285 month 2414: temp. sh-rwxr-xr-x. 1 root root 695 month 2414: test. SH 0
Example F:
[Email protected] scripts]#CatTemp.SH#!/bin/Bash#llls if[[$?==0]]; Then Echo 'Previous command executed successfully'Else Echo 'Previous command failed to execute'fi[email protected] scripts]#./temp.SHtemp.SHTest.SH
上一条命令执行成功
6. Calculate the result of an integer expressionFormat: Expr variable 1 operator variable 2 ... [operator Variable n]
Common operators for expr (now less, in a simple way)Addition operations: + Subtraction Operations:-Multiplication: \* Division:/Modulo (take-up) operation:%
Cat A.sh#!/bin/expr+expr $1 + $2num= 'expr1+2'echo $num [[ Email protected] scripts]#. /A.sh11553
Now more commonly used is the following method:
[Email protected] scripts]#CatA.SH#!/bin/Bash NUM1=$((4+2)) num2=$((4-2)) num3=$((4*2)) Num4=$((4/2)) NUM5=$((4%2))Echo$num 1Echo$num 2Echo$num 3Echo$num 4Echo$num 5[[email protected] scripts]#./A.SH62820
III. shell variable types and operators