【发布时间】:2011-05-24 22:43:34
【问题描述】:
我编写了一个函数来检查作为参数传递的一个或多个变量是否为空 (got the idea from here)
下面是脚本的样子:
#!/bin/bash
is_variable_defined?() {
for VAR in "$@"; do
: "${!VAR? "ERROR: $VAR is undefined."}"
done
}
TEST='a'
is_variable_defined? TEST TEST2
这是输出:
/path/to/script.sh: line 4: !VAR: ERROR: TEST2 is undefined.
但是我想输出的是:
TEST2 is undefined
我尝试调整 : "${!VAR? "ERROR: $VAR is undefined."}" 行,但无论我做什么都会中断。
有人知道要在脚本中修改什么以获得我想要的输出吗?
【问题讨论】:
-
您是否阅读了您链接到的问题的答案? 打算打破这条信息。据我所知,另一个问题的答案就是答案。
-
因此我的问题是,我该如何更改消息?
-
你不能,它是一个内置函数。我可以从另一个问题中引用lhunath answer:
If you just want to test whether foo is empty, instead of aborting the script, use this instead of a function: [[ $foo ]]。
标签: bash