bash 中有没有办法理解传递给它的变量的名称?
例子:
var1=file1.txt
err_var=errorfile.txt
function func1 {
echo "func1: name of the variable is: " $1
echo "func1: value of variable is: " $1
echo
if [ ! -e $var1 ]
then
$1 = $err_val #is this even possible?
fi
func2 $1
}
function func2 {
echo "func2: name of the variable is: " $1
echo "func2: value of variable is: " $1
echo
}
func1 $var1
func1 $err_var
如果 file1.txt 存在,我希望得到以下输出:
func1: name of the variable is: var1
func1: value of variable is: file1.txt
func2: name of the variable is: var1
func2: value of variable is: file1.txt
当 file1.txt 不存在时:
func1: name of the variable is: var1
func1: value of variable is: file1.txt
func2: name of the variable is: err_var
func2: value of variable is: errorfile.txt
有任何想法吗?