Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Turn off file expansion for command line arguments

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I supply a script with cmd line args ...
e.g. somescript -a *.msg

When I echo the cmd line arguments in the script:
echo "$@" or with echo "$*"
(I want "-a *.msg")

Using a "set -f" within the script has no effect on the cmd line arguments.

However, if I use a "set -f" at the command prompt BEFORE I execute the script, file expansion doesn't occur.

How do I prevent file expansion from within the script on cmd line arguments?

I'm on solaris using ksh shell ...

 
You either set -f on the invoking shell (your login shell) or you hide the wildcard characters from it.

set -f
yourscript -a *.msg
set +f

or

yourscript -a "*.msg"
yourscript -a '*.msg'
yourscript -a \*.msg

Another possibility is to use a wildcard character that the shell leaves alone e.g. yourscript -a %.msg

Then within the script, translate (see man tr) that % into a *.



HTH,

p5wizard
 
p5 -

Yes, I understand either case (set -f on the invoking shell or hide the wildcard characters at input), but you & I know we can't expect an end-user to do either of these two task.

How about some kind of an option right after the "#! /bin/ksh" at the top of the script. I tried, to no avail:

#! /bin/ksh -f
or
#! /bin/ksh -o noglob

(In fact I can't get any options to work after the she-bang ... any thoughts?)
 
No, settings for the shell that interprets the script cannot keep the parent shell from expanding wildcards BEFORE the script is launched...

You may try using an alias to turn -f on automatically, but I don't see how you'd turn -f off again after you run your script:

alias yourscript='set -f;/path/to/yourscript'

a user invokes:

yourscript -a *.msg

the shell alias-expands this to

set -f; /path/to/yourscript -a *.msg

and the strings are provided unchanged as parameters to yourscript...


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top