代码: 全选
#!/bin/sh
if [ $# -ne 2 ]; then
echo "Usage: txtstr path string"
exit 1
fi
if [ ! -d "$1" ]; then
echo "Input path not exist"
exit 1
fi
find ${1} -type f |xargs -I {} sh -c 'file="{}";type=$(file $file);[[ $type=~"ASCII text" ]]&&echo $file' |xargs grep "${2}"
如果文件名包含空格,则出错为:
No such file or directory
如果内容里包含',则出错为:
sh: -c: line 0: unexpected EOF while looking for matching ``'
sh: -c: line 1: syntax error: unexpected end of file
要怎么改这一段脚本呢,或者用其它什么办法可以达到需求呢。