【问题标题】:Bash Script : Unwanted OutputBash 脚本:不需要的输出
【发布时间】:2013-04-23 05:15:19
【问题描述】:

我有这个简单的 bash 脚本: 我对传入参数的每个文件运行 ns 模拟器,其中最后一个参数是要搜索的文本字符串。

#!/bin/bash

nsloc="/home/ashish/ns-allinone-2.35/ns-2.35/ns"
temp="temp12345ashish.temp"

j=1

for file in "$@"
do
        if [ $j -lt $# ]
        then
                let j=$j+1

                `$nsloc $file > $temp 2>&1`

                if grep -l ${BASH_ARGV[0]} $temp
                then
                        echo "$file Successful"

                fi

        fi
done

我预计:

   file1.tcl Successful

我得到:

   temp12345ashish.temp
   file1.tcl Successful

当我自己在终端上运行模拟器命令时,我没有得到输出指向的文件名。

我没有从第一行输出的打印位置得到。 请解释一下。

提前致谢。

【问题讨论】:

    标签: linux bash scripting


    【解决方案1】:

    见man grep,具体见-l选项的解释。

    在您的脚本(上图)中,您使用的是-l,因此 grep 会告诉您(按照说明)匹配发生的文件名。

    如果您不想看到文件名,请不要使用-l,或同时使用-q。例如:

    grep -ql ${BASH_ARGV[0]} $temp
    

    【讨论】:

      【解决方案2】:

      让 grep 静音:

       if grep -l ${BASH_ARGV[0]} $temp &> /dev/null 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-31
        • 2018-11-12
        • 1970-01-01
        相关资源
        最近更新 更多