日期:2014-05-16  浏览次数:20680 次

傻瓜grep脚本
本帖最后由 songjinshi 于 2013-12-23 15:29:17 编辑
#!/bin/sh

if [ "$1" = "" ]
then
echo -e "The first parameter can not be empty.\nPlease check the first parameter from your input."
#exit 1
elif [ "$2" = "" ]
then
echo -e "The second parameter can not be empty.\nPlease check the second parameter from your input."
#exit 2
elif [ "$3" = "" ]
then
echo -e "The third parameter can not be empty.\nPlease check the third parameter from your input."
#exit 3
elif [ "$4" = "" ]
then
echo -e "The fourth parameter is null.\nDefault output to current findReslut.txt file."
filelist=$(find $2 -name $3)
if [ "$filelist" != "" ]
then
grep -n $1 -r $filelist > findResult.txt
#exit 4
fi
else
filelist=$(find $2 -name $3)
if [ "$filelist" != "" ]
then
grep -n $1 -r $(find $2 -name $3) > $4
fi
fi


------解决方案--------------------
不要所有的代码都定格写
#!/bin/sh

if [ "$1" = "" ]
then
    echo -e "The first parameter can not be empty.\nPlease check the first parameter from your input."
    #exit 1
elif [ "$2" = "" ]
then
    echo -e "The second parameter can not be empty.\nPlease check the second parameter from your input."
    #exit 2
elif [ "$3" = "" ]
then
    echo -e "The third parameter can not be empty.\nPlease check the third parameter from your input."
    #exit 3
elif [ "$4" = "" ]
then
    echo -e "The fourth parameter is null.\nDefault output to current findReslut.txt file."
    filelist=$(find $2 -name $3)
    if [ "$filelist" != "" ]
    then
       &