shell assignment

One, read receives keyboard input

read [options] [variable name]

Options:

-p prompt message

-t seconds

-n characters

-s hide input data

Example: Use the read prompt to enter the parameter value

 

result:

 

 

 

 Compared with changes in position parameters, it is more friendly

Second, declare the variable type

+ Cancel type attribute of variable

-Set type attributes for variables

-a Set variable type to array

-i declare variable type as integer

-x declare variables as environment variables

-p displays the declared type of the variable

For example 1: a does not define a type, the default is a string, when a is defined as an integer (-i), when assigning a string to a, of course it will fail, the output is 0. View the properties of a (-p ), For plastic surgery


gjl@gjl-virtual-machine:~/shellTest$ declare -p a declare -- a="1" gjl@gjl-virtual-machine:~/shellTest$ declare -i a gjl@gjl-virtual-machine:~/shellTest$ a='we' gjl@gjl-virtual-machine:~/shellTest$ echo $a 0 gjl@gjl-virtual-machine:~/shellTest$ a=12 gjl@gjl-virtual-machine:~/shellTest$ echo $a 12 gjl@gjl-virtual-machine:~/shellTest$ declare -p a declare -i a="12"

Example 2: Declare b as an array and display the array

gjl@gjl-virtual-machine:~/shellTest$ declare -a c='([0]=1 [2]='11' [3]='aa')'
gjl@gjl-virtual-machine:~/shellTest$ echo ${c[2]}
11

Array assignment

1. Add as needed

gjl@gjl-virtual-machine:~/shellTest$ b[0]=1
gjl@gjl-virtual-machine:~/shellTest$ b[1]=2
gjl@gjl-virtual-machine:~/shellTest$ echo ${b[*]}
1 2
gjl@gjl-virtual-machine:~/shellTest$ echo ${b[@]}
1 2

2. Add multiple elements at once 

gjl@gjl-virtual-machine:~/shellTest$ declare -a c
gjl@gjl-virtual-machine:~/shellTest$ c=(1 2 3)
gjl@gjl-virtual-machine:~/shellTest$ echo ${c[*]}
1 2 3

3. Add in sparse format

gjl@gjl-virtual-machine:~/shellTest$ declare -a c='([0]=1 [2]='11' [3]='aa')'
gjl@gjl-virtual-machine:~/shellTest$ echo ${c[2]}
11

The array is not in the order of 0123  

4. Add by read -a

#!/bin/bash
read  -p "please input shuzu:" -a   e
echo ${e[*]}
echo ${e[2]}



gjl@gjl-virtual-machine:~/shellTest$ test07.sh
please input shuzu:3 4 34
3 4 34
34

Note: When referencing variables, there must be at least curly brackets. Use brackets to represent the array. Array elements are separated by "space"

 

 

 

Delete a value in an array / delete an array

gjl@gjl-virtual-machine:~/shellTest$ echo ${c[*]}
1 2 3
gjl@gjl-virtual-machine:~/shellTest$ unset c[2]
gjl@gjl-virtual-machine:~/shellTest$ echo ${c[*]}
1 2
gjl@gjl-virtual-machine:~/shellTest$ unset c
gjl@gjl-virtual-machine:~/shellTest$ echo ${c[*]}

gjl@gjl-virtual-machine:~/shellTest$ 

Cancel the type of variable

For example: a is an integer, use + to delete the integer attribute of a

gjl@gjl-virtual-machine:~/shellTest$ declare -p a
declare -i a="12"
gjl@gjl-virtual-machine:~/shellTest$ declare +i a
gjl@gjl-virtual-machine:~/shellTest$ declare -p a
declare -- a="12"
gjl@gjl-virtual-machine:~/shellTest$

 

Three, operation

 Three methods of numerical operations

①declare (not recommended)

②Use expr or let calculation tool (not recommended)

gjl@gjl-virtual-machine:~$ a=12
gjl@gjl-virtual-machine:~$ b=23
gjl@gjl-virtual-machine:~$ c=$(expr $a + $b)
gjl@gjl-virtual-machine:~$ echo $c
35
gjl@gjl-virtual-machine:~$ let e=$a+$b
gjl@gjl-virtual-machine:~$ echo $e
35
gjl@gjl-virtual-machine:~$ 
 


Matters needing attention: 1 , + space is required on both sides
         2 , $ () means calling system command

 

③Use $ (()) or $ [] to perform numerical operations

gjl@gjl-virtual-machine:~$ a=11 ; b=33
gjl@gjl-virtual-machine:~$ echo c=$(($a+$b))
c=44
gjl@gjl-virtual-machine:~$ echo d=$[$a+$b]
d=44
gjl@gjl-virtual-machine:~$ 

Example: Four arithmetic calculators (first edition)

#!/bin/bash
read -t 30 -p "please input num1:" num1
read -t 30 -p "please input num2:" num2
read -t 30 -p "please input operato:[+-*/]:" oper
[ "$oper" == "+" ] && echo "$(($num1+$num2))" && exit
[ "$oper" == "-" ] && echo "$(($num1-$num2))" && exit
[ "$oper" == "*" ] && echo "$(($num1*$num2))" && exit
[ "$oper" == "/" ] && echo "$(($num1/$num2))" && exit
echo "please input a varible operato"

Disadvantages and points of attention: num1 and num2 are not judged in this example. And [] needs spaces on both sides, otherwise it will report an error

 

4. Variable testing and content replacement

 

For example 1: If the variable a is not set, the x value is the new value. If a is set to null, the x value is null. If set to 123, the value of x is 123.

Can be used to test whether a variable exists or has a value

 

Guess you like

Origin www.cnblogs.com/recommencer/p/12719230.html