0

I have this simple bash script: I run ns simulator on each file passed in argument where last argument is some text string to search for.

#!/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

I expected:

   file1.tcl Successful

I am getting:

   temp12345ashish.temp
   file1.tcl Successful

When i run the simulator command myself on the terminal i do not get the file name to which output is directed.

I am not getting from where this first line of output is getting printed. Please explain it.

Thanks in advance.

2 Answers 2

3

See man grep, and see specifically the explanation of the -l option.

In your script (above), you are using -l, so grep is telling you (as instructed) the filename where the match occurred.

If you don't want to see the filename, don't use -l, or use -q with it also. Eg:

grep -ql ${BASH_ARGV[0]} $temp
Sign up to request clarification or add additional context in comments.

Comments

0

Just silence the grep:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.