Unix Shell - question about variable in case structure

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2007 > question about variable in case structure





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author question about variable in case structure
EdStevens

2007-11-27, 1:29 pm

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.
Icarus Sparry

2007-11-27, 1:29 pm

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.
EdStevens

2007-11-27, 1:29 pm

On Nov 27, 10:31 am, Icarus Sparry <use...@icarus.freeuk.com> wrote:
> On Tue, 27 Nov 2007 08:11:25 -0800, EdStevens wrote:
>
>
>
>
>
>
> 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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com