Search code examples
bashmacosshellshplist

fallback command via || throws "No such file or directory"


I'm trying to execute an update or insert with PlistBuddy on my Mac.

Script

I've written the following script:

#!/bin/sh
set -e
PLIST_LOCATION="Info.plist"
ENVIRONMENT="DEV"
/usr/libexec/PlistBuddy $PLIST_LOCATION -c "Set :MyEnvironment string $ENVIRONMENT" || \
/usr/libexec/PlistBuddy $PLIST_LOCATION -c "Add :MyEnvironment string $ENVIRONMENT"

Test

I wanted to ensure, that my script fails if the second command fails, so I changed it just for test to:

/usr/libexec/PlistBuddy $PLIST_LOCATION -c "Set :TKEnvironment string $ENVIRONMENT" || \
/usr/libexec/PlistBuddy $PLIST_LOCATION -c "Delete :TKEnvironment string $ENVIRONMENT"

But instead of

Set: Entry, ":MyEnvironment", Does Not Exist
Delete: Entry, ":MyEnvironment", Does Not Exist

the command fails with

Set: Entry, ":MyEnvironment", Does Not Exist
./env.sh: line 6:  /usr/libexec/PlistBuddy: No such file or directory

Question

Why does it execute the first command, but then fails, because it can't find the command?


Solution

  • If you look at the error line

    ./env.sh: line 6:  /usr/libexec/PlistBuddy: No such file or directory
    

    you'll see there is an extra space between the colon and the first / of the command's path. This shows that the shell is picking up a stray character from somewhere, and the shell is trying to run a command with a space (or some other whitespace character) on the front.

    If you were running Windows, I'd suspect a carriage-return character had crept into into the file and was interacting badly with the \ line continuation. Under macOS, I'd guess that the usual Unix linefeed has been replaced with an old-macOS carriage return. I'm sure some helpful editor will jump in at this point...

    Anyway, I'd suggest removing the \ and trying one long line. If that works, it suggests line-ending problems.

    Also, check your editor to ensure it is using sensible line endings.