我在寻找一个动作,当一个bahsscript在我的家中找到一个文件中的字符串,但不知何故它不工作,我不明白为什么.. 我有我的grep不会返回0的印象(而字符串确实存在于文件中)。用if语句检查文件夹中的字符串
#!/bin/bash
find /home/ -type f | xargs grep -q 'stringtofind'
if [ $? == 0 ] #if the result is not equal to zero
then
echo "Action"
else
echo "Pas d'action"
exit 1
fi
exit 0
当我做了以下我的壳返回我123,而不是0
find /home/anselmewebsite/ -type f | xargs grep -q 'stringtofind' ; echo $?
shell returns : 123
预先感谢您的亮点,
非常感谢。正如我之前所说,我不知道为什么我使用xargs时,grep可以自己做到这一点..初学者的错误。再次感谢。 – Anselme
@安塞尔姆没问题。请注意,一些发行版为'grep -r'提供'rgrep'简写。如果你的发行版没有,你可以在你的.bashrc中创建一个别名:'alias rgrep =“grep -r”' – hek2mgl
感谢有用的提示..确实grep -r将需要很多:) – Anselme