Arrays Array
Variables: Storing the memory space of a single element
Array: A contiguous memory space that stores multiple elements, which is equivalent to a collection of multiple variables that support sparse formatting, which is a discontinuous index number.
Advanced variable Usage-variable with type
Shell variables are generally untyped, but Bash provides declare and typeset two commands to specify the type of the variable, and two commands are equivalent
declare [OPTION] variable name
-R declares or displays read-only variables
-I declares or displays integer variables
-a declares or displays an indexed array
-a declares or displays an associative array
-F Displays all functions of the system
-F Displays all function names only
-X declares or displays environment variables and functions
-L declares the variable to be lowercase declare–l var=upper
-U declares the variable to be uppercase Declare–u Var=lower
Declaring an array:
Declare-a array_name//Declaration index Array, element number can only be number, starting from 0
Declare-a array_name//Declaration associative array, element number can be custom format, need to bash4.0 above version to support
Declare-i Array_Name//Declare an array of integers, element variables only support integer values
Declare-r array_name//Declaration only reading group
Declare-l Array_Name//convert array to lowercase
Declare-u Array_Name//convert array to uppercase
Declaring an array
DECLARE-A user//Declaration index Array
DECLARE-A//View index array
DECLARE-A user//Declaration associative array
Declare-a
Declare-i//View all array of integers
array element Assignment , the index array number only supports numbers, and is starting from 0
1. Assigning values to individual elements
user[0]= "User0"
user[1]= "User1"
user[2]= "User2"
user[3]= "User3"
user[4]= "User4"
user[5]= "User5"
2. Assigning values to multiple elements
User= ("UserA" "UserB" "UserC" "userd" ...)
3. Assign only a specific element, the other unassigned element value is empty, such as user[1] is empty
User= ([0]= "UserA" [3]= "UserC" ...)
4. Interactively Assign values
Read-a User
A b c d E//input parameters are separated by a space
A\ b cc EE//If the input parameter contains a space, it needs to be escaped, such as "a B".
displaying or referencing an array
Declare-a
echo $user//Display first element, equivalent to echo ${user[0]}
Echo ${user[0]}
Echo ${user[2]}
Echo ${user[3]}
Echo ${user[*]}
echo ${#user [*]}
To cancel an array or element
unset user[2]//Cancel array element 2
unset user//Cancel entire array
declaring associative arrays
Declare-a User
array element Assignment , numbering supports letters, numbers or words
User[a]=usera
User[b]=userb
User[2]=userc
User[abc]=userabc
View array element values
Declare-a|tail-1
Echo ${user[a]}
Echo ${user[2]}
Echo ${USER[ABC]}
To cancel an array or element
Unset User[a]
unset user
Bulk Definition array elements , non-declared array type default to indexed array
User= (UserA userb UserC userd)
Echo ${user[3]}
Num= ({1..10})
Echo ${num[8]}
echo ${num[*]}//Displays all elements in the array, * also available @ instead
echo ${#num [*]}//Displays the number of elements of the array, * also available @ instead
File= ({/app/bin/*.sh})
Echo ${file[3]}
echo ${file[@]}//Display all elements of an array
echo ${#file [@]}//shows the number of elements in an array
Num= ({1..10})
For ((i=0;i<${#num [@]};i++));d o echo num is ${num[$i]};d One
Unset Num[2]
For ((i=0;i<${#num [@]};i++));d o echo num is ${num[$i]};d one//sparse array display is problematic, that is, there is no automatic recursive filling of null values
Num[2]=3//Adding missing array elements
Array element handling
Array slice: ${array[@]:offset:number}
Offset: Number of elements to skip
Number: How many elements to remove
Num= ({1..10})
echo ${num[*]:2:5}//Skip top 2, remove 5 elements
3 4 5) 6 7
echo ${num[*]:3}//Skip Top 3, remove all elements after
4 5 6 7 8 9 10 11 12 13
SEQ 10//Show 1: 10, default starting from 1 to specified number
SEQ 2 5//display 2,3,4,5, showing the number from the start value to the ending value
SEQ 1 2 10//display 1,3,5,7,9, format as start value, increment value, end value
Num=10;seq 0 2 $[$num-1]
echo {1..10..2}//Display 1,3,5,7,9, format as Start value, end value, increment value
Num= ({1..10..2})
append an element to an array : array[${#ARRAY [*]}]
Num= ({1..10})
echo ${SUM[10]} value is empty
num[${#num [*]}]=11//Append array elements, appended to the last of all array elements
echo ${sum[10]} displays the appended element value 11
num[${#num [*]}]=12//Can be added multiple times
num[${#num [*]}]=13
Deleting an element in an array results in a sparse format: unset Array[index]
Unset Num[8]
Delete entire array : unset array
unset num
String processing
Man bash Search \$\{parameter\}//view command Help
Take character value
Note: When you take a value, the positive number does not need to be preceded by a space, plus a space before negative numbers.
$alp or ${alp} displays string contents
${#var} Displays the length of the string
${VAR:OFFSET:0} Show All strings
${var:offset} skips the first few characters and takes all subsequent characters
${var:offset:length} skips the preceding offset characters, taking the length characters after it
${var:offset:-length} skips the previous offset characters and the length characters, displaying the contents of the middle
${var:-length} takes the rightmost few characters of the string. Note: There is a space after the colon, such as ${num:-3}
${var:-length:-offset} takes the rightmost character of the string and, based on this, skips a few characters and displays the remaining characters.
${var:-length:length} takes the rightmost number of characters from the string, and then takes the length characters out on this basis
Alp= ' echo {a.. Z}|tr-d "" '//Display string ABCDEFG: Xyz
echo $alp//Display string
echo ${#alp}//View the length of the string, that is, the number of characters
echo ${alp:0}//Show All strings
echo ${alp:3}//Skips the first 3 characters, showing all subsequent characters
echo ${alp:3:5}//Skips the first 3 characters, takes 5 characters after it, and displays the DEFGH
echo ${alp:3:-5}//Skips the first 3 characters and the last 5 character, showing the contents of the middle
echo ${alp:-3}//Display last 3 characters, i.e. XYZ
echo ${alp:-3:-2}//First Take out the rightmost 3 characters, then jump after 2 characters, that is, the last display of the character X.
echo ${alp: -3:2}//First remove the rightmost 3 characters and then remove the first 2 characters, which is shown as XY
Delete character
${var#word}//delete the specified prefix character
${var#*word}//left to right, match delete word itself and all characters before word
${var##*word}//greedy mode, match delete Last word itself and all previous characters
${var%word}//delete the specified suffix character
${var%word*}//From right to left, matching deletes all characters after word itself and word. Note the notation * is on the right side of word
${var%%word*}//greedy mode, match delete Last word itself and all subsequent characters
Url=http://www.baidu.com:80
Echo ${url#http://}
echo ${url#*:}
echo ${url##*:}//Fetch port number
Echo ${url%:80}
echo ${url%:*}//Fetch URL
echo ${url%%:*}//Fetch protocol
Fetch path and file name
File= "/var/log/messages"
echo ${file#*/}//Take relative path: var/log/messages
echo ${file##*/}//Take file name: messages, equivalent to basename
echo ${file%/*}//Fetch parent directory:/var/log, equivalent to DirName
Find replacements
${var/pattern/string}//Find and replace the character that was first matched to
${var//pattern/string}//Find and replace all characters that are matched to
${var/#pattern/string}//Find and replace characters that match the beginning of the line
${var/%pattern/string}//Find and replace line endings to match the characters
A=abcabc
echo ${a/b/g}//Match replace once, display as AGCABC
echo ${a//a/g}//Match replace all, display as GBCGBC
echo ${a/#a/g}//Match replace header, shown as Gbcabc
echo ${a/%c/g}//Match replacement line end, shown as ABCABG
Url=http://www.baidu.com:80
echo ${url/%80/8080}//replacement port is 8080
Find Delete
${var/pattern}//Find and delete the first match to the character
${var//pattern}//Find and delete all matching characters
${var/#pattern}//Find and delete characters that match the beginning of the line
${var/%pattern}//Find and delete the characters at the end of the line
A=abcabc
echo ${a/b}//Display as ACABC
Echo ${a//b}//ACAC
echo ${a/#a}//BCABC
Echo ${a/%c}//abcab
Convert case
${var^}//Convert the first letter to uppercase
${var^^}//Convert all lowercase letters to uppercase
${var,}//Convert first letter lowercase
${var,,}//Convert all uppercase letters to lowercase
A=abcabc
B=abcabc
echo ${a^^}//lowercase to uppercase
Echo ${b,,}//uppercase to lowercase
Variable Assignment detection
${var:-word} if the var value is empty or not set, the word value is returned, otherwise the Var value set before is returned
${var:+word} if the var value is empty or not set, the return is empty, otherwise the word value is returned
${var:=word} if the var value is empty or not set, the Var value is set to the word value, that is, the variable value is defined, otherwise the Var value set before is returned
${var:?word} if the var value is empty or not set, returns the error message Word, otherwise returns the Var value set previously
A=abc
b=
Echo ${a:-c} show ABC
Echo ${b:-c} display C
Echo ${a:+c} display C
Echo ${b:+c} is displayed as empty
Echo ${a:=c} displayed as ABC
Echo ${b:=c} is displayed as C and assigned to B
The Echo $b is displayed as C, i.e. the above assignment takes effect
Unset b
Echo ${a:?c} or Echo ${a:?} Show ABC
#echo ${b:?} Undefined displays the following error message by default
-BASH:B: Parameter null or not set
#echo ${b:?this is error} errors are displayed as custom statements or values
-BASH:B: This is error
#echo ${b:?100}
-BASH:B: 100
Use a separate variable file for all scripts, unifying all variables, similar to independent function files
echo "Name=user1" > var//Put all the variables in file Var, and later scripts call this variable file to
VI a.sh
#!/bin/bash
#a. Sh
. Var
Echo Your name is $name
Execution./a.sh
The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for their functionality
Cmd=whoami
# echo $cmd
WhoAmI
# echo ' $cmd '
# $cmd
# eval $cmd
Echo ' WhoAmI '
Eval WhoAmI
Echo ' ll '
Eval ll//Reserve Source command properties, display format, etc.
n=10
echo {1: $n}//cannot display sequence correctly
Eval echo {1: $n}//Normal display number sequence 1-10
Indirect variable Reference
If the value of the first variable is the name of the second variable, referring to the value of the second variable from the first variable is called an indirect variable reference
Two different ways: eval var1=\$ $var 2 or Var1=${!var2}
A=b
B=c
Eval echo \$ $a//display as C
echo ${!a}//Display as C
This article is from the "Rackie" blog, make sure to keep this source http://rackie386.blog.51cto.com/11279229/1945757
Array of advanced shell programming