I am trying to just put bash commands into a variable and later run with exit code function.
我试图将bash命令放入一个变量中,然后使用exit代码函数运行。
command1="$(awk -v previous_date=$reply -f $SCRIPT_HOME/tes.awk <(gzip -dc $logDir/*) > $OUTFILE)" # Step 1
check_exit $command1 # Step2
Here it runs the command in step 1 and always returns 0 exit code.
在这里,它运行步骤1中的命令,并且总是返回0退出代码。
How Can I put commands in a variable and later run with exit function.
如何将命令放在变量中,然后使用exit函数运行。
2 个解决方案
#1
1
Try using a function:
试着用一个函数:
do_stuff() { echo "$var"; echo that; }
You can later call this function and check the error code:
您可以稍后调用此函数并检查错误代码:
$ var="this"
$ do_stuff
this
that
$ echo $?
0
The variables defined in the global scope of your script will be visible to the function.
在脚本的全局范围中定义的变量将对函数可见。
So for your example:
所以对于你的例子:
command1() { awk -v previous_date="$reply" -f "$SCRIPT_HOME/tes.awk" <(gzip -dc "$logDir/*") > "$OUTFILE"); }
check_exit command1
By the way, I put some quotes around the variables in your function, as it's typically a good idea in case their values contain spaces.
顺便说一下,我在函数中添加了一些引号,因为这通常是一个好主意,以防它们的值包含空格。
#2
2
You can declare your function this way:
你可以这样声明你的功能:
function command1 {
awk -v "previous_date=$reply" -f "$SCRIPT_HOME/tes.awk" <(gzip -dc "$logDir"/*) > "$OUTFILE"
}
Or the older form:
或者年长的形式:
command1() {
awk -v "previous_date=$reply" -f "$SCRIPT_HOME/tes.awk" <(gzip -dc "$logDir"/*) > "$OUTFILE"
}
Take attention on placing variables inside double quotes to prevent word splitting and pathname expansion.
注意在双引号内放置变量,以防止分词和路径名扩展。
Some POSIXists would prefer that you use the old version for compatibility but if you're just running your code with bash, everything can just be a preference.
一些POSIXists希望您使用旧版本来实现兼容性,但是如果您只是使用bash运行代码,那么一切都可以是首选项。
And you can also have another function like checkexit
which would check the exit code of the command:
还可以有另一个函数,例如checkexit,它会检查命令的退出代码:
function checkexit {
"$@"
echo "Command returned $?"
}
Example run:
示例运行:
checkexit command1
#1
1
Try using a function:
试着用一个函数:
do_stuff() { echo "$var"; echo that; }
You can later call this function and check the error code:
您可以稍后调用此函数并检查错误代码:
$ var="this"
$ do_stuff
this
that
$ echo $?
0
The variables defined in the global scope of your script will be visible to the function.
在脚本的全局范围中定义的变量将对函数可见。
So for your example:
所以对于你的例子:
command1() { awk -v previous_date="$reply" -f "$SCRIPT_HOME/tes.awk" <(gzip -dc "$logDir/*") > "$OUTFILE"); }
check_exit command1
By the way, I put some quotes around the variables in your function, as it's typically a good idea in case their values contain spaces.
顺便说一下,我在函数中添加了一些引号,因为这通常是一个好主意,以防它们的值包含空格。
#2
2
You can declare your function this way:
你可以这样声明你的功能:
function command1 {
awk -v "previous_date=$reply" -f "$SCRIPT_HOME/tes.awk" <(gzip -dc "$logDir"/*) > "$OUTFILE"
}
Or the older form:
或者年长的形式:
command1() {
awk -v "previous_date=$reply" -f "$SCRIPT_HOME/tes.awk" <(gzip -dc "$logDir"/*) > "$OUTFILE"
}
Take attention on placing variables inside double quotes to prevent word splitting and pathname expansion.
注意在双引号内放置变量,以防止分词和路径名扩展。
Some POSIXists would prefer that you use the old version for compatibility but if you're just running your code with bash, everything can just be a preference.
一些POSIXists希望您使用旧版本来实现兼容性,但是如果您只是使用bash运行代码,那么一切都可以是首选项。
And you can also have another function like checkexit
which would check the exit code of the command:
还可以有另一个函数,例如checkexit,它会检查命令的退出代码:
function checkexit {
"$@"
echo "Command returned $?"
}
Example run:
示例运行:
checkexit command1