I need help with a Basic script (please)

scentsy
Contributor

I'm trying to run a script to check first if a file is already on the mac I want it to quit, and if the file is not on the mac to go ahead and install it.

At the moment I have a policy that copy the file to /tmp/CB/file.pkg than it gets installed.
( file.pkg gets installed into "/Applications/file.pkg")
But I want it to skip install if the file.pkg is already installed on the "/Applications/file.pkg"

I told you it was basic =)

this is what I have so far:

#!/bin/sh
installer -pkg /tmp/CB/file.pkg -target /
1 ACCEPTED SOLUTION

lkrasno
Contributor II

Many examples available online for "bash if" (and other conditional statements)

#!/bin/bash

pkgDir="/tmp/CB/file.pkg"

if [ -d $pkgDir ]; then 
    do this 
else
    do that 
fi

View solution in original post

7 REPLIES 7

lkrasno
Contributor II

Many examples available online for "bash if" (and other conditional statements)

#!/bin/bash

pkgDir="/tmp/CB/file.pkg"

if [ -d $pkgDir ]; then 
    do this 
else
    do that 
fi

scentsy
Contributor

that was fast! thank you.
one last question I want it to exit if the file is on the /tmp/CD/file.pkg and install if its not there....does the "exit" looks correct? (do I need a "exit 0" or "exit 1")?

thank you very much!

#!/bin/bash

pkgDir="/tmp/CB/file.pkg"

if [ -d $pkgDir ]; then 
    exit
else
   installer -pkg /tmp/CB/file.pkg -target /
fi

lkrasno
Contributor II

You're on the right track, and yes, ideally you'll want to use the appropriate exit code

scentsy
Contributor

got it thank you again!

donmontalvo
Esteemed Contributor III

Would probably use -e unless that's a non-flat PKG.

--
https://donmontalvo.com

mschroder
Valued Contributor

A few comments:

  • Good idea to save resources and not do the transfer and install if not needed.

  • Instead of running the script again and again, just to find out the package is already there you could make a smart group that contains the devices that don't have the package installed, and run it only on these.

  • Non-zero exit codes usually indicate that an error occurred. It is useful to stick to that convention and don't use non-zero exit codes to signal something else.

  • A package in /Applications? Sounds weird to me.

Cheers,

Matthias

scentsy
Contributor

@mschroder thank you for your comments =)

yes you are right a package in Applications doesn't make sense =) I didn't explain it correctly, so the package gets download it to /tmp/CD/file.pkg than the scripts run the installer and what it actually happens the file.pkg installs the application into /Applications =)

I will also try your suggestion on using a smart group.

Thank you very much!