All of lore.kernel.org
 help / color / mirror / Atom feed
* [OT] Strange bash phenomenon
@ 2006-02-11 13:44 John A. Sullivan III
  2006-02-13 15:50 ` Pablo Sanchez
  0 siblings, 1 reply; 4+ messages in thread
From: John A. Sullivan III @ 2006-02-11 13:44 UTC (permalink / raw)
  To: Netfilter users list

Hello everyone.  This is a little off topic but I find it both
perplexing and amusing.  I've been spending the last few days enabling
support for CyberGuard security devices for the ISCS network security
management platform.  It has gone very well with the new firmware and
has given us a near off the shelf solution.

However, in one of the scripts for automated distribution of the rules,
it checks for the presence of existing iptables files.  I originally
thought I could do something like:

if [ -f iptables* ];then

But the shell expands all the iptables file names and creates an error
when passing that many arguments to the test.

So I then thought I could do:

FILES=/etc/config/iptables*
if [ -f ${FILES%% *} ];then
        cp /etc/config/iptables* /etc/config/PEPBackup/ 2>/dev/null
        if [ $? -ne 0 ]; then
                echo '[PEP Update Error]' Could not backup up PEP
configuration files >&2
                exit 1
        fi
fi

I'd trim off everything after the first space and test for that single
file.  However, it doesn't work.  ${FILES%% *} expands to include all
the file names.  On the other hand, the version below works!


FILES=$(echo /etc/config/iptables*)
if [ -f ${FILES%% *} ];then
        cp /etc/config/iptables* /etc/config/PEPBackup/ 2>/dev/null
        if [ $? -ne 0 ]; then
                echo '[PEP Update Error]' Could not backup up PEP
configuration files >&2
                exit 1
        fi
fi

Any ideas why the latter one works fine but the other does not? Just
curious - John
-- 
John A. Sullivan III
Open Source Development Corporation
+1 207-985-7880
jsullivan@opensourcedevel.com

If you would like to participate in the development of an open source
enterprise class network security management system, please visit
http://iscs.sourceforge.net



^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [OT] Strange bash phenomenon
  2006-02-11 13:44 [OT] Strange bash phenomenon John A. Sullivan III
@ 2006-02-13 15:50 ` Pablo Sanchez
  2006-02-13 16:56   ` John A. Sullivan III
  0 siblings, 1 reply; 4+ messages in thread
From: Pablo Sanchez @ 2006-02-13 15:50 UTC (permalink / raw)
  To: Netfilter users list



> -----Original Message-----
> From: netfilter-bounces@lists.netfilter.org
> [mailto:netfilter-bounces@lists.netfilter.org]On Behalf Of John A.
> Sullivan III
> Sent: Saturday, February 11, 2006 8:45 AM
> To: Netfilter users list
> Subject: [OT] Strange bash phenomenon
> 
> Case #1
> -------
> FILES=/etc/config/iptables*
>
> [ snipped ]
>
> Case #2
> -------
> FILES=$(echo /etc/config/iptables*)
> 
> Any ideas why the latter one works fine but the other does not? Just
> curious.

Hi John,

It has to do when the shell does the expansion.  In the first case when you assign 'FILES=', you're assigning it the string and it's not expanded by the shell.  If you want to change the behavior, you can do this:

	FILES=${/etc/config/iptables*}

btw, Ilove the ${FILES%% *} trick.  I didn't know about it.  The shell is quite nifty.

-pablo



^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [OT] Strange bash phenomenon
  2006-02-13 15:50 ` Pablo Sanchez
@ 2006-02-13 16:56   ` John A. Sullivan III
  2006-02-13 18:07     ` Pablo Sanchez
  0 siblings, 1 reply; 4+ messages in thread
From: John A. Sullivan III @ 2006-02-13 16:56 UTC (permalink / raw)
  To: pablo; +Cc: Netfilter users list

On Mon, 2006-02-13 at 10:50 -0500, Pablo Sanchez wrote:
> > -----Original Message-----
> > From: netfilter-bounces@lists.netfilter.org
> > [mailto:netfilter-bounces@lists.netfilter.org]On Behalf Of John A.
> > Sullivan III
> > Sent: Saturday, February 11, 2006 8:45 AM
> > To: Netfilter users list
> > Subject: [OT] Strange bash phenomenon
> > 
> > Case #1
> > -------
> > FILES=/etc/config/iptables*
> >
> > [ snipped ]
> >
> > Case #2
> > -------
> > FILES=$(echo /etc/config/iptables*)
> > 
> > Any ideas why the latter one works fine but the other does not? Just
> > curious.
> 
> Hi John,
> 
> It has to do when the shell does the expansion.  In the first case when you assign 'FILES=', you're assigning it the string and it's not expanded by the shell.  If you want to change the behavior, you can do this:
> 
> 	FILES=${/etc/config/iptables*}
> 
> btw, Ilove the ${FILES%% *} trick.  I didn't know about it.  The shell is quite nifty.
> 
> -pablo
> 
> 
Thanks.  That makes sense, i.e., unexpanded assignment.  By the way, if
I try FILES=${/iptables*}, I get:

./testbash2: line 2: ${testb*}: bad substitution

But, at least I know how to make it work and understand why the first
approach failed - John
-- 
John A. Sullivan III
Open Source Development Corporation
+1 207-985-7880
jsullivan@opensourcedevel.com

Financially sustainable open source development
http://www.opensourcedevel.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [OT] Strange bash phenomenon
  2006-02-13 16:56   ` John A. Sullivan III
@ 2006-02-13 18:07     ` Pablo Sanchez
  0 siblings, 0 replies; 4+ messages in thread
From: Pablo Sanchez @ 2006-02-13 18:07 UTC (permalink / raw)
  To: Netfilter users list



> -----Original Message-----
> From: John A. Sullivan III [mailto:jsullivan@opensourcedevel.com]
> Sent: Monday, February 13, 2006 11:56 AM
> To: pablo@blueoakdb.com
> Cc: Netfilter users list
> Subject: RE: [OT] Strange bash phenomenon
> 
> By the way, if
> I try FILES=${/iptables*}, I get:
> 
> ./testbash2: line 2: ${testb*}: bad substitution
> 
> But, at least I know how to make it work and understand why the first
> approach failed 

Hi John,

The 'bad substition' error originates because there are no files which match the expression.

Given you're looking at trying to determine whether a directory has files you could either do it the way you've initially written up your code or use 'find'

The possible 'issue' with the original 'cp *' is if there was a boatload of files in the directory, you could get a shell expansion error.  Probably very unlikely.

One way to handle the corner case is to use find - see the execdir/exec option.

Cheers,
---
Pablo Sanchez - Blueoak Database Engineering, Inc
Ph:    819.459.1926          Toll free:  888.459.1926
Cell:  819.918.9731                Pgr:  pablo_p@blueoakdb.com
Fax:   603.720.7723 (US)           Fax:  514.371.1255 (Canada)



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-02-13 18:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-11 13:44 [OT] Strange bash phenomenon John A. Sullivan III
2006-02-13 15:50 ` Pablo Sanchez
2006-02-13 16:56   ` John A. Sullivan III
2006-02-13 18:07     ` Pablo Sanchez

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.