Ten minutes Easy Learning Series: 2020-3-19_SHELL learn advanced variable _

This chapter is for advanced usage of the previous chapter on the application of variable

  • Variable tamper-proof - readonly

Requirements: I think he is the world's most handsome man, do not accept any rebuttal, but the code problem as follows:

#!/bin/bash
THEMOSTHANDSOMEMAN="璐璐桑"
THEMOSTHANDSOMEMAN="庞统"
echo "墨镜墨镜请告诉我谁是这个世界上最帅的人:$THEMOSTHANDSOMEMAN"

The results overturned. . .
The results overturned
Therefore, in order to prevent malicious or unintentional tampering with someone behind my variables, I should be how to ensure that they are the world's most handsome man?

#!/bin/bash
readonly THEMOSTHANDSOMEMAN="璐璐桑"
THEMOSTHANDSOMEMAN="庞统"
echo "墨镜墨镜请告诉我谁是这个世界上最帅的人:$THEMOSTHANDSOMEMAN"

See the figure error, error inform the most handsome quota is locked, Pang Tong finished calf, and I'm the world's most handsome man!
Here Insert Picture Description
Knock blackboard! Knock blackboard! Knock blackboard! ==> This function is quite common, especially for some of the variables may be the value of the same name.
Extended knowledge: Linux command #unset <variable name> can put this variable waste can no longer references, but read-only variable is invalid

  • Global and local variables - local

From the beginning of the story, one of the following five institutional departments, five departments have annual budget, funding agencies for each department:
Case1: Sector 1: "Boss, no money, to the point" ===> Boss: "Take ~ "
Case2: sector 1:" brothers, we have no money to the point " ===> sector 2:" roll "!
Case3: boss:" Buddy, no money, to the point " ===> sector 1:" you find department [≠ 1] to go, "
the story said, we can grilled outside, but you can not eat there!

Meanwhile a partial shell defines a global variable to see the output

#!/bin/bash
local VAR1=100
VAR1=200
echo $VAR1

200 print out a shell of global variables, local variables local to see how incorrect report, the local function can only be used in:
Here Insert Picture Description
explain this thing, I used a piece of code:

#!/bin/bash
VAR1=200
function A1() {
local VAR1=10
echo $VAR1
}
function A2() {
local VAR1=20
echo $VAR1
}
echo $VAR1 # 这个是shell全局的变量,所以肯定是200
A1         # 不用管这个什么意思,调用了函数,打印了VAR1,但是是A1函数中的VAR1,也就是10
A2         # 这个自然就是20了,学过编程一定要了解作用域

The results shown below, there is no analysis of the problem:
Here Insert Picture Description

  • Variable types and common usage

From the beginning of the story: to ~ print a backslash \ Le $$ pole again a double quote "
themselves to try it, I give you the results:

#!/bin/bash
echo "\"    # ./1.sh: line 3: unexpected EOF while looking for matching `"
echo \      # 啥都没有
echo "      # ./1.sh: line 3: unexpected EOF while looking for matching `"'
echo $$     # 39858 这数字什么鬼
echo "$$"   # 39916 这又特么什么鬼

It raises the question of how to print special characters, word:
special characters printed if necessary, special characters should echo back caused by single quotes , for example:

#!/bin/bash
echo '$$'   # $$
echo '\'    # \

The role of single quotes is to cancel the special character escape function!
Double quotes is a string boundaries, but have a special character escaping
Remember these two words, if you want to replace some of the reference variable value string must not be enclosed in single quotes, look at the following example you will understand:

#!/bin/bash
THEMOSTHANDSOMEBOY="zhaolu"
echo "${THEMOSTHANDSOMEBOY}太特么帅了!"  #这个变量边界线昨天已经讲过了 ${}
echo '${THEMOSTHANDSOMEBOY}太特么帅了!'

Look output:
Here Insert Picture Description
single quotes do not escape special characters, a direct negation of the most handsome I am not happy, you here today, finishing the string content tomorrow.

Published 49 original articles · won praise 18 · views 3991

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/104946147