question about variable in case structure

question about variable in case structure

am 27.11.2007 17:11:25 von EdStevens

Platform: HP-UX 11 on Itanium

Given the following test script:

#!/bin/sh
toss()
{
for FPATH in `cat`
do
FILE=`basename $FPATH`
case $FILE in
# $DONTTOUCH )
lost+found|cntrldptn.dbf|bastille.rdo)
echo Ignoring: $FILE
;;
*)
echo Processing: $FILE
;;
esac
done
}
DONTTOUCH="lost+found|cntrldptn.dbf|bastille.rdo"
cd /home/stevense
find . -mtime +1 -print 2>&1 | toss
exit


If, in the case statement, I hard-code the first pattern as shown
above, everything works as expected and I get "Ignoring: bastille.rdo"
in my output.

If I replace the hard-coded patter with the variable $DONTTOUCH, files
that should hit this condition actually show up in the default
"Processing: bastille.rdo"

I'm not particularly wedded to one version over the other, but would
like to understand why the use of the variable doesn't work.

Re: question about variable in case structure

am 27.11.2007 17:31:48 von Icarus Sparry

On Tue, 27 Nov 2007 08:11:25 -0800, EdStevens wrote:

> Platform: HP-UX 11 on Itanium
>
> Given the following test script:
>
> #!/bin/sh
> toss()
> {
> for FPATH in `cat`
> do
> FILE=`basename $FPATH`
> case $FILE in
> # $DONTTOUCH )
> lost+found|cntrldptn.dbf|bastille.rdo) echo Ignoring: $FILE
> ;;
> *)
> echo Processing: $FILE
> ;;
> esac
> done
> }
> DONTTOUCH="lost+found|cntrldptn.dbf|bastille.rdo" cd /home/stevense
> find . -mtime +1 -print 2>&1 | toss
> exit
>
>
> If, in the case statement, I hard-code the first pattern as shown above,
> everything works as expected and I get "Ignoring: bastille.rdo" in my
> output.
>
> If I replace the hard-coded patter with the variable $DONTTOUCH, files
> that should hit this condition actually show up in the default
> "Processing: bastille.rdo"
>
> I'm not particularly wedded to one version over the other, but would
> like to understand why the use of the variable doesn't work.

With the variable it is as if you had used the pattern
lost+found\|cntrldptn.dbf\|bastille.rdo
i.e. the "|" characters are used as literals rather than giving different
choices.

It is just a matter of the oder in which things are done. You can use
"eval" as in

eval '
case $FILE in
'$DONTTOUCH') echo Ignoring: $FILE
;;
*)
echo Processing: $FILE
;;
esac'

to change the order if you need to.

Re: question about variable in case structure

am 27.11.2007 18:06:47 von EdStevens

On Nov 27, 10:31 am, Icarus Sparry wrote:
> On Tue, 27 Nov 2007 08:11:25 -0800, EdStevens wrote:
> > Platform: HP-UX 11 on Itanium
>
> > Given the following test script:
>
> > #!/bin/sh
> > toss()
> > {
> > for FPATH in `cat`
> > do
> > FILE=`basename $FPATH`
> > case $FILE in
> > # $DONTTOUCH )
> > lost+found|cntrldptn.dbf|bastille.rdo) echo Ignoring: $FILE
> > ;;
> > *)
> > echo Processing: $FILE
> > ;;
> > esac
> > done
> > }
> > DONTTOUCH="lost+found|cntrldptn.dbf|bastille.rdo" cd /home/stevense
> > find . -mtime +1 -print 2>&1 | toss
> > exit
>
> > If, in the case statement, I hard-code the first pattern as shown above,
> > everything works as expected and I get "Ignoring: bastille.rdo" in my
> > output.
>
> > If I replace the hard-coded patter with the variable $DONTTOUCH, files
> > that should hit this condition actually show up in the default
> > "Processing: bastille.rdo"
>
> > I'm not particularly wedded to one version over the other, but would
> > like to understand why the use of the variable doesn't work.
>
> With the variable it is as if you had used the pattern
> lost+found\|cntrldptn.dbf\|bastille.rdo
> i.e. the "|" characters are used as literals rather than giving different
> choices.
>
> It is just a matter of the oder in which things are done. You can use
> "eval" as in
>
> eval '
> case $FILE in
> '$DONTTOUCH') echo Ignoring: $FILE
> ;;
> *)
> echo Processing: $FILE
> ;;
> esac'
>
> to change the order if you need to.

Ahh, I see. Thanks.

Which leads to my next issue, but I'll post that under a new thread.