Finder got an error: The command exited with a non-zero status

I have a 2 TB USB HD with 8 partitions. Each partition has its own name.

Some processes ejects/dismounts them all 8 and this is intended to be so. However I like to load all of them (all 8) again at a later point. That is al 8 partitions in one go. I am planning to do this with an automatically running Apple Script.

However it could be they where not ejected/dismounted so I put a ‘if not’ in the script.

set driveName to "/dev/disk4"
tell application "Finder"
	if not (disk "driveName" exists) then
		set driveName to "/dev/disk4"
		set driveInfo to do shell script "diskutil list | grep \"" & driveName & "\""
		set driveID to last word of driveInfo
		do shell script "diskutil mountdisk " & driveID & ""
	end if
end tell

It hangs on: do shell script “diskutil list | grep "” & driveName & “"”
and I get error:
Finder got an error: The command exited with a non-zero status.

Why would this be?

How to fix?


PS this is running on an old Mac running 10.6.8


The “non-zero status” message is actually from the shell script and probably means that it couldn’t find the string “/dev/disk4” in the diskutil result. The reason it’s reported as coming from the Finder is that it’s the Finder being told to call the shell script, so the error message is coming back through it.

Before that, the Finder’s told to check for the existence of a disk called “driveName”. I don’t know if you mean that or not. The variable driveName is set to “/dev/disk4” in two different places.

Unfortunately (or maybe fortunately :wink: ) I no longer have 10.6.8, so I don’t know what the last word of driveInfo’s supposed to be. In 10.13.4, it’s one of a number of words describing the drive, eg. “virtual”, “internal”, “image”, etc.

In shell scripts the return value of the last succesfully executed command is returned, do shell script considers any non-zero value as an error which is for testing and many command an wrong interpretation. grep will return an non-zero value if the regular expression has no match, thus do shell script will responds with an error because it wrongly assumed that the command didn’t execute successful.

You can test it yourself quite easily, the false command in the shell returns an non-zero value:

do shell script "false"

so in your case: “/dev/disk4” didn’t exist at the time you ran the script.

If you’re switching drives a lot (including USB sticks) going by device number is not the best wat to find your disk. disk4 can be nothing, a few moments later an arbitrary removable storage device. To be more precise it’s much better to identify the disk by the volume names and the amount of partitions it has. Then lookup the device id and use diskutil to mount it.