Discussion:
Scripting help needed
(too old to reply)
Generic Usenet Account
2010-10-14 22:47:49 UTC
Permalink
Hi,

I am having trouble with my shell script. I have been able to
reproduce the problem with the small scripting snippet:

======
#!/bin/ksh
cmd="egrep -n 'Pat-1 | Pat-2' file"
echo "Executing $cmd"
`$cmd` # `echo $cmd` also does not work
=====

I know that I am doing something really silly. Any help will be
appreciated.

Thanks,
Ramesh
John Gordon
2010-10-14 23:29:44 UTC
Permalink
In <1e3adb4f-c08f-4b92-83ee-***@i5g2000yqe.googlegroups.com> Generic Usenet Account <***@sta.samsung.com> writes:

> I am having trouble with my shell script. I have been able to
> reproduce the problem with the small scripting snippet:

> ======
> #!/bin/ksh
> cmd="egrep -n 'Pat-1 | Pat-2' file"
> echo "Executing $cmd"
> `$cmd` # `echo $cmd` also does not work
> =====

> I know that I am doing something really silly. Any help will be
> appreciated.

The backticks didn't work because they're used for capturing the
*output* of a command.

If you have a command stored in a variable and you want to execute it,
just use the variable all by itself:

$cmd

--
John Gordon A is for Amy, who fell down the stairs
***@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
Generic Usenet Account
2010-10-15 00:44:14 UTC
Permalink
On Oct 14, 6:29 pm, John Gordon <***@panix.com> wrote:
>
> The backticks didn't work because they're used for capturing the
> *output* of a command.
>
> If you have a command stored in a variable and you want to execute it,
> just use the variable all by itself:
>
>   $cmd
>

I forgot to mention that I had tried that already, but I get the
following error:
egrep: can't open |
egrep: can't open Pat-2'

Thanks,
Ramesh
Barry Margolin
2010-10-15 01:32:26 UTC
Permalink
In article
<263896dc-b078-4e45-ba8a-***@l8g2000yql.googlegroups.com>,
Generic Usenet Account <***@sta.samsung.com> wrote:

> On Oct 14, 6:29 pm, John Gordon <***@panix.com> wrote:
> >
> > The backticks didn't work because they're used for capturing the
> > *output* of a command.
> >
> > If you have a command stored in a variable and you want to execute it,
> > just use the variable all by itself:
> >
> >   $cmd
> >
>
> I forgot to mention that I had tried that already, but I get the
> following error:
> egrep: can't open |
> egrep: can't open Pat-2'

eval "$cmd"

--
Barry Margolin, ***@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Generic Usenet Account
2010-10-15 15:31:30 UTC
Permalink
On Oct 14, 8:32 pm, Barry Margolin <***@alum.mit.edu> wrote:

>
> eval "$cmd"
>
> --

Thank you John! Than you Barry! That indeed did the trick.

I have another question. In my original script, I would like to pass
certain command line parameters. Most of them are single words, but
sometimes they can be a collection of words within double quotes.
Something like this:

my_util first_arg second_arg "this is the third argument" fourth_arg

I would like "this is the third argument" in its entirety to be
interpreted as the third argument.

TIA!
Ramesh
Generic Usenet Account
2010-10-15 15:38:46 UTC
Permalink
On Oct 14, 8:32 pm, Barry Margolin <***@alum.mit.edu> wrote:
> eval "$cmd"

First of all apologies if this is a duplicate post. My original post
is not showing up.

Thanks to all who chipped in. eval is working like a charm.

I have another question. Suppose I want to pass single word and
multiple-word arguments to a script. For example:
my_util arg-1 arg-2 "This is the third argument" arg-4

I would like "This is the third argument", in its entirety, to be
treated as a single argument.

Thanks in advance!
Ramesh
David Schwartz
2010-10-15 16:51:34 UTC
Permalink
On Oct 15, 8:38 am, Generic Usenet Account <***@sta.samsung.com>
wrote:
> On Oct 14, 8:32 pm, Barry Margolin <***@alum.mit.edu> wrote:
>
> > eval "$cmd"
>
> First of all apologies if this is a duplicate post.  My original post
> is not showing up.
>
> Thanks to all who chipped in.  eval is working like a charm.
>
> I have another question.  Suppose I want to pass single word and
> multiple-word arguments to a script.  For example:
> my_util arg-1 arg-2 "This is the third argument" arg-4
>
> I would like "This is the third argument", in its entirety, to be
> treated as a single argument.
>
> Thanks in advance!
> Ramesh

Then you need to put it in quotes, just like the person who passed it
to you needed to do so.

DS
Continue reading on narkive:
Loading...