在bash中调用自定义函数将导致“找不到命令". [英] Calling custom function in bash results in "Command not found"
本文介绍了在bash中调用自定义函数将导致“找不到命令".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在执行脚本时未提供任何参数的情况下显示默认的帮助文本:
I'm trying this to display a default helptext when no parameters are given when executing the script:
if [[ $@ ]]; then
do stuff
else displayHelp;
fi
displayHelp() {
echo "some helptext"
}
但是由于某些原因,在控制台上执行脚本时,它显示:
But for some reason, when executing the script on console, it says:
./myScript.sh: Line 48: displayHelp: Command not found
当我通过-h
参数调用此函数时,也会发生同样的情况
The same occurs when I call this function via -h
parameter
推荐答案
必须先定义函数,然后才能使用它们.因此,将方法放在调用之前:
Functions must be defined before they can be used. So put the method before your call it:
displayHelp() {
echo "some helptext"
}
if [[ $@ ]]; then
do stuff
else displayHelp;
fi
或将您的主代码放入另一种方法中,并在脚本末尾调用该方法:
or put your main code in another method and call this one at the end of your script:
main() {
if [[ $@ ]]; then
do stuff
else displayHelp;
fi
}
displayHelp() {
echo "some helptext"
}
main "$@"
这篇关于在bash中调用自定义函数将导致“找不到命令".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文