Is there a way to perform an if statement based on the error output of a command?
For example, I want to use udisksctl
in a script which mounts a USB drive and then perform some action on the drive contents.
- The drive is not mounted: it should mount it and then perform the action.
- The drive is absent: it should throw an error message and exit;
- The drive is already mounted, it should perform the action;
Running udisksctl
can have three results in this scenario:
In case 1), it runs successfully.
In the other two cases it returns an error, but:
- In case 2), it gives me the error:
Error looking up object for device ...
- In case 3) it gives me the error:
Error mounting /dev/sdb1: GDBus.Error:org.freedesktop.UDisks2.Error.AlreadyMounted: Device /dev/sdb1 is already mounted at
Is there an elegant way to make an if statement which takes different actions based on these two error messages?
Piping the error message to grep like this udisksctl mount -b /dev/disk/by-label/CRUZER 2> >(grep -i "Error looking up object for device")
seems to make no difference in the output.
Aucun commentaire:
Enregistrer un commentaire