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?
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, eg:
/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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.