Operating System - HP-UX
1825766 Members
2101 Online
109687 Solutions
New Discussion

How to capture * without automatic expansion

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

How to capture * without automatic expansion

Here's what I want to do.

User enters: myprogram.sh *
I want to just capture the asterick (*), I don't want the filename expansion. Later in the program I use the * to help derive filenames from a different directory. How can I do this?

Example:
MYVAR=$*
if [ "X*" = "X${MYVAR}" ] ; then
ll -d /var/spool/lp/request/*
fi

I tried using set -f but it only does it for the current shell and not the script.

Thanks...
Jack...
7 REPLIES 7
Sundar_7
Honored Contributor

Re: How to capture * without automatic expansion


set -o noglob

Learn What to do ,How to do and more importantly When to do ?
Mark Grant
Honored Contributor

Re: How to capture * without automatic expansion

you coud put "set -f" in your script of course.
Never preceed any demonstration with anything more predictive than "watch this"
Kevin Wright
Honored Contributor

Re: How to capture * without automatic expansion

escape it '*' or \*
Stuart Abramson_2
Honored Contributor

Re: How to capture * without automatic expansion

myprogram.sh \*

or

myprogram.sh "*"
Michael Steele_2
Honored Contributor

Re: How to capture * without automatic expansion

Use a backslash along with any special character in a search.

find /dir -name "\*"
-or-
grep "\*" file
-or-
Use inode numbers.

ls -i (* write down inode number *)

find /dir -inum ### | awk ' { print $NF ) '
Support Fatherhood - Stop Family Law
Jack C. Mahaffey
Super Advisor

Re: How to capture * without automatic expansion

Using the \* option apparently is the best approach. I was hoping to intercept the * in the program.

I tried setting the noglob / -f options in the script and it didn't work when I looked at the variable in the program. It still expanded.


thanks all...

Jack
Bill Hassell
Honored Contributor
Solution

Re: How to capture * without automatic expansion

The problem is that your script nev er gets a chance to change the behavior of shell expansion...it occurred BEFORE you started the script. Your local shell does the expansion by default. To see this, rather than entering myprogram *, type:

echo myprogram *

Wow! myprogram now has a long list of filenames because the local shell expanded the list before myprogram even got started. This, the \* construct tells the CURRENT shell to stop any expansion on the next character. You can type this:

set -f
myprogram *

and it will work fine. BUT now something like ls *txt or echo *abc* will no longer produce any file matches because set -f turns of special character expansion permanently. This, the \* mechanism is the best choice. Another alternative is to tell users that anything besides alphanumerics should always be enclosed in quotes:

myprogram "*"

This tends to be more portable across other opsystems too (special characters require special handling).


Bill Hassell, sysadmin