Disadvantages of Shell Variables
- shell variables are weak types
- The default is string type
declare command
Syntax: declare [+/-] [option] variable name
- - Setting type attributes for variables
- + Cancel the type attribute of a variable
- - a: Declare variables as arrays
- - i: Declare variables as integer s
- - x: Declare variables as environment variables
- - r: Declare variables as read-only variables
- - p: Displays the declared type of the specified variable
#to aa and bb assignment misty@ubuntu:~/MyFile/test$ aa=11 misty@ubuntu:~/MyFile/test$ bb=22 misty@ubuntu:~/MyFile/test$ cc=$aa+$bb misty@ubuntu:~/MyFile/test$ echo $cc 11+22 #Add `i'to automatically convert the value of the operation into character type #Declare cc as an integer and its value is the sum of aa bb misty@ubuntu:~/MyFile/test$ declare -i cc=$aa+$bb misty@ubuntu:~/MyFile/test$ echo $cc 33 #View the type of declared variable misty@ubuntu:~/MyFile/test$ declare -p cc declare -i cc="33"
Declare array variables
#Define arrays misty@ubuntu:~/MyFile/test$ arr[0]=a misty@ubuntu:~/MyFile/test$ arr[1]=b misty@ubuntu:~/MyFile/test$ declare -a arr[2]=cc #View arrays misty@ubuntu:~/MyFile/test$ echo ${arr} a misty@ubuntu:~/MyFile/test$ echo ${arr[2]} cc misty@ubuntu:~/MyFile/test$ echo ${arr[*]} a b cc
Declare environment variables
Grammar: declare-x text = 123
It's similar to export, but it's actually declare command.
Declare variables as read-only properties
Syntax: declare-r test, which gives test a read-only property, but note that the read-only property will not allow variables to be modified, deleted, or even cancelled.
#Temporary effective restart can be deleted, the operation is more troublesome, but do not write in the environment variables or boot automatically executed procedures, or you need to modify the environment variables configuration file. misty@ubuntu:~/MyFile/test$ declare -r test misty@ubuntu:~/MyFile/test$ declare -p test declare -r test misty@ubuntu:~/MyFile/test$ unset test bash: unset: test: Cannot cancel settings: read-only variable misty@ubuntu:~/MyFile/test$ declare +r test bash: declare: test: read-only variable
Method of numerical operation
Method 1: declare-i CC = $aa + $bb
Method 2: dd=$(expr $aa + bb), the value of DD is the sum of AA and bb.
Note that there must be spaces on both sides
Method 3: $((Operational Formula) or $[Operational Formula]
misty@ubuntu:~/MyFile/test$ aa=11 misty@ubuntu:~/MyFile/test$ bb=22 misty@ubuntu:~/MyFile/test$ ff=$(($aa+$bb)) misty@ubuntu:~/MyFile/test$ echo $ff 33 misty@ubuntu:~/MyFile/test$ gg=$[$aa+$bb] misty@ubuntu:~/MyFile/test$ echo $gg 33
> Note that in this way, all operators with or without parentheses are ok
operator
- High priority execution first, low priority execution later
- Priority is the same as that of computation, and is the same as that of other programs.
#The operation of logic and sum is only 1 on both sides of each other. The result of sum is 1. Otherwise, the result of sum is 0. misty@ubuntu:~/MyFile/test$ declare cc=$((1&&0)) misty@ubuntu:~/MyFile/test$ echo $cc 0
### Variable Testing
Generality is not high, only for the shell, can be replaced by other ways, understanding can be used, but not necessary.
> shell scripts help linux managers to manage the system. They do not require high performance, but they need clear logic.
#Delete variable y to ensure that it does not exist misty@ubuntu:~/MyFile/test$ unset y #Testing misty@ubuntu:~/MyFile/test$ x=${y-2} misty@ubuntu:~/MyFile/test$ echo $x 2 #Because y does not exist, the value is x=new #Y value assignment is null misty@ubuntu:~/MyFile/test$ y="" misty@ubuntu:~/MyFile/test$ x=${y-2} misty@ubuntu:~/MyFile/test$ echo $x misty@ubuntu:~/MyFile/test$ #Because y is empty, x is empty. #Y is 1 misty@ubuntu:~/MyFile/test$ y=1 misty@ubuntu:~/MyFile/test$ x=${y-2} misty@ubuntu:~/MyFile/test$ echo $x 1 #x=$y
Operator Notes
[] Test statement
() As an operator, statements are usually placed inside.
(()) Operational Formula for Digital Computing
$() for expressions