Stateful Scripts in Automator

automator

Is there a way to run stateful bash scripts in automator?

I'm trying to run something depending if a file does or does not exist. With bash script alone in terminal the code below works and will show the correct output, depending on wether the file exists or not.

Only one line of output per run of the script is displayed either way.

FILE="/Volumes/Canvio1TB/EyeTV Archive/sync.ffs_lock"
if test -f "$FILE"; then
    echo "$FILE exists"
else
  echo "$FILE does not exist"
fi

When I run this in automator as a "run shell script" action the output is:

/Volumes/Canvio1TB/EyeTV Archive/sync.ffs_lock exists

/Volumes/Canvio1TB/EyeTV Archive/sync.ffs_lock does not exist

Regardless of wether the file exists or not, the script generates both lines of output.

 

If there's a better approach I'd be open to using it – I haven't had success with the syntax of applescript running a command based on the (non)existence of a full path & file.

Best Answer

The following example works for me in a Run Shell Script action in Automator:

f="/Volumes/Canvio1TB/EyeTV Archive/sync.ffs_lock"

if [ -f "$f" ]; then
    echo "$f exists."
else
    echo "$f does not exist."
fi

Note: Shell is set to: /bin/bash

This also works:

f="/Volumes/Canvio1TB/EyeTV Archive/sync.ffs_lock"

[ -f "$f" ] && echo "$f exists." || echo "$f does not exist."

Here it is in AppleScript in a Run AppleScript action:

set f to "/Volumes/Canvio1TB/EyeTV Archive/sync.ffs_lock"

tell application "System Events"
    if exists file f then
        return f & " file exists." as string
    else
        return f & " file does not exist." as string
    end if
end tell