我可能有一些菜鸟问题
如何在 bash 中的 if 语句中调用定义的函数?
看看我的例子:
#!/bin/bash -x
Variable_A=xyz
function_x() {
echo "hello everyone"
}
#here I want to define a if statement after the function
if Variable_A == working;then
function_x()
#here I have a while for the parameters in bash example:
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--status)
Variable_A="$2"
shift
shift
;;
esac
done
所以这个想法是当我运行脚本时,让我们称它为 test.sh 它在 bash 中运行脚本 exp:
./test.sh working
我不知道如何创建这样的东西,也许你们中的一些人可以帮助我解决这个问题。
提前谢谢你
您的if
应如下所示:
#here I want to define a if statement after the function
if [ "$Variable_A" = "working" ]
then
function_x
fi
您需要以fi
结束每个 if 语句,并且在没有()
的情况下调用函数。
https://www.thegeekdiary.com/bash-if-loop-examples-if-then-fi-if-then-elif-fi-if-then-else-fi/
关于if
声明的另一个重要说明。 您需要在[
或]
之前和之后有一个空格。
您的if
逻辑也应该在读取脚本 parms 并设置Variable_A
之后。