How to pass reg exp as command line argument in shell script?

How to pass reg exp as command line argument in shell script?

Post by Brandon Hopp » Sun, 21 Jul 2002 04:56:23



I want to pass a directory filter as a command line argument as such:

./script *.c

But *.c expands to every .c file in the directory on the command line,
so instead of 1 argument, I get several arguments.

Is there way for the script to receive *.c instead of a list of files. I
want the variable $1 to be "*.c"

TIA

 
 
 

How to pass reg exp as command line argument in shell script?

Post by John Do » Sun, 21 Jul 2002 05:20:06



Quote:> Is there way for the script to receive *.c instead of a list of files. I
> want the variable $1 to be "*.c"

./script '*.c'

 
 
 

How to pass reg exp as command line argument in shell script?

Post by Brandon Hopp » Sun, 21 Jul 2002 05:37:49





> > Is there way for the script to receive *.c instead of a list of files. I
> > want the variable $1 to be "*.c"

> ./script '*.c'

Granted that works although it expands *.c into all the files still. I
would rather not have to put quotes around it so that it works as other
scripts when wanting a list of files.
 
 
 

How to pass reg exp as command line argument in shell script?

Post by John Do » Sun, 21 Jul 2002 05:57:52



Quote:> Granted that works although it expands *.c into all the files still. I
> would rather not have to put quotes around it so that it works as other
> scripts when wanting a list of files.

Well, you have to do something to prevent expansion by the shell before your
script sees it. You can escape the * by
./script \*.c

Also, in your script refer to it as "$1" instead of simply $1

For example:
#!/bin/ksh
echo "$1"

When I call this script as
./script \*.c
I get
*.c
as the output

 
 
 

How to pass reg exp as command line argument in shell script?

Post by William Par » Sun, 21 Jul 2002 08:00:31






>> > Is there way for the script to receive *.c instead of a list of files. I
>> > want the variable $1 to be "*.c"

>> ./script '*.c'

> Granted that works although it expands *.c into all the files still. I
> would rather not have to put quotes around it so that it works as other
> scripts when wanting a list of files.

It is the shell which does the filename expansion.  So, you have to quote
the metacharaters '*' somehow, either by
    '*.c'
or by
    \*.c

--

8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin

 
 
 

How to pass reg exp as command line argument in shell script?

Post by John DuBo » Sun, 21 Jul 2002 14:05:05






>> > Is there way for the script to receive *.c instead of a list of files. I
>> > want the variable $1 to be "*.c"

>> ./script '*.c'

>I would rather not have to put quotes around it

The script itself has no control over this; by the time it gets its arguments
globbing has either been done or it hasn't.  If your interactive shell happens
to be zsh, you can control whether such globbing is done by using the
precommand modifier 'noglob':

alias myscript='noglob myscript'

        John
--

 
 
 

How to pass reg exp as command line argument in shell script?

Post by Nick Hayne » Tue, 23 Jul 2002 19:30:50






>>>Is there way for the script to receive *.c instead of a list of files. I
>>>want the variable $1 to be "*.c"

>>./script '*.c'

> Granted that works although it expands *.c into all the files still.
> I
> would rather not have to put quotes around it

Sorry, but that's the way it works.
 
 
 

How to pass reg exp as command line argument in shell script?

Post by Brandon Hopp » Wed, 24 Jul 2002 02:27:53


Quote:> Sorry, but that's the way it works.

That's cool. Thanks for all the replies. Learned something new today.
 
 
 

1. Passing variables as arguments to commands in a shell script

Hi all,

I am a newbie with shell scripting and not sure if I am going about
this the right way.

What I have is:

  ADGroup="Domain Admins"
  cmd="ldapsearch -x -LLL"
  filter="CN=\"${ADgroup}\" member"
  ${cmd} ${filter}

I want the command to run as

  ldapsearch -x -LLL CN="Domain Admins" member

Instead what actually runs is

  ldapsearch -x -LLL 'CN="Domain' 'Admins"' member

How can I setup the variables so that I don't get the extra single
quotes when the command runs?

What also will work is

    ldapsearch -x -LLL CN='Domain Admins' member

But I have tried several combinations and couldn't get any to work with
the way my variables are setup.

Thanks,

Shahid

2. Compaq PowerStorm on Alpha XP1000

3. How can I pass a command line argument to an aliased command?

4. Acrobat readers

5. bourne shell script command line argument

6. groff fonts??

7. Question about passing variable as reg exp in awk

8. DB/DBM problems

9. Passing command-line arguments

10. passing command line arguments to RPC server(again)

11. passing command line arguments to RPC server

12. how to pass command line arguments to strace?

13. "pidof" on a process (which was ran by passing arguments from the command line)