The initial value of the string processing

Creative Commons License Creative Commons

Problems
in this case required to write a script sumx.sh , and from seeking, requirements 1-x as follows:
the x value is read from the keyboard
when the user does not enter any values, a calculation according to the default
program
steps
implemented This case requires the following steps get on.

Step one: know the most common treatment method for the initial value of the string

1) values only, $ {var: -word}
If the variable var already exists and not Null, then the $ var; otherwise, returns the string "word", the value of the original variable var unaffected.
Circumstances existing variable values:

[root@svr5 ~]# echo $SCHOOL  			//查看原变量值
Tarena IT Group.
[root@svr5 ~]# echo ${SCHOOL:-Tarena}  	//因SCHOOL已存在,输出变量SCHOOL的值
Tarena IT Group.
[root@svr5 ~]# echo $SCHOOL  			//原变量的值并不改变
Tarena IT Group.

Variable Value absence:

[root@svr5 ~]# unset SCHOOL  			//清除SCHOOL变量
[root@svr5 ~]# echo ${SCHOOL:-Tarena}  	//因SCHOOL已不存在,输出“Tarena”
Tarena
[root@svr5 ~]# echo $SCHOOL  			//变量SCHOOL仍然不存在

[root@svr5 ~]#

2) + value assignment, $ {var: = word}
If the variable var already exists and is non-Null, the $ var is returned, the old variable values unchanged; otherwise, the string "word", and this string assignment to the variable var.
Variable Value absence:

[root@svr5 ~]# echo $SCHOOL  			//前面已将此变量清除

[root@svr5 ~]# echo ${SCHOOL:=Tarena}  	//因SCHOOL不存在,输出“Tarena”
Tarena
[root@svr5 ~]# echo $SCHOOL  			//且将“Tarena”赋值给变量SCHOOL
Tarena

Circumstances existing variable values:

[root@svr5 ~]# echo $SCHOOL  			//确认当前的变量值
Tarena
[root@svr5 ~]# echo ${SCHOOL:=Tarena IT Group.}  //变量已存在,输出其值
Tarena
[root@svr5 ~]# echo $SCHOOL  			//原变量的值也不受影响
Tarena

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91946299