我今天才刚开始使用bash编程,有一件事我不知道...这是我写的脚本:
#!/bin/bash
FILE=test.txt
test_file () {
if [ -e "$FILE" ]; then
echo "$FILE exists"
else
echo "$FILE does not exist"
return 1
fi
return
}
$(test_file)
当我运行脚本(命名为test)时,它会输出以下错误消息:
"./test: line 16: test.txt: command not found"
令我惊讶的是,我对“功能部件”的建议超过了,并用exit代替return,如下所示:
#!/bin/bash
FILE=test.txt
#test_file () {
if [ -e "$FILE" ]; then
echo "$FILE exists"
else
echo "$FILE does not exist"
exit 1
fi
exit
#}
#$(test_file)
它运行没有任何抱怨。即使我将带有函数调用的函数粘贴/粘贴到其他函数中的另一个脚本中(也可以正常工作),它也可以工作。
我想念什么?
您试图将函数的输出作为命令运行。只需放下$(...)
。
#!/bin/bash
FILE=test.txt
test_file () {
if [ -e "$FILE" ]; then
echo "$FILE exists"
else
echo "$FILE does not exist"
return 1
fi
return
}
test_file
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句