我正在构建一个简单的脚本,它从目录中选择一个随机文件,然后将文件名作为参数传递给Python脚本,然后删除该文件。
#!/bin/bash
var=$(ls images/ | /usr/bin/shuf -n 1 )
imagepath="'images/"$var"'"
python myscript.py -i $imagepath
rm $imagepath如果文件中有空格(这就是目的),脚本就会失败,因为它像传递单个参数一样传递空格。
例如
rm: cannot remove "'images/my": No such file or directory
rm: cannot remove 'test': No such file or directory
rm: cannot remove "file.jpg'": No such file or directory如果我在$imagepath上的原始脚本中添加了一个回显,然后将输出‘图像我的测试file.jpg’用单引号输入rm,它会删除该文件。只有当脚本试图运行它时,它才会失败。它也适用于python脚本。
我尝试过将$imagepath转储到数组中,并将其用作输入参数,但它仍然以同样的方式失败。
如果变量中有空格,如何将其作为参数传递?
发布于 2020-05-17 04:20:08
如果希望保留空格,请在双引号中嵌入变量名。
python myscript.py -i "$imagepath"https://stackoverflow.com/questions/61846852
复制相似问题