[ale] Baffled on doing CASE statement with a Variable list of values

Lightner, Jeff JLightner at dsservices.com
Mon Aug 3 13:51:55 EDT 2015


Issue is the “;;” on the last line of the case (above the esca).  That is a continuation line for case statements so it is confused.  Remove that and your first test will work as designed.

You can’t set a variable like LIST the way you show in a later example.  You CAN set an “array” but wouldn’t use pipe as the separator usually.

Were it me and I had a file like the one you were describing I’d use a while loop to process it rather than an array (assuming I didn’t need to reparse the list after getting the initial value.

e.g.
cat allow.list |while read REMOTE_ADDR
do
   case $REMOTE_ADDR in
          one|two|three) echo "I like $REMOTE_ADDR" ;;    # Picks this one.
          *) echo "I do NOT like $REMOTE_ADDR"
  esac
done

The above while loop assumes that your allow.list has only one item on each line which is in fact a remote address in the form you expect (e.g. one, two, three, four, five etc…)   The default separator for the while is white space so if you did have multiple items per line and only wanted the first one on the line you have to set two variables – it will assign the first item on the line to the first variable and everything else the next variable (which can then be ignored in the rest of the script.)  Similarly if you had 4 (or more) items per line and wanted the first and third you’d have to set 4 variables so it would assign the first 3 items to their own variables and the remaining to a final variable.   Here again you can use the first and third variables in the script and ignore the second and fourth.

Jeffrey C. Lightner
Sr. UNIX/Linux Administrator

DS Services of America, Inc.
2300 Windy Ridge Pkwy
Suite 600 N
Atlanta, GA  30339-8461

P: 678-486-3516
C: 678-772-0018
F: 678-460-3603
E: jlightner at dsservices.com

From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of Neal Rhodes
Sent: Monday, August 03, 2015 12:04 PM
To: Atlanta Linux Enthusiasts
Subject: [ale] Baffled on doing CASE statement with a Variable list of values

Exactly how does not assign a list of values separated by pipe to use that variable later on in a script and make a CASE statement happy?
The script below shows a variety of attempts.

Ultimately, we're aiming to grab the list from a file, eg "LIST="`cat allow.list`"

REMOTE_ADDR=one

case $REMOTE_ADDR in
        one|two|three) echo "I like $REMOTE_ADDR" ;;    # Picks this one.
        *) echo "I do NOT like $REMOTE_ADDR" ;;
esac

LIST="one|two|three"
case $REMOTE_ADDR in
        $LIST) echo "But now I like $REMOTE_ADDR" ;;
        *) echo "But now I do NOT like $REMOTE_ADDR" ;; # Picks this one.
esac

case $REMOTE_ADDR in
        "$LIST") echo "And I like $REMOTE_ADDR" ;;
        *) echo "And I do NOT like $REMOTE_ADDR" ;;     # Picks this one.
esac

LIST="one"
case $REMOTE_ADDR in
        "$LIST") echo "Yet I like $REMOTE_ADDR" ;;      # Picks this one.
        *) echo "Yet I do NOT like $REMOTE_ADDR" ;;
esac

LIST="one\|two\|three"
case $REMOTE_ADDR in
        $LIST) echo "However now I like $REMOTE_ADDR" ;;
        *) echo "However now I do NOT like $REMOTE_ADDR" ;;     # Picks this one.
esac

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20150803/eb8a58fe/attachment.html>


More information about the Ale mailing list