Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

How to keep special meaning "*" inside a script

SOLVED
Go to solution

How to keep special meaning "*" inside a script

Hello,

I want to execute the following command inside a script but it doesn't work because the special character "*" lose its special meaning inside a script.

...
if test -f /tmp/ffin*
...

Has anyone any idea?
14 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: How to keep special meaning "*" inside a script

Hi:

Escape the asterisk with a backslash:

# touch /tmp/my\*file
# rm /tmp/my\*file

...are examples of this.

# if test -f /tmp/ffin\*

...would test for the presence of a file named '/tmp/ffin*'

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: How to keep special meaning "*" inside a script

you could do it like this

if [ -f "/tmp/ffin*"]
then.....
All paths lead to destiny
Helen French
Honored Contributor

Re: How to keep special meaning "*" inside a script

Use backslash (\) in front of * like this:

..../tmp/ffin\* ...
Life is a promise, fulfill it!

Re: How to keep special meaning "*" inside a script

Hello again,

I just want to keep the meaning of "*".

I want to know if there are files with the pattern "ffin". For example, ffin0202.txt or ffin0304.txt, ...
Ravi_8
Honored Contributor

Re: How to keep special meaning "*" inside a script


Hi,

\* will keep metacharacter"*" anywhere
never give up
Leif Halvarsson_2
Honored Contributor

Re: How to keep special meaning "*" inside a script

Hi,
Another way is to use single quotes.

# touch '/tmp/my*file'
# rm '/tmp/my*file'


Dario_1
Trusted Contributor

Re: How to keep special meaning "*" inside a script

Hi!!

Use the backslash (\) before the *.

Regards,

DR
Francisco J. Soler
Honored Contributor

Re: How to keep special meaning "*" inside a script

Hi, Manel

I resolved a similar question in
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xfc075ec05a7ad711abdc0090277a778c,00.html

try this:

if (ls /tmp/ffin* > /dev/null 2>&1) ; then echo Hi ; fi

Saludos
Frank.

Linux?. Yes, of course.
S.K. Chan
Honored Contributor

Re: How to keep special meaning "*" inside a script

I don't think it allows multiple file test. One other way is this ..
..
if (($(ls /tmp/ffin*|wc -l) > 1))
...
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to keep special meaning "*" inside a script

Hi (again):

Do you mean that you want to disable or enable globbing (file name generation). If so:

set -f

...disables shell file name generation.

set +f

...enables shell file name generation.

You can certainly choose the option you want inside a script to make it differ (or not) from your shell environent.

Regards!

...JRF...
Leif Halvarsson_2
Honored Contributor

Re: How to keep special meaning "*" inside a script

Hi,
You perhaps want to do something if there is any files. Try the following:

for file in `ls /tmp/ffin* 2>/dev/null`
do
echo $file
done
Mike Miller_8
Regular Advisor

Re: How to keep special meaning "*" inside a script

Hi,

Try the following :


if [ -f /tmp/ffin* ]; then
echo 'Found files'
fi

regards,

= Mike =
Jdamian
Respected Contributor

Re: How to keep special meaning "*" inside a script

I agree with James R. Ferguson.

If filename generation is active, shell will replace your original line when it is going to be executed:

if test -f /tmp/ffin*

for

if test -f /tmp/ffin02.txt /tmp/ffin09 /tmp/ffin.old

if there are filenames that match the pattern /tmp/ffin*

If not shell will keep the original line:

if test -f /tmp/ffin*

and test will check if /tmp/ffin* file exist (test will not expand the metacharacter *).

Thus, I think that, if file name generation is on, your script would run fine.

Re: How to keep special meaning "*" inside a script

Thanks a lot, guys!
I wanted to enable file name generation. The problem was that I had the next parameter at the begin of the script:

#! /bin/sh -f

Next time, I'll send you the full script for detailed analysis ;-)