这就是我尝试过的。
inputFile=$(zenity --file-selection --title "Test" --text "Please select the file for analysis." | sed -e "s/ /\\\ /g")
我sed
执行了将a替换\
为空格的操作,并使复制命令生效。然后从Zenity文件选择GUI我已经选择了一个文件,以使里面的值inputFile
是/home/username/Documents/Learning\ and\ Development/testfile.txt
。
现在,当我尝试使用以下命令将该文件复制到我的工作目录中时
cp $inputFile .
仍然返回错误,
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
有什么办法可以绕过这个?其实我在写程序。因此,我不想告诉用户重命名其文件夹名称以避免空格。
您可以轻松地做到这一点,用双引号将其括起来:
cp "$inputFile" /destination/
将字符括在双引号('“')中可保留引号内所有字符的字面值,但'$','`','\'除外,更多信息:http : //www.gnu.org/软件/bash/manual/html_node/Double-Quotes.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句