结合grep并找到所需的结果

时间:2017-11-20 06:11:37

标签: linux bash unix

我正在尝试将从脚本中找到的文件列为“文件大小 - 路径”。我的脚本需要过滤的是每当我给它两个参数并且第一个参数是“-u”时,那么我要查找的文件是那些在开头有“优先级”字样的文件。文件。 (第二个参数始终是目录)。

到目前为止,我有这个:

    if [ "$1" = -u ]
    for i in `grep -ril ^Priority "$2"`
    do
    echo | ls -lh `grep -ril ^Priority "$i"` | cut -d" " -f5,9
    done
    fi

它返回第7行不期望文件的结尾。

然而,当我运行时:

for i in `grep -ril ^Prioridad "$1"`
do
echo | ls -lh `grep -ril ^Prioridad "$i"` | cut -d" " -f5,9
done

返回所需的结果。

如何让第一个使用这样的结果但是使用-u作为第一个参数和目录作为第二个?

1 个答案:

答案 0 :(得分:3)

您错过了then

if [ "$1" = -u ]; then # <-- here
    for i in `grep -ril ^Priority "$2"`
    do
      # Some more edits and trying to remove the echo/ls
      # and battling against not found scenario
       if grep -ril ^Priority "$i" ; then # if exists 
          echo "$(stat -c%s $i) $i" # stat size and File
       fi
    done
fi