several arguments together

Hello guys,

you may help me on this :

I want to search thru many files if a sentence or a string exists and printout the lines where this is present by calling a script like :

loc "arg1 arg2 arg3 arg4"
where arg1 arg2 arg3 arg4 would be in fact the sentence separated with blanks...

In my loc script, I would have to count the number of arguments and store them as a sentence into one single variable i guess, like :
sentence=`arg1 arg2 arg3 arg4`

But, as arguments number is undefined, how can i insure this to work in any case ?

Any idea ? :rolleyes:
Thanks,
homefp :wink:

If you invoke a script like this:
loc "one two three"

The script called loc will see only one argument and it will be "one two three". This seems to be what you want, so I don't see a problem here.

thanks but,

in my loc i do the following :

egrep " -ni $1"

and $1 is egual to arg1 arg2 arg3 arg4 which does not give what expected of course.
I tried egrep " -ni `$1`" and other ways but still no result.

In fact I receive the following message :

egrep: can't open arg2

Any more idea ?
:frowning:

Try:
egrep -ni "$1"

Thanks perderabo,
of course I already tried this but here is my concern :
in fact i'm using the following command :
egrep_source argument="-ni $1" where egrep_source is an embedded egrep working in a configuration management tool...
And, as you can see, the quotes are already present and mandatory for egrep_source to work.
The only way I found to solve this is entering the command :
loc arg1 "arg2 arg3 arg4"
with the following code :
egrep_source argument="-ni $1" | egrep $2
This works of course, but I don't really like it !

If you have a better idea...
:rolleyes:

Hi homefp,

 here you go.

you get to know the number of args passed to a script using $#. I think that solves your problem

Also use $@ in the grep directly and it ll take care of all. :slight_smile:

does not work or I don't know how to manage this !.....sorry ! :rolleyes:

egrep_source argument="-ni $@" gives me an empty output...
and using $# is tricky as I don't know how many arguments will be entered....
Do you mean using $# in a for loop ? How ?

thanks

:confused:

linuxpenguin suggestion ($# )works in sh and ksh but not csh. What is your script written in?

Using sh or ksh:
$ cat newtest
#!/bin/ksh
echo $#
$
$ ./newtest what is this
3
$

Using csh:
% ./newtest what is this
Variable syntax
%

The other suggestion, $@, gives the following in sh or ksh (csh gives same error)

$ cat newtest
#!/bin/sh
echo $@
$
$ ./newtest what is this
what is this

I'm using sh and both are working
$# = number of args
#@ = all args together

my problem is : how to use them ?!
My tests are giving me wrong results !
I need to find my args as a sentence in the right order....
For instance > loc arg1 arg2 arg3 arg4
with $@ = arg1 arg2 arg3 arg4 which could be "hi how are you" sentence.
I don't know how to use $# as $@ does not work in Egrep_source due to the quotes...

thanks again
:wink: