1

My question is a combination of sorts of this question and this question. I want to mount a file with spaces in its path with sudo permissions, as such:

sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name

If there's no spaces in the paths, I can just do this, since this user has sudo permissions for the mount command:

Runtime.exec("sudo mount /path/to/mount/point /dir/without/spaces");

But there are spaces, so I tried:

Runtime.exec("sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");

This gave me "Unrecognized arg '\'" or something to that effect from mount. Surrounding the pathnames with single or double quotes also didn't work.

Then I tried:

Runtime.exec("sudo", "mount", "/path/to/mount/point", "/dir with/spaces in name");

which of course fails because sudo wants some method of password entry, even though I don't normally need to enter a password for sudo mount. Faaantastic.

This seems like a catch-22 to me. I can get sudo working if I go with the single String method, or I can get the pathnames working if I go with the String array method. How do I get both working?

1 Answer 1

1

Single backslash cannot be written verbatim in string literal. Backslashes should be escaped, so use \\:

Runtime.exec("sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");

As for the password, sudo may not ask for password if recently you entered it on the same shell. If you want to allow to mount given path without superuser permissions (for regular user) edit its /etc/fstab entry as shown here.

Add user to its options, e.g.:

/dev/sda8    /media/foo    ext4    rw,user,exec 0 0

and then add privileges to read and write:

# chmod a+rw /media/foo

Then your line can be:

Runtime.exec("mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");

or

Runtime.exec("/bin/mount", "/path/to/mount/point", "/dir with/spaces in name");

or alike.

4
  • Thanks for the answer. And sorry, I was in a hurry yesterday and forgot to escape my backslashes: that wasn't the problem I was running into. I've tried every combination of single, double, and even triple backslashes ("\\", "\\\\", and "\\\\\\" in Java terms), but unfortunately none of those work. Sudo also has to stay in there, because the account I'm running this from has sudo permission for mount but not regular permission, and I don't have control over that. It's a very particular set of constraints, I know. Commented Oct 16, 2015 at 15:46
  • You want to sudo without supplying user password? Commented Oct 16, 2015 at 15:55
  • Basically I need a solution which both allows paths with spaces and doesn't require me to supply the password. I already have the mount command set up with NOPASSWD in /etc/sudoers for this account. Commented Oct 16, 2015 at 16:00
  • I'm just guessing: Runtime.exec("/usr/bin/sudo","/bin/mount",...);. You have to say something more on the result (process output? error message?) than just "does not work". Commented Oct 16, 2015 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.