如果命令 returns 在 heredoc (ssh ... <<EOF) 中 运行 时为空字符串则退出
Exiting if a command returns an empty string when run in a heredoc (ssh ... <<EOF)
如果命令 returns 在 EOF
中没有输出,我希望我的命令以失败状态 (1) 退出。但是我无法使用变量来存储命令或将输出存储在文件中。
例如:
#!/bin/bash
a='Some other commands in local machine'
ssh ubuntu@xx.xx.xx.xx << EOF
echo $a;
ldt=$(date +'%Y%m%d')
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-1000 min')" - F'[[:space:]]*[|][[:space:]]*' '
($4>=start) && /INFO: Server startup in/
' /some/file/path-$ldt.log
EOF
现在,如果命令给出空输出,它应该 exit 1
,如果显示一些文本,它应该 exit 0
。
我无法将 awk
命令存储在变量中。如果我将它存储在变量中,awk
命令将不起作用。尝试将输出存储在文件中,但这也失败了。
请帮我想办法。
当您使用未加引号的 heredoc 时——<<EOF
——其中的扩展是 运行,然后它被输入标准输入到被调用的命令。
这包括 $(awk ...)
,因此很难从以这种方式生成的代码中正确捕获输出并在以后对其进行操作。
所以 -- 您可以做的一件事是使用引用的 heredoc,然后返回到您之前尝试过的方法(捕获 awk 结果并在其上进行分支),它应该可以正常工作。
您可以做的另一件事是 awk
根据是否找到任何匹配设置自己的退出状态。
在 awk
中设置退出状态
#!/bin/bash
a='Some other commands in local machine'
printf -v args_q '%q ' "$a"
ssh ubuntu@xx.xx.xx.xx "bash -s $args_q" <<'EOF'
a=
echo "$a"
ldt=$(date +'%Y%m%d')
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-1000 min')" -F'[[:space:]]*[|][[:space:]]*' '
BEGIN { found=0 }
(>=start) && /INFO: Server startup in/ { print [=10=]; found=1; }
END { if (found == 0) { exit(1) } else { exit(0) } }
' "/some/file/path-$ldt.log"
EOF
设置退出状态之后 awk
#!/bin/bash
a='Some other commands in local machine'
printf -v args_q '%q ' "$a"
ssh ubuntu@xx.xx.xx.xx "bash -s $args_q" <<'EOF'
a=
echo "$a"
ldt=$(date +'%Y%m%d')
awk_result=$(
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-1000 min')" -F'[[:space:]]*[|][[:space:]]*' '
BEGIN { found=0 }
(>=start) && /INFO: Server startup in/ { print [=11=]; found=1; }
END { if (found == 0) { exit(1) } else { exit(0) } }
' "/some/file/path-$ldt.log"
)
[[ $awk_result ]] && echo "$awk_result"
[[ $awk_result ]] # success only if awk_result is nonempty
EOF
请注意,如果 <<EOF
已更改为 <<'EOF'
,则此 仅 有效,以便远程评估 awk 命令。
如果命令 returns 在 EOF
中没有输出,我希望我的命令以失败状态 (1) 退出。但是我无法使用变量来存储命令或将输出存储在文件中。
例如:
#!/bin/bash
a='Some other commands in local machine'
ssh ubuntu@xx.xx.xx.xx << EOF
echo $a;
ldt=$(date +'%Y%m%d')
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-1000 min')" - F'[[:space:]]*[|][[:space:]]*' '
($4>=start) && /INFO: Server startup in/
' /some/file/path-$ldt.log
EOF
现在,如果命令给出空输出,它应该 exit 1
,如果显示一些文本,它应该 exit 0
。
我无法将 awk
命令存储在变量中。如果我将它存储在变量中,awk
命令将不起作用。尝试将输出存储在文件中,但这也失败了。
请帮我想办法。
当您使用未加引号的 heredoc 时——<<EOF
——其中的扩展是 运行,然后它被输入标准输入到被调用的命令。
这包括 $(awk ...)
,因此很难从以这种方式生成的代码中正确捕获输出并在以后对其进行操作。
所以 -- 您可以做的一件事是使用引用的 heredoc,然后返回到您之前尝试过的方法(捕获 awk 结果并在其上进行分支),它应该可以正常工作。
您可以做的另一件事是 awk
根据是否找到任何匹配设置自己的退出状态。
在 awk
中设置退出状态
#!/bin/bash
a='Some other commands in local machine'
printf -v args_q '%q ' "$a"
ssh ubuntu@xx.xx.xx.xx "bash -s $args_q" <<'EOF'
a=
echo "$a"
ldt=$(date +'%Y%m%d')
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-1000 min')" -F'[[:space:]]*[|][[:space:]]*' '
BEGIN { found=0 }
(>=start) && /INFO: Server startup in/ { print [=10=]; found=1; }
END { if (found == 0) { exit(1) } else { exit(0) } }
' "/some/file/path-$ldt.log"
EOF
设置退出状态之后 awk
#!/bin/bash
a='Some other commands in local machine'
printf -v args_q '%q ' "$a"
ssh ubuntu@xx.xx.xx.xx "bash -s $args_q" <<'EOF'
a=
echo "$a"
ldt=$(date +'%Y%m%d')
awk_result=$(
awk -v start="$(date +'%Y/%m/%d %H:%M' --date '-1000 min')" -F'[[:space:]]*[|][[:space:]]*' '
BEGIN { found=0 }
(>=start) && /INFO: Server startup in/ { print [=11=]; found=1; }
END { if (found == 0) { exit(1) } else { exit(0) } }
' "/some/file/path-$ldt.log"
)
[[ $awk_result ]] && echo "$awk_result"
[[ $awk_result ]] # success only if awk_result is nonempty
EOF
请注意,如果 <<EOF
已更改为 <<'EOF'
,则此 仅 有效,以便远程评估 awk 命令。