Search code examples
bashmacosshellterminalpermission-denied

Permission denied on cat via shell script


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


Solution

  • 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.