grep "$1 $2" $TMP/"ORACLE_SID"_dbmode.txt > /dev/null 2>$1
  1. What is the meaning of the above command?
  2. What are $1 and $2? Why it is necessary?
  3. Is there any alternatives for using $1 and $2?

Is that exactly what the code looks like?

As it is, it is a function that takes two arguments, let's say they're arg1 and arg2.

Then it reads a file called ORACLE_SID_dbmode.txt and prints every line that contains arg1 arg2 (joined together with a space in between), and saves the error messages in a file called arg1.

This is quite confusing, so I don't think this is the actual code.

I would imagine it is actually this:

    grep "$1" "$2" $TMP/"$ORACLE_SID"_dbmode.txt >/dev/null 2>&1


  • "$1 $2" becomes "$1" "$2"
  • 2>$1 becomes 2>&1

If so, it could be used two ways:

grepstr options string_or_pattern

for example:

grepstr -i something

would make it print (see below) any lines in the file "$ORACLE_SID"_dbmode.txt that include the string something, Something, SOMETHING, etc.

-i means case insensitive and is just an example; any option could be used.

(Also, technically something is not a string, but a pattern, for example [0-9] would match any digit).

The second way is:

grepstr string_or_pattern filename

in which case it will print any lines matching string_or_pattern in filename or "$ORACLE_SID"_dbmode.txt.

That seems less likely, but it is possible, depending how the code is used.

It could also be called like this:

grepstr option1 option2

but that would read from standard input (e.g. the keyboard by default), and print any lines that contained "$ORACLE_SID"_dbmode.txt, which is almost certainly not what was intended, given "$ORACLE_SID"_dbmode.txt looks like a file name, not a string or pattern to match.

If my guess is right, the code isn't >/dev/null 2>$1, instead it's >/dev/null 2>&1, which means output and error messages are not printed.

>/dev/null means that grep's standard output (i.e. the matching lines) is being sent to /dev/null, which is a special file that the operating system just ignores.

2>&1 after >/dev/null means that error messages go there too.

So if the output isn't important, grepstr is probably being called either like this:

if grepstr...

or like this:

if [ $? -ne 0 ]

to only execute some code if there was a match.

In answer to the other part of your question, yes, it could be changed to

    grep "$@" $TMP/"$ORACLE_SID"_dbmode.txt > /dev/null 2>&1

The "$@" expands to "$1" "$2" "$3"... (i.e. as many arguments as are provided).

I think that way would be better, because it would be more flexible.

On the other hand, if it does require two arguments for some reason I can't think of, then perhaps "$1" "$2" is what the author intended.


$1 and $2 are parameters. Your snippet is defining function grepstr.

For example, running

grepstr first second

actually runs command

grep "first second" $TMP/"ORACLE_SID"_dbmode.txt

and redirects output to /dev/null (which is message sink, everything put there just disappears).

Basically, when you run script (or function), $1, $2, $3 (etc.) are parameters you gave. You can test whether parameter is set with -z, for example if [ -z $3 ]; then ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.