[英]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.