Help with script

tomhastings
Contributor II

I am trying to run a script to remove an app from our Macs. When I run it, the killall lines work and the rm -r lines do not. The error I get is, "No such file or directory". If I run any of the commands alone in Terminal, they all work. Any ideas what I am doing wrong?
Here is the script:

!/bin/sh

sudo killall EquitracLoginController
sudo killall EQLoginController
sudo rm -r /Applications/Utilities/EquitracPrinterUtilityX.app
sudo rm -r /Library/Printers/Equitrac/
sudo rm -r /Library/Application Support/Equitrac/

10 REPLIES 10

JayDuff
Contributor II

Without being on the computer, it's hard to troubleshoot. But, if you're running the script from the JSS, you don't need sudo, as everything runs as root. It may be breaking because it's expecting you to authenticate.

If you're installing Equitrac as a package, from Casper Imaging or Self Service, you could index the package, then check the Allow package to be uninstalled box. That might be a more complete deletion.

tomhastings
Contributor II

At this point, I am just testing the script in the Terminal on a machine that has items listed in the directories targeted in lines 3-5.
Are you suggesting that I remove sudo and attempt running the script from the JSS?

JayDuff
Contributor II

Either that, or get rid of the sudos in the script, and use sudo to run the script itself.

As quoted from askubuntu:

It is rarely a good idea to have sudo inside scripts. Instead, remove the sudo from the script and run the script itself with sudo:

sudo myscript.sh

That way, all commands within the script will be run with root privileges and you only need to give the password once when launching the script.

Bonus tidbit: If you need a particular command within the script to be run without sudo privileges, you can run it as a regular user:

sudo -u username command

JayDuff
Contributor II

So, your script should be:

#!/bin/sh
killall EquitracLoginController
killall EQLoginController
rm -r /Applications/Utilities/EquitracPrinterUtilityX.app
rm -r /Library/Printers/Equitrac/
rm -r /Library/Application Support/Equitrac/
exit 0

Then just run it with:

sudo sh ./dieEquitracDie.sh

tomhastings
Contributor II

Thanks. I tried your script version and I still get the "No such file or directory" error.

geoffrepoli
Contributor

The error is originating here:

sudo rm -r /Library/Application Support/Equitrac/

Ref: Word Splitting

The interpreter is reading that line as sudo rm -r /Library/Application.

Assuming you've followed JayDuff's instructions re: sudo, you'll want to change the above line to either:

rm -r /Library/Application Support/Equitrac

or

rm -r "/Library/Application Support/Equitrac"

JayDuff
Contributor II

Duh. Good spot, @grepoli !

tomhastings
Contributor II

Thanks @grepoli!
Using the same suggestion, I am unable to execute this line:

rm -r /Applications/Utilities/EquitracPrinterUtilityX.app

How should I handle that line?

geoffrepoli
Contributor

@tomhastings

Assuming you're running this script multiple times on the same computer, that file was already deleted the last time you ran the script during testing. Without the force (-f) option, the rm command fails when it encounters a nonexistent file/directory. Add that option, and for peace of mind, add the verbose (-v) option as well.

Thus:

#!/usr/bin/env bash

# Kill processes if running
killall EquitracLoginController EQLoginController

# Remove the app and installed components
rm -rfv "/Applications/Utilities/EquitracPrinterUtilityX.app"
rm -rfv "/Library/Printers/Equitrac"
rm -rfv "/Library/Application Support/Equitrac"

sudo chmod +x /path/to/script to make it executable, then run with sudo /path/to/script

tomhastings
Contributor II

Thanks for all the help! Everything is working.