0

When using the following:

for what in $@; do
read -p "Where?" where
grep -H "$what" $where -R | cut -d: -f1

How can I, instead of using read to define a user-variable, have a second variable input along with the first variable when calling the script.

For example, the ideal usage I believe I can get is something like:

sh scriptname var1 var2

But my understanding is that the for... line is for looping the subsequent entires into the one variable; what would I need to change to input multiple variables?

2
  • So do you want to access the command line arguments passed to the script? Commented Feb 9, 2013 at 22:42
  • Yes, thank you; I couldn't seem to articulate what I was getting at. I would like to be able to, for example, pass the first argument to the first variable position, second to second, etc. Commented Feb 9, 2013 at 22:44

2 Answers 2

2

As an aside: using | cut -D: -f1 is not safe, because grep does not escape colons in filenames. To see what I mean, you can try this:

ghoti@pc:~$ echo bar:baz > foo
ghoti@pc:~$ echo baz > foo:bar
ghoti@pc:~$ grep -Hr ba .
./foo:bar:baz
./foo:bar:baz

Clarity .. there is not.

So ... let's clarify what you're looking for.

  1. Do you want to search for one string in multiple files? Or,
  2. Do you want to search for multiple strings in one file?

If the former, then the following might work:

#!/bin/bash

if [[ "$#" -lt 2 ]]; then
  echo "Usage: `basename $0` string file [file ...]
  exit 1
fi

what="$1"
shift        # discard $1, move $2 to $1, $3 to $2, etc.

for where in "$@"; do
  grep -HlR "$what" "$where" -R
done

And if the latter, then this would be the way:

#!/bin/bash

if [[ "$#" -lt 2 ]]; then
  echo "Usage: `basename $0` file  string [string ...]
  exit 1
fi

where="$1"
shift

for what in "$@"; do
  grep -lR "$what" "$where"
done

Of course, this one might be streamlined if you concatenated your strings with an or bar, then used egrep. Depends on what you're actually looking for.

Sign up to request clarification or add additional context in comments.

4 Comments

I should've clarified: The intent was to search a given directory for files where a match for the specified string existed where the output is just the filenames where the string existed. Thank you for the tips; I'll make some changes with that in mind, but I think I found what I wanted, more or less.
So ... a single directory, and a single file? Where does $@ come in to it?
Well, I had more or less just re-appropriated something I'd used in a previous script that I mistakenly understood the purpose of; in any case, I was hoping to replace the $@ section and I was able to using the other answer to this question. I apologize for my earlier explanation of the issue.
@josephmarhee - your final question was "what would I need to change to input multiple variables". The other answer may provide more succinct help for the problem you were having, but this one actually answers the question you asked.
2

You can get parameters passed on the command line with $1 $2 etc.

Read up on positional parameters: http://www.linuxcommand.org/wss0130.php. You don't need a for loop to parse them.

sh scriptname var1 var2

v1=$1 # contains var1
v2=$2 # contains var1

$@ is basically just a list of all the positional parameters: $1 $2 $3 etc.

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.