Operating System - HP-UX
1833587 Members
4162 Online
110061 Solutions
New Discussion

Re: Shell script filename expansion

 
SOLVED
Go to solution
Sebastien Masson
Valued Contributor

Shell script filename expansion

Hi all,

After a few search in the forum, I would like to do a wrapper for a unix command and I want to pass /*/zaza as script parameter and the script resend this parameter to the unix command without expanding the *....

Please help wanted....
8 REPLIES 8
Tom Danzig
Honored Contributor

Re: Shell script filename expansion

Add a backslash prior to the * to avoaid shell expanding the *.


/\*/zaza
Luc Bussieres
Frequent Advisor

Re: Shell script filename expansion

Sebastien,

What you need to do is quote the parameter, all the time. For example if your script is named test. The user will have to type:

test "/*/params"

This will make sure that the shell won't expand it. Then in the script you will also quote the parameter when you want to resend it to a unix command:

test2 "$1"


Luc
La réponse est le malheur de la question
Volker Borowski
Honored Contributor
Solution

Re: Shell script filename expansion

Hi,

you need to stop the shell from evaluating the wildcard, so the script must be called with quotes.

script '/*/zaza'

inside the script $1 will be '/*/zaza' WITHOUT the quotes.

Since the line in your script will be evaluated by a shell again, you need to re-insert quotes. But plainly reinserting quotes might hinder the shell from evaluating the $-sign to expand the parameter so you need to use double quoutes, therefore the call inside your script needs to be

call_inside_script "$1"

See the following to explain the quotes:

# export HELLO='*'

# echo $HELLO
Mail SD_CDROM a adabas b bin c cn_tmp dev etc home lib lost+found lsof-4.55-sd-11.00.depot mapvg01.map net nfs_mount omni omnicopy r

# echo '$HELLO'
$HELLO

# echo "$HELLO"
*


Hope this helps
Volker


Sebastien Masson
Valued Contributor

Re: Shell script filename expansion

Ok I think some few cmd output needed:

----------------------------
My sample script: zazou.sh
----------------------------
#!/bin/sh
echo $1
----------------------------

Sure: echo /\*/zaza
return: /*/zaza

But

./zazou.sh /\*/zaza
return: /a01/zaza /a02/zaza /a03/zaza

T. M. Louah
Esteemed Contributor

Re: Shell script filename expansion

BTW did u try .. # export H=\/\*\/zaza
# echo $H should give
/*/zaza


g`d luck
Little learning is dangerous!
Volker Borowski
Honored Contributor

Re: Shell script filename expansion

Yes Sebastien,

that is exactly correct.
If you want the script to display

/*/zaza

You need to use

echo "$1"

inside the script.

In
echo $1
the parameter $1 will be evaluated and after that the wildcard will be expanded.

Volker
Deepak Extross
Honored Contributor

Re: Shell script filename expansion

Are you looking for "set -f"?

Doing a
set -f
will turn off filename expansion. It will remained turned off till you issue a
set +f

Hope this helps.
Peter Kloetgen
Esteemed Contributor

Re: Shell script filename expansion

Hi Sebastien,

quoting (masquerading) of special characters isn't as trivial as people often think! But if you know the rules, it can be very simple:

'string_to_quote'--> quotes every special character within...

"string_to_quote" -->quotes some special characters, but others are not quoted, within there is continuing variable substitution and command substitution, so the $- character and the ``backticks are not quoted!

\special_character -->quotes the character which is following directly, only one character!

The next thing you need to know is, how a shell interpretes a command line before it is executed:

the shell takes a look at the whole command line and seeks for special characters to be interpreted, like backticks for command substitution or a $-character for variable substitution. It is easy to find out how the command line looks like after interpreting but before execution:

set -o xtrace
command_line
set +o xtrace

set -o xtrace turns on a debugging mode which shows you the command line after interpretation with a + character in front of the line. Then you see the output of the command line.
set +o turns of the debugging mode. These two lines at the beginning and at the end of your scripts can be very helpful when searching for errors!

example:
var=value
set -o xtrace
echo '$var' "$var" \$var (RETURN)
+ echo $var value $var
$var value $var


Allways stay on the bright side of life!

Peter
I'm learning here as well as helping