没有太多可以说的,直接列脚本:
[root@oracle-cluster-3 shell]# cat var_gen.sh var_list="a b c" var_init_value=1 for var_item in $var_list do eval loop_var_name="$var_item" eval $loop_var_name=$var_init_value eval echo "current variable is: '$'$loop_var_name, value is: \$$loop_var_name" if [ `eval echo '$'$loop_var_name` -eq 2 ] then echo "item is 2" fi let var_init_value++ echo "" done [root@oracle-cluster-3 shell]# [root@oracle-cluster-3 shell]# sh var_gen.sh current variable is: $a, value is: 1 current variable is: $b, value is: 2 item is 2 current variable is: $c, value is: 3 [root@oracle-cluster-3 shell]#
另一个脚本:
[root@oracle-cluster-3 shell]# cat check_var.sh eval func_var_name='$1' eval func_var_purpose_value='$2' eval echo "current variable is: '$'$func_var_name, value is: \$$func_var_name" echo "func_var_purpose_value=$func_var_purpose_value" if [ "`eval echo '$'$func_var_name`" == "" ] then eval func_var_name="func_var_purpose_value" eval echo "current variable is: $func_var_name, value is: \$$func_var_name" fi [root@oracle-cluster-3 shell]# [root@oracle-cluster-3 shell]# sh check_var.sh i_am_variable "hello world" current variable is: $i_am_variable, value is: func_var_purpose_value=hello world current variable is: func_var_purpose_value, value is: hello world [root@oracle-cluster-3 shell]#
另一个脚本:
[root@oracle-cluster-3 shell]# cat check_func_variable.sh a=56 function variable_if_null_then() { eval loop_var_name="$1" #eval $loop_var_name="$2" eval echo "current variable is: '$'$loop_var_name, value is: \$$loop_var_name" if [ `eval echo '$'$loop_var_name` -ne "$2" ] then eval $loop_var_name="$2" echo "-----> after change" eval echo "current variable is: '$'$loop_var_name, value is: \$$loop_var_name" fi } variable_if_null_then a 70 echo "-------" echo "variable [a] is: $a" [root@oracle-cluster-3 shell]# [root@oracle-cluster-3 shell]# sh check_func_variable.sh current variable is: $a, value is: 56 -----> after change current variable is: $a, value is: 70 ------- variable [a] is: 70 [root@oracle-cluster-3 shell]#
——————————
Done。