Search code examples
bashshellfindautomator

Bash conditional with shell variable


I want to check if a file exists with [ -f "$1" ] but it's not working. The command is working with plain text like [ -f "filename.xml" ].

I echoed $1 which is for example filename.xml. Any ideas?

sourcePath=/SPECIFICPATH/${1};
echo $sourcePath;
echo $1;

find /EXAMPLEPATH -name pages -type d -execdir bash -c 'cd pages && [ -f "$1" ] && pwd && cp $sourcePath .' \;

I'm working in automator using a shell script block.


Solution

  • You’re invoking an entirely new shell with bash -c …, so you need to pass $1 along. Same with $sourcePath, if it’s not exported.

    find /EXAMPLEPATH -name pages -type d -execdir bash -c 'cd pages && [ -f "$1" ] && pwd && cp "$2" .' bash "$1" "$sourcePath" \;
    

    (In bash -c … bash "$1" "$sourcePath", the second bash is $0.)