Tom Smith
Tom Smith

Reputation: 3

Mac Deleting Multiple Files Bash

I'm trying to uninstall a program by deleting all of the files the installer installed. This is the script I have tried, but it returns a "Too many arguments" error on line 6 (highlighted with **) when I try and run it.

This is to be deployed out to multiple machine through Apple Remote Desktop.

I would like to put it in a package to run, but as an executable script will also do the job. Am I going about this wrong? This is not the entire script but it follows the same pattern.

#!/bin/bash

## This will uninstall ETC Nomad v2.3.3.9.0.10.mpkg
## From Contents of ETCnomad Eos Mac 2.3.3.9.0.10.pkg

**if [ -d /Applications/Eos Family Welcome Screen.app ]; then**
/bin/rm -rf /Applications/Eos Family Welcome Screen.app
fi

if [ -f /tmp/Element_Hotkeys.pdf ]; then
/bin/rm -rf /tmp/Element_Hotkeys.pdf
fi

if [ -f /tmp/Eos_Hotkeys.pdf ]; then
/bin/rm -rf /tmp/Eos_Hotkeys.pdf
fi

if [ -f /tmp/FixtureReleaseNotes.pdf ]; then
/bin/rm -rf /tmp/FixtureReleaseNotes.pdf
fi

if [ -f usr/local/etc/DCIDTable ]; then
/bin/rm -rf usr/local/etc/DCIDTable
fi

exit 0

Upvotes: 0

Views: 231

Answers (1)

Jens
Jens

Reputation: 9130

Answer

Use ' around path/filenames that contain spaces or else the shell will try to interpret the parts as different from the filename and get confused, hence the error message.

More comments

  • As jubobs points out, there's no use in testing whether the file exists before deleting it. Furthermore, you already use the -f option which ignores nonexistent files so the test becomes irrelevant.
  • Remove absolute paths from your commands to the keep your script portable. The shell's PATH environment variable is used to search for commands in the right places.
  • No need to remove files from /tmp/ because the OS does that for you.
  • Be careful when you tinker with system folders like /usr/ because every system upgrades overwrite them, and often times it's hard to tell all dependencies.

You can simplify your script:

#!/bin/bash

## This will uninstall ETC Nomad v2.3.3.9.0.10.mpkg
## From Contents of ETCnomad Eos Mac 2.3.3.9.0.10.pkg

rm -rf '/Applications/Eos Family Welcome Screen.app'
# rm -rf /tmp/Element_Hotkeys.pdf
# rm -rf /tmp/Eos_Hotkeys.pdf
# rm -rf /tmp/FixtureReleaseNotes.pdf
rm -rf /usr/local/etc/DCIDTable

exit 0

Upvotes: 3

Related Questions