简体   繁体   中英

Linux shell scripting - assigning tail value to a variable?

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

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.

Thanks, Paul G

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM