Files and processes payload - using if condition in "execute command"

OMP
New Contributor II

Hello everyone

I'm using execute command option in the Files and processes payload of a policy and want to run a small script ( 5 lines), I'm using a simple if statement to check whether a folder exists or not and create it, I'm getting a syntax error on the test client when I run it from Jamf however I tested and it works fine if I run it as a script from terminal so this issue is possibly the way I'm using semicolon, this could be done using script payload but I need to make it work this way.

This is working as a script 
if [ ! -d "Path/to/location" ]
then
mkdir Path/to/location
Sleep 2
touch Path/to/location/hello.plist
else
touch Path/to/location/hello.plist
fi

 

This is how I added it to files and processes payload

if [ ! -d "Path/to/location/" ] ; then ; mkdir Path/to/location/ ; sleep 2 ; touch /Path/to/location/hello.plist ; else ; touch Path/to/location/hello.plist ;

1 ACCEPTED SOLUTION

talkingmoose
Moderator
Moderator

The mkdir command has an option to create directories if they don't exist.

Try this instead of using an if statement: mkdir -p Path/to/location

View solution in original post

5 REPLIES 5

andrew_nicholas
Valued Contributor

In my opinion, anything longer than one or maybe two lines should be done as a dedicated script. 

sdagley
Esteemed Contributor II

@OMP  I'm curious, why won't running this via a Script payload work? I am in the same camp as @andrew_nicholas when it comes to dedicated scripts as the maintainability of multi line commands in a Files and Processes payload is non-optimal (you can read that as it sucks).

brockwalters
Contributor II

You are missing the closing fi. Also watch out for missing leading slashes in your paths... 🙂

Screen Shot 2022-04-04 at 11.53.05 AM.png

 

if ! [ -d "Path/to/location/" ]; then; mkdir Path/to/location/; sleep 2; touch /Path/to/location/hello.plist; else; touch Path/to/location/hello.plist; fi

 

 

 

talkingmoose
Moderator
Moderator

The mkdir command has an option to create directories if they don't exist.

Try this instead of using an if statement: mkdir -p Path/to/location

OMP
New Contributor II

Thank you all for your helpful contributions, checking now