Skip to main content
Solved

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

  • April 4, 2022
  • 5 replies
  • 22 views

Forum|alt.badge.img+3
  • New Contributor
  • 4 replies

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 ;

Best answer by talkingmoose

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

5 replies

Forum|alt.badge.img+13
  • Honored Contributor
  • 365 replies
  • April 4, 2022

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


sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3567 replies
  • April 4, 2022

@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
Forum|alt.badge.img+8
  • Valued Contributor
  • 64 replies
  • April 4, 2022

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

 

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
Forum|alt.badge.img+36
  • Community Manager
  • 1913 replies
  • Answer
  • April 4, 2022

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 4 replies
  • April 4, 2022

Thank you all for your helpful contributions, checking now