这是我尝试过的。
inputFile=$(zenity --file-selection --title "Test" --text "Please select the file for analysis." | sed -e "s/ /\\\ /g")
Run Code Online (Sandbox Code Playgroud)
我做了sed操作用 a\和一个空格替换空格以使复制命令起作用。然后从 Zenity 文件选择 GUI 中我选择了一个文件,因此里面的值inputFile是/home/username/Documents/Learning\ and\ Development/testfile.txt.
现在,当我尝试将此文件复制到我的工作目录时
cp $inputFile .
Run Code Online (Sandbox Code Playgroud)
它仍然返回错误,
cp: cannot stat ‘/home/user/Documents/Learning\\’: No such file or directory
cp: cannot stat ‘and\\’: No such file or directory
cp: cannot stat ‘Development/WhatsApp.apk’: No such file or directory
Run Code Online (Sandbox Code Playgroud)
有没有办法绕过这个?其实我正在写一个程序。所以我不想告诉用户重命名他们的文件夹名称以避免空格。
May*_*hux 12
你可以很容易地做到这一点,用双引号括起来:
cp "$inputFile" /destination/
Run Code Online (Sandbox Code Playgroud)
用双引号 ('"') 括起来的字符会保留引号内所有字符的字面值,'$'、'`'、'\' 除外,阅读更多:http : //www.gnu.org/软件/bash/manual/html_node/Double-Quotes.html
您可以在出现空格的地方添加 *,然后可以像这样完成复制
cp /home/user/users*tst.txt /home/user/Downloads
Run Code Online (Sandbox Code Playgroud)
您要将用户 tst.txt 从主文件夹复制到下载文件夹的位置