2

N years ago, I learned Brother Bird's linux private kitchen on a whim. As a result, after many years, I lost all my skills. It turns out that after entering the job, the most efficient way to learn is to learn around the problems faced. Of course, it does not mean that there is no need to do technical reserves, and it is necessary to broaden the technical scope, but to systematically learn a technology, it is best to use it to solve the current problem.
I recently started to contact ci and found that the shell scripts in it can be understood, but it is difficult to optimize and transform. Need to re-combine the shell script.

shell variable

variable declaration

 key="value"
  • Unlike many other languages, there can be no spaces around the equal sign.
  • Variable names can only use English letters, numbers and underscores, and the first character cannot start with a number.

variable usage

When using a variable that has already been defined, add the $ symbol before the variable name, and use {} to include the variable name.

 name="George"

# $变量名
echo "my name is $name"

prefix="foot"

# ${变量名}
echo "i like play ${prefix}ball"

The addition {} is to help the interpreter recognize the bounds of the variable. As a best practice, add ${} to all variables.

string variable

The most common data type for shell variables is a string. The other is an array, which will be introduced later.
When assigning a string to a variable, you can use double quotation marks, single quotation marks, or no quotation marks.

You can assign values to variables using other variable values

 name=George
alias=${name}

When a variable value needs to contain spaces, it needs to be enclosed in quotation marks (both single and double quotation marks are supported). Without parentheses, shell execution will report an error.

 t1="this is a nice day"
t2='yes,it is'

# 以下shell执行时会报错 info: command not found。因为shell会把空格后的info当成指令执行
t3=error info

The shell supports concatenation when defining string variables, as long as there are no spaces between each string value.

 t1="hi there"

# 虽然shell能正常运行以下代码,但可读性太差。
t2=${t1},this" is mike"

# 以下代码具备更强的可读性。
t2="${t1},this is mike"

You can use ${} in double quotes to refer to the specified variable. In single quotes, any character is output as-is.

 t1="hi there"
t2="${t1},this is mike"
t3='${t1},this is mike'

The actual value of t2 above is "hi there, this is mike", and the value of t3 is "${t1}, this is mike".
In double quotes, the same function as single quotes can be achieved by translating the character \

 t1="hi there"
t2="\${t1},this is mike"

It is recommended to use double quotation marks uniformly.

get string length

 name="George"

# 使用`${#变量名}`获取变量属性值的长度
echo ${#name}

String truncation

 t1="it's a good day"

# 使用${变量名:from:length}获取从from开始,长度为length的字符串
# 以下实例从字符串第 7 个字符开始截取 4 个字符,结果就是"good"
echo ${t1:7:4}

array variable

Arrays are represented by parentheses, and array elements are separated by a "space" symbol or a "carriage return". The individual components of the array can also be defined individually.

 array1=("value0" "value1" "value2" "value3")

array2=(
"v0"
"v1"
"v2"
)

array2[3]="v3"
array2[4]="v4"

read array

 # ${数组名[下标]}
echo ${array1[0]}

# 使用 [@] 或 [*] 符号可以获取数组中的所有元素
echo ${array1[@]}
echo ${array2[*]}

the length of the array

The method to get the length of an array is the same as the method to get the length of a string

 array=(0 1 2 3 4)

# 使用`${#数组变量名[@]}`获取数组长度
echo ${#array[@]}

one-dimensional array

The shell only supports one-dimensional arrays (multi-dimensional arrays are not supported)

 arr1=(1 2 3 4)
arr2=(5 6 7 8)

# 以下代码并不是构建一个二维数组,而是将两个数组打平后拼接在一起。
arr=(${arr1[@]} ${arr2[@]})

# arr实际值为 1 2 3 4 5 6 7 8
echo ${arr[@]}

# arr长度为 8
echo ${#arr[@]}

read-only variable

 name="George"
readonly name
name="Carol"
echo ${name}

Through readonly, the specified variable can be set as read-only. When the above shell executes to the third line, it will prompt an error: err.sh: line 3: name: readonly variable.

delete field

 unset var_name

After a variable is deleted, it cannot be used again. The unset command cannot delete read-only variables.

References
https://www.runoob.com/linux/linux-shell-variable.html


梦梦她爹
1.8k 声望122 粉丝