3

I have an issue with running cat in shell script on a log file which is in ~/bin/rclone_sync_ACD.log. This is the line in the shell script:

RESULT=cat $LOGFILE | tail -1

But when running the script I get:

./rclone_sync: line 63: /Users/pjburnhill/bin/rclone_sync_ACD.log: Permission denied

In terminal, if I type cat $LOGFILE | tail -1, it gives the right output.

What permissions would the script need to have to access and print out the correct line?

Thanks, PJ

PJ Palomaki
  • 99
  • 4
  • 10
  • 1
    `RESULT=cat $LOGFILE | tail -1` runs `$LOGFILE` as a command (and it's probably not executable, hence the error). You probably want `RESULT=$(cat $LOGFILE | tail -1)`, or [simply](https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat) `RESULT=$(tail -1 "$LOGFILE")`. This is really basic stuff: You might want to read a shell scripting tutorial. – Biffen Oct 11 '16 at 17:07
  • 3
    Try [shellcheck](http://shellcheck.net). It automatically detects [your problem](https://github.com/koalaman/shellcheck/wiki/SC2037). – that other guy Oct 11 '16 at 17:30

1 Answers1

8

To assign the output of a command to a variable, wrap the command in backticks or $().

RESULT=$(cat $LOGFILE | tail -1)

Your command performed the environment variable assignment RESULT=cat, and then executed the command $LOGFILE | tail -1 in that environment. Since $LOGFILE is not an executable file, you got an error.

Barmar
  • 741,623
  • 53
  • 500
  • 612