bash Linux shell 脚本 - 将尾值分配给变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12558100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:21:35  来源:igfitidea点击:

Linux shell scripting - assigning tail value to a variable?

linuxbashshelltail

提问by PaulG

I'm setting up a shell script that reads the last line of a log file using:

我正在设置一个 shell 脚本,它使用以下命令读取日志文件的最后一行:

tail -1 "/path/to/gdscript.log"

.. which is echo'ing the last line of the script just fine. The last line of the log dictates whether the process succeeded or failed, so I'm trying to run a quick echo as follows (this is the bit I'm failing at):

.. 这是在脚本的最后一行呼应就好了。日志的最后一行指示该过程是成功还是失败,因此我尝试按如下方式运行快速回显(这是我失败的地方):

if [ (tail -1 "/path/to/gdscript.log")  == "Process Complete" ]; then
echo "Data Transfer OK"
else
echo "Data Transfer Failed"
exit 1
fi

.. but using the script above I'm getting:

.. 但使用上面的脚本我得到:

./gdscript.sh: line 14: syntax error near unexpected token `tail'

Can someone in the know show me how to format the IF gate above so that I can work off the last line of the log file? I'm new to shell scripting and would really appreciate the help.

知道的人可以告诉我如何格式化上面的 IF 门,以便我可以处理日志文件的最后一行吗?我是 shell 脚本的新手,非常感谢您的帮助。

Thanks, Paul G

谢谢,保罗 G

回答by Sodved

To get the output of a command you need $(cmd...). So I think you mean:

要获得命令的输出,您需要$(cmd...). 所以我认为你的意思是:

if [ "$(tail -1 '/path/to/gdscript.log')"  == "Process Complete" ]; then
...

回答by zjhui

you should use:
if [ $(tail -1 /path/to/gdscript.log) == "Process Complete" ]; then

你应该使用:
if [ $(tail -1 /path/to/gdscript.log) == "Process Complete" ]; then