在bash脚本中转义空间 [英] Escaping space in bash script

查看:38
本文介绍了在bash脚本中转义空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,以附加所有以.hash结尾的文件,以通过md5deep进行验证.名称中带有空格的文件似乎破坏了此脚本.

I am trying to make a script to append all files ending with .hash to be verified by md5deep. Files with space in their name seem to break this script.

#!/bin/bash
XVAR=""
for f in *.hash
do
    XVAR="$XVAR -x $f "
done
md5deep -e $XVAR -r *

每当我使用名为"O S.hash"的文件运行脚本时,我都会得到

Whenever i run the script with a file called "O S.hash" i would get

O: No such file or directory

如果我将 XVAR ="$ XVAR -x $ f" 更改为 XVAR ="$ XVAR -x \'$ f \'" XVAR="$ XVAR -x \" $ f \"

md5deep将以"O代替"的形式渗透输入

md5deep will interpenetrate the input as "O instead

"O: No such file or directory

脚本中变量的回显将XVAR显示为 -x'O S.hash' -x"O S.hash"

an echo of the variable in the script shows XVAR as -x 'O S.hash' or -x "O S.hash"

在shell中手动输入命令,例如 md5deep -e -x"O S.hash" -r * 可以,但是如果脚本中的命令似乎中断了

a manual input of the command in shell such as md5deep -e -x "O S.hash" -r * works but if its in the script the command seems to break

推荐答案

这不是最好的解决方案,但似乎可以使用:

This is not the nicest solution, but is seems it will work:

find . -name '*.hash' -printf "-x\0%p\0" | xargs -0 md5deep -r * -e

这实际上与OP所需的功能不完全相同,因此这是蒂姆·波特(Tim Pote)和乔纳森·莱夫勒(Jonathan Leffler)建议的修改:

This actually doesn't do exactly the same as the OP wanted, so here's a modification as suggested by Tim Pote and Jonathan Leffler:

find . -maxdepth 1 -name '*.hash' -printf "-x\0%p\0" | xargs -0 md5deep -r * -e

这篇关于在bash脚本中转义空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆