在bash中有一种方法可以理解传递给它的变量的名称?在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
有任何想法吗?
如果你做了'FUNC1 “富”'? –
'-e'测试文件是否存在,而不是变量存在(或(非)空洞)。 –