[英]Java exec mount with sudo and file path spaces
我的问题是这个问题和这个问题的组合 。 我想使用sudo权限挂载路径中带有空格的文件,例如:
sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name
如果路径中没有空格,我可以执行此操作,因为该用户对mount命令具有sudo权限:
Runtime.exec("sudo mount /path/to/mount/point /dir/without/spaces");
但是有空格,所以我尝试了:
Runtime.exec("sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");
这给了我“无法识别的arg'\\'”或从mount获得的某种效果。 用单引号或双引号引起来的路径名也不起作用。
然后我尝试了:
Runtime.exec("sudo", "mount", "/path/to/mount/point", "/dir with/spaces in name");
这当然会失败,因为sudo需要某种密码输入方法,即使我通常不需要为sudo安装输入密码。 Faaantastic。
对我来说,这似乎是一个22点。 如果使用单个String方法,则可以使sudo工作;如果使用String数组方法,则可以使路径名工作。 我怎么都可以工作?
不能以字符串文字逐字写反斜杠。 反斜杠应该转义,所以使用\\\\
:
Runtime.exec("sudo mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");
至于密码,如果您最近在同一shell上输入sudo
,则sudo
可能不会要求输入密码。 如果你想允许安装没有超级用户权限的指定路径(普通用户)编辑的/etc/fstab
条目显示在这里 。
将user
添加到其选项,例如:
/dev/sda8 /media/foo ext4 rw,user,exec 0 0
然后添加读取和写入权限:
# chmod a+rw /media/foo
然后您的行可以是:
Runtime.exec("mount /path/to/mount/point /dir\\ with/spaces\\ in\\ name");
要么
Runtime.exec("/bin/mount", "/path/to/mount/point", "/dir with/spaces in name");
或类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.