shell array and declare usage
Usage description
The declare command is a built-in command of bash, which can be used to declare shell variables and set the attributes of variables (Declare variables and/or give them attributes). This command can also be written as typeset. Although people rarely use this command, if you know some of its usage, you will find that this command is quite useful.
Common parameters
Format: declare
Format: typeset
Format: declare -p
Format: typeset -p
Display the values of all variables.
Format: declare -p var
Format: typeset -p var
Display the value of the specified variable var.
Format: declare var=value
Format: typeset var=value
Format: var=value
< p> Declare variables and assign values.
Format: declare -i var
Format: typeset -i var
Define the variable var as an integer. After that, you can directly evaluate the expression, and the result can only be an integer. If the evaluation fails or is not an integer, it is set to 0.
Format: declare -r var
Format: typeset -r var
Format: readonly var
Declare the variable var as a read-only variable. Read-only variables are not allowed to be modified or deleted.
Format: declare -a var
Format: typeset -a var
Declare the variable var as an array variable. But this is not necessary. All variables can be used as arrays without explicitly defining them. In fact, in a sense, it seems that all variables are arrays, and assigning a value to a variable without a subscript is the same as assigning a value to “[0]”.
Format: declare -f
Format: typeset -f
Display all custom functions, including names and function bodies.
Format: declare -F
Format: typeset -F
Display the names of all custom functions.
Format: declare -f func
Format: typeset -f func
Only display the function definition of the specified function func.
Format: declare -x var
Format: typeset -x var
Format: export var
Set the variable var to an environment variable so that it can be used in subsequent scripts and programs.
Format: declare -x var=value
Format: typeset -x var=value
Format: export var=value
Set the variable var to the Chen environment variable and assign it to value.
Example of use
Example one declare is a built-in command
[[emailprotected] ~]#type -a declare< br>declare is a shell builtin
[[emailprotected] ~]#
[[emailprotected] ~]#type -a typeset
typeset is a shell builtin
[[ email protected] ~]#
Example 2: After declare -i, you can directly evaluate the expression
[[emailprotected] ~]#x= 6/3
[[emailprotected] ~]#echo $x
6/3
[[emailprotected] ~]#declare -ix
[[emailprotected] ~]#echo $ x
6/3
[[emailprotected] ~]#x=6/3
[[emailprotected] ~]#echo $x
2
If variable Is declared as an integer, you can assign an expression directly to it, and bash will evaluate it.
[[email protected] ~]#x=error
[[email protected] ~]#echo $x
0
If the variable is declared as an integer, When an expression whose result is not an integer is assigned to it, it becomes 0.
[[email Protected] ~]#x=3.14
-bash: 3.14: syntax error: invalid arithmetic operator (error token is “.14”)
If the variable is declared as an integer, assigning a decimal number (floating point number) to it will not work. Because bash does not have built-in support for floating point numbers.
[[email protected] ~]#
[[email protected] ~]#declare +i x
The result of this command is to cancel the integer type attribute of the variable x.
[[email Protected] ~]#x=6/3
[[email Protected] ~]#echo $x
6/3
Because the variable x is not an integer variable , So the expression is not automatically evaluated. The following two methods can be used.
[[emailprotected] ~]#x=$[6/3]
[[emailprotected] ~]#echo $x
2
[[emailprotected] ~ ]#x=$((6/3))
[[emailprotected] ~]#echo $x
2
[[emailprotected] ~]#
Example three Declare read-only variables
[[emailprotected] ~]#declare -rr
[[emailprotected] ~]#echo $r
[[emailprotected] ~]# r=xxx
-bash: r: readonly variable
[[emailprotected] ~]#declare -rr=xxx
-bash: declare: r: readonly variable
[[emailprotected] ~ ]#declare +rr
-bash: declare: r: readonly variable
[[emailprotected] ~]#
[[emailprotected] ~]#declare +rr
-bash: declare: r: readonly variable
[[emailprotected] ~]#
[[emailprotected] ~]#unset r
-bash: unset: r: cannot unset: readonly variable
[[emailprotected] ] ~]#
Example 4 Declare array variables (in fact, any variable can be operated as an array)
[[email protected] ~]#declare -a names
[[emailprotected] ~]#names=Jack
[[emailprotected] ~]#echo ${names[0]}
Jack
[[emailprotected] ~]#names[1] =Bone
[[email protected] ~]#echo ${names[1]} Bone
[[emailprotected] ~]#echo ${names}
Jack
[[emailprotected] ~]#echo ${names [*]}
Jack Bone
[[email protected] ~]#echo ${#names}
4
Quoting names directly is equivalent to quoting names[0]
[[email Protected] ~]#echo ${#names[*]}
2
[[email Protected] ~]#echo ${names[@]}
Jack Bone
[[email Protected] ~]#echo ${#names[@]}
2
[[email Protected] ~]#declare -p names
declare -a names ='([0]=”Jack” [1]=”Bone”)’
[[emailprotected] ~]#
Sample five shows function definitions
[ [email protected] ~]#declare -F
declare -f add_jar
declare -f add_jar2
declare -f add_jar3
[[emailprotected] ~]#declare -f
add_jar ( )
{
[-e $1] && CLASSPATH=$CLASSPATH:$1
}
add_jar2 ()
{
if [-e $1 ]; then
CLASSPATH= $CLASSPATH:$1;
else
if [-e $2 ]; then
CLASSPATH=$CLASSPATH:$2;
fi;
fi;
add_jar3 ()< br>{
if [-e $1 ]; then
CLASSPATH=$CL ASSPATH:$1;
else
if [-e $2 ]; then
CLASSPATH=$CLASSPATH:$2;
CLASSPATH=$CLASSPATH:$2;
CLASSPATH= CLASSPATH if [-e $3 ]; then
CLASSPATH:$3;
fi;
fi;
fi
}
[[emailprotected] ~]#declare -f add_jar
add_jar ()
{
[-e $1] && CLASSPATH=$CLASSPATH:$1
}
[[emailprotected] ~]#declare -F add_jar
add_jar
[[emailprotected] ~]#declare -F add_
[[emailprotected] ~]#declare -F add_*
[[emailprotected] ~]#declare -F’add_*’
[[emailprotected] ~]#
< h3>Shell script generates non-repeated random numbers
#!/bin/bash#AUTHOR:AN#DATE:2019-3-24#Describe:Generate No-Repeat Random-Number#Method: as generated Random number 3~7, first generate random number 0~4 (7-3), then add 3 (starting value) to modify ################## ############################################## ##echo “Generate non-repeated random positive integers!!!” read -p “Enter the start value of the random number range:” Start_Numread -p “Enter the end value of the random number range:” End_Num#Control the length of the array, and The remainder used for RANDOM Array_Length=$[End_Num-Start_Num+1]#Define the array to store the final random number typeset RAND #Generate $Array_Length random numbers in a loop for ((i=0;i< $Array_Length;i++)) do Rnum=$[RANDOM%$Array_Length+Start_Num] #Extract the length of the array Length=${#RAND[@]} #Double if statement idea: first assign the first value to the array, and then take The random number generated later is compared with the elements of the array in turn #If the same, regenerate a random number, if different, store it in the array if [$Length -eq 0 ]; then RAND[$i]=$Rnum else for ((j = 0; j <$ Length; j ++)) do [$ Rnum = $ {RAND [$ j]}!] if; then continue else Rnum = $ [RANDOM% $ array_Length + start_Num] j = -1 fi done RAND [ $i]=$Rnum fi done#Output the value of the array for ((x=0;x<$Array_Length;x++)) do echo ${RAND[$x]}done
shell array and declare usage
< /div>
shell array and declare usage
< /p>
Usage of shell array and declare
Usage description
The declare command is a built-in command of bash. It can be used to declare shell variables and set the attributes of variables (Declare variables and/or give them att ributes). This command can also be written as typeset. Although people rarely use this command, if you know some of its usage, you will find that this command is quite useful.
Common parameters
Format: declare
Format: typeset
Format: declare -p
Format: typeset -p
Display the values of all variables.
Format: declare -p var
Format: typeset -p var
Display the value of the specified variable var.
Format: declare var=value
Format: typeset var=value
Format: var=value
< p> Declare variables and assign values.
Format: declare -i var
Format: typeset -i var
Define the variable var as an integer. After that, you can directly evaluate the expression, and the result can only be an integer. If the evaluation fails or is not an integer, it is set to 0.
Format: declare -r var
Format: typeset -r var
Format: readonly var
Declare the variable var as a read-only variable. Read-only variables are not allowed to be modified or deleted.
Format: declare -a var
Format: typeset -a var
Declare the variable var as an array variable. But this is not necessary. All variables can be used as arrays without explicitly defining them. In fact, in a sense, it seems that all variables are arrays, and assigning a value to a variable without a subscript is the same as assigning a value to “[0]”.
Format: declare -f
Format: typeset -f
Display all custom functions, including names and function bodies.
Format: declare -F
Format: typeset -F
Display the names of all custom functions.
Format: declare -f func
Format: typeset -f func
Only display the function definition of the specified function func.
Format: declare -x var
Format: typeset -x var
Format: export var
Set the variable var to an environment variable so that it can be used in subsequent scripts and programs.
Format: declare -x var=value
Format: typeset -x var=value
Format: export var=value
Set the variable var to the Chen environment variable and assign it to value.
Example of use
Example one declare is a built-in command
[[emailprotected] ~]#type -a declare< br>declare is a shell builtin
[[emailprotected] ~]#
[[emailprotected] ~]#type -a typeset
typeset is a shell builtin
[[ email protected] ~]#
Example 2: After declare -i, you can directly evaluate the expression
[[emailprotected] ~]#x= 6/3
[[emailprotected] ~]#echo $x
6/3
[[emailprotected] ~]#declare -ix
[[emailprotected] ~]#echo $ x
6/3
[[emailprotected] ~]#x=6/3
[[emailprotected] ~]#echo $x
2
If variable Is declared as an integer, you can assign an expression directly to it, and bash will evaluate it.
[[email protected] ~]#x=error
[[email protected] ~]#echo $x
0
If the variable is declared as an integer, When an expression whose result is not an integer is assigned to it, it becomes 0.
[[email Protected] ~]#x=3.14
-bash: 3.14: syntax error: invalid arithmetic operator (error token is “.14”)
If the variable is declared as an integer, assigning a decimal number (floating point number) to it will not work. Because bash does not have built-in support for floating point numbers.
[[email protected] ~]#
[[email protected] ~]#declare +i x
The result of this command is to cancel the integer type attribute of the variable x.
[[email Protected] ~]#x=6/3
[[email Protected] ~]#echo $x
6/3
Because the variable x is not an integer variable , So the expression is not automatically evaluated. The following two methods can be used.
[[emailprotected] ~]#x=$[6/3]
[[emailprotected] ~]#echo $x
2
[[emailprotected] ~ ]#x=$((6/3))
[[emailprotected] ~]#echo $x
2
[[emailprotected] ~]#
Example three Declare read-only variables
[[emailprotected] ~]#declare -rr
[[emailprotected] ~]#echo $r
[[emailprotected] ~]# r=xxx
-bash: r: readonly variable
[[emailprotected] ~]#declare -rr=xxx
-bash: declare: r: readonly variable
[[emailprotected] ~ ]#declare +rr
-bash: declare: r: readonly variable
[[emailprotected] ~]#
[[emailprotected] ~]#declare +rr
-bash: declare: r: readonly variable
[[emailprotected] ~]#
[[emailprotected] ~]#unset r
-bash: unset: r: cannot unset: readonly variable
[[emailprotected] ] ~]#
Example 4 Declare array variables (in fact, any variable can be operated as an array)
[[email protected] ~]#declare -a names
[[emailprotected] ~]#names=Jack
[[emailprotected] ~]#echo ${names[0]}
Jack
[[emailprotected] ~]#names[1] =Bone
[[email protected] ~]#e cho ${names[1]}
Bone
[[email Protected] ~]#echo ${names}
Jack
[[email Protected] ~]#echo ${names[*] }
Jack Bone
[[email protected] ~]#echo ${#names}
4
Quoting names directly is equivalent to quoting names[0]
[[ email Protected] ~]#echo ${#names[*]}
2
[[emailprotected] ~]#echo ${names[@]}
Jack Bone
[[emailprotected] ~]#echo ${#names[@]}
2
[[emailprotected] ~]#declare -p names
declare -a names='( [0]=”Jack” [1]=”Bone”)’
[[emailprotected] ~]#
Sample five shows function definitions
[[emailprotected ] ~]#declare -F
declare -f add_jar
declare -f add_jar2
declare -f add_jar3
[[emailprotected] ~]#declare -f
add_jar ()
{
[-e $1] && CLASSPATH=$CLASSPATH:$1
}
add_jar2 ()
{
if [-e $1 ]; then
CLASSPATH=$CLASSPATH: $1;
else
if [-e $2 ]; then
CLASSPATH=$CLASSPATH:$2;
fi;
fi
}
add_jar3 ()
{
if [-e $1 ]; then
CLASSPATH=$CLASSPATH:$1 ;
else
if [-e $2 ]; then
CLASSPATH=$CLASSPATH:$2;
else
CLASSPATH if [-e $3 ]; then
if [-e $3 ]; then
;
fi;
fi;
fi
}
[[emailprotected] ~]#declare -f add_jar
add_jar ()
{
[-e $1] && CLASSPATH=$CLASSPATH:$1
}
[[emailprotected] ~]#declare -F add_jar
add_jar
[[emailprotected] ~]#declare -F add_
[[emailprotected] ~]#declare -F add_*
[[emailprotected] ~]#declare -F’add_*’
[[emailprotected] ~]#
Shell The script generates non-repeated random numbers
#!/bin/bash#AUTHOR:AN#DATE:2019-3-24#Describe:Generate No-Repeat Random-Number#Method: such as generating 3~7 Random numbers of 0~4 (7-3) are generated first, and 3 (starting value) is added to modify ##################### ###############################################echo “Generate non-repeated random positive integers!!!” read -p “Enter the start value of the random number range:” Start_Numread -p “Enter the end value of the random number range:” End_Num#Controls the length of the array and is used in RANDOM The remainder Array_Length=$[End_Num-Start_Num+1]#Define the array to store the final random number typeset RAND #Generate $Array_Length random numbers in a loop for ((i=0;i<$Array_Lengt h;i++)) do Rnum=$[RANDOM%$Array_Length+Start_Num] #Extract the length of the array Length=${#RAND[@]} #Double-if statement idea: first assign the first value to the array, then take the following The generated random numbers are compared with the elements of the array in turn #If the same, regenerate a random number, if different, store it in the array if [$Length -eq 0 ]; then RAND[$i]=$Rnum else for ((j= 0; j <$ Length; j ++)) do if [! $ Rnum = $ {RAND [$ j]}]; then continue else Rnum = $ [RANDOM% $ array_Length + start_Num] j = -1 fi done RAND [$ i]=$Rnum fi done#Output the value of the array for ((x=0;x<$Array_Length;x++)) do echo ${RAND[$x]}done
Usage description
The declare command is a built-in command of bash, which can be used to declare shell variables and set the attributes of variables (Declare variables and/or give them attributes). This command can also be written as typeset. Although people rarely use this command, if you know some of its usage, you will find that this command is quite useful.
Common parameters
Format: declare
Format: typeset
Format: declare -p
Format: typeset -p
Display the values of all variables.
Format: declare -p var
Format: typeset -p var
Display the value of the specified variable var.
Format: declare var=value
Format: typeset var=value
Format: var=value
< p> Declare variables and assign values.
Format: declare -i var
Format: typeset -i var
Define the variable var as an integer. After that, you can directly evaluate the expression, and the result can only be an integer. If the evaluation fails or is not an integer, it is set to 0.
Format: declare -r var
Format: typeset -r var
Format: readonly var
Declare the variable var as a read-only variable. Read-only variables are not allowed to be modified or deleted.
Format: declare -a var
Format: typeset -a var
Declare the variable var as an array variable. But this is not necessary. All variables can be used as arrays without explicitly defining them. In fact, in a sense, it seems that all variables are arrays, and assigning a value to a variable without a subscript is the same as assigning a value to “[0]”.
Format: declare -f
Format: typeset -f
Display all custom functions, including names and function bodies.
Format: declare -F
Format: typeset -F
Display the names of all custom functions.
Format: declare -f func
Format: typeset -f func
Only display the function definition of the specified function func.
Format: declare -x var
Format: typeset -x var
Format: export var
Set the variable var to an environment variable so that it can be used in subsequent scripts and programs.
Format: declare -x var=value
Format: typeset -x var=value
Format: export var=value
Set the variable var to the Chen environment variable and assign it to value.
Example of use
Example one declare is a built-in command
[[emailprotected] ~]#type -a declare< br>declare is a shell builtin
[[emailprotected] ~]#
[[emailprotected] ~]#type -a typeset
typeset is a shell builtin
[[ email protected] ~]#
Example 2: After declare -i, you can directly evaluate the expression
[[emailprotected] ~]#x= 6/3
[[emailprotected] ~]#echo $x
6/3
[[emailprotected] ~]#declare -ix
[[emailprotected] ~]#echo $ x
6/3
[[emailprotected] ~]#x=6/3
[[emailprotected] ~]#echo $x
2
If variable Is declared as an integer, you can assign an expression directly to it, and bash will evaluate it.
[[email protected] ~]#x=error
[[email protected] ~]#echo $x
0
If the variable is declared as an integer, When an expression whose result is not an integer is assigned to it, it becomes 0.
[[email Protected] ~]#x=3.14
-bash: 3.14: syntax error: invalid arithmetic operator (error token is “.14”)
If the variable is declared as an integer, assigning a decimal number (floating point number) to it will not work. Because bash does not have built-in support for floating point numbers.
[[email protected] ~]#
[[email protected] ~]#declare +i x
The result of this command is to cancel the integer type attribute of the variable x.
[[email Protected] ~]#x=6/3
[[email Protected] ~]#echo $x
6/3
Because the variable x is not an integer variable , So the expression is not automatically evaluated. The following two methods can be used.
[[emailprotected] ~]#x=$[6/3]
[[emailprotected] ~]#echo $x
2
[[emailprotected] ~ ]#x=$((6/3))
[[emailprotected] ~]#echo $x
2
[[emailprotected] ~]#
Example three Declare read-only variables
[[emailprotected] ~]#declare -rr
[[emailprotected] ~]#echo $r
[[emailprotected] ~]# r=xxx
-bash: r: readonly variable
[[emailprotected] ~]#declare -rr=xxx
-bash: declare: r: readonly variable
[[emailprotected] ~ ]#declare +rr
-bash: declare: r: readonly variable
[[emailprotected] ~]#
[[emailprotected] ~]#declare +rr
-bash: declare: r: readonly variable
[[emailprotected] ~]#
[[emailprotected] ~]#unset r
-bash: unset: r: cannot unset: readonly variable
[[emailprotected] ] ~]#
Example 4 Declare array variables (in fact, any variable can be operated as an array)
[[email protected] ~]#declare -a names
[[emailprotected] ~]#names=Jack
[[emailprotected] ~]#echo ${names[0]}
Jack
[[emailprotected] ~]#names[1] =Bone
[[emailprotected] ~]#echo $ {names[1]}
Bone
[[email protected] ~]#echo ${names}
Jack
[[email protected] ~]#echo ${names[*]}< br>Jack Bone
[[emailprotected] ~]#echo ${#names}
4
Quoting names directly is equivalent to quoting names[0]
[[emailprotected] ] ~]#echo ${#names[*]}
2
[[email protected] ~]#echo ${names[@]}
Jack Bone
[[ emailprotected] ~]#echo ${#names[@]}
2
[[emailprotected] ~]#declare -p names
declare -a names='([0 ]=”Jack” [1]=”Bone”)’
[[emailprotected] ~]#
Sample five shows function definitions
[[emailprotected] ~ ]#declare -F
declare -f add_jar
declare -f add_jar2
declare -f add_jar3
[[emailprotected] ~]#declare -f
add_jar ()
{
[-e $1] && CLASSPATH=$CLASSPATH:$1
}
add_jar2 ()
{
if [-e $1 ]; then
CLASSPATH=$CLASSPATH:$1;
else
if [-e $2 ]; then
CLASSPATH=$CLASSPATH:$2;
fi;
fi
}
add_jar3 ()
{
If [-e $1 ]; then
CLASSPATH=$CLASSPATH:$1;
e lse
if [-e $2 ]; then
CLASSPATH=$CLASSPATH:$2;
else
if [-e $3 ]; then
CLASSPATH=$3; then
CLASSPATH=$3; fi;
fi;
fi
}
[[emailprotected] ~]#declare -f add_jar
add_jar ()
{
[-e $1] && CLASSPATH =$CLASSPATH:$1
}
[[emailprotected] ~]#declare -F add_jar
add_jar
[[emailprotected] ~]#declare -F add_
[[emailprotected] ] ~]#declare -F add_*
[[emailprotected] ~]#declare -F’add_*’
[[emailprotected] ~]#
Shell script generation is not repeated Random number
#!/bin/bash#AUTHOR:AN#DATE:2019-3-24#Describe:Generate No-Repeat Random-Number#Method: If generating a random number from 3 to 7, First generate a random number of 0~4 (7-3), then add 3 (starting value) to modify ######################## ############################################echo “Generates no repetition Random positive integer!!!”read -p “Enter the start value of the random number range:” Start_Numread -p “Enter the end value of the random number range:” End_Num#Controls the length of the array and is used for the remainder of RANDOM Array_Length =$[End_Num-Start_Num+1]#Define an array to store the final random number typeset RAND #Generate $Array_Length random numbers in a loop for ((i=0;i<$Array_Length;i++)) do Rnum=$[RANDOM%$Array_Length+Start_Num] #Extract the length of the array Length=${#RAND[@]} #Double-if statement idea: first assign the first value to the array, and then take the random number generated later Compare the elements of the array #If the same, regenerate a random number, if different, store it in the array if [$Length -eq 0 ]; then RAND[$i]=$Rnum else for ((j=0;j<$Length ; j ++)) do if [! $ Rnum = $ {RAND [$ j]}]; then continue else Rnum = $ [RANDOM% $ array_Length + start_Num] j = -1 fi done RAND [$ i] = $ Rnum fi done#Output the value of the array for ((x=0;x<$Array_Length;x++)) do echo ${RAND[$x]}done
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5487 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC