Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
Registered User
 

Join Date: Jun 2007
Posts: 2
help with variable substitution in case statement

I have a script where I take input from user and see if it falls in list of valid entry.

---------------
#!/bin/ksh
VALID_ENTRIES="4|5|6"

echo "enter your choice"
read reply

IFS="|"
echo "valid entries are $VALID_ENTRIES"
case "$REPLY" in
Q|q ) IFS="$IFS_SAVE";return ;;
$VALID_ENTRIES )
IFS="$IFS_SAVE"
echo "The entry is valid";;
*) echo "invalid entry"
esac

-------------------------------------

Now even if user enters a value from 4,5,6 I always get output as invalid entry.

The value of VALID_ENTRIES doesn't seem to get substituted properly.

However if in case statement instead of $VALID_ENTRIES I directly write 4|5|6 for testing purpose it works.

So this shows that exact substitution is not made.
Can somebody help me with this?
Sponsored Links
  #2 (permalink)  
Old 11-20-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 779
Hi.

If you were using bash, you could use:

1) special expression notation feature
Code:
@($VALID_ENTRIES)) ... ;;

2) eval " ... entire case structure ..."
3) the literal, not the variable

The first does not work with ksh because it does not have the feature (nor built-in shopt, which is also required), so that leaves the second and third.

The second might require some re-writing, because you need to provide eval with a string of the entire case statement. That would normally be done with double quotes to allow the variable to be expanded. You are already using double quotes, so that would need to be fixed.

In this specific situation, I would use the literal.

There may be features in ksh which allows you to use the variable, but I didn't try anything other than the choices above. Perhaps a ksh expert will stop by with additional advice.

Best wishes ... cheers, drl
  #3 (permalink)  
Old 11-20-2008
Registered User
 

Join Date: Jun 2007
Posts: 2
Here I cannot use literal because the values are obtained at runtime and from that the user has to make a choice.
Also I did not understand how to use the second option.
  #4 (permalink)  
Old 11-20-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 779
Hi.

Like so:

Code:
#!/bin/bash -

# @(#) s2       Demonstrate variable in case, eval, extglob (bash).

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo
echo " Results:"

x="a|b|c|d"
echo " x is :$x:"
echo " Desiring yes, but getting no:"

case a in
  $x) echo yes;;
  *) echo no;;
esac

echo
echo " Expecting yes:"
case a in
  a|b|c|d) echo yes;;
  *) echo no;;
esac

echo
echo " Various features, expecting yes (echo included):"
shopt -s extglob
case a in
  @($(echo $x))) echo yes;;
  *) echo no;;
esac

echo
echo " Various features, expecting yes (echo omitted):"
shopt -s extglob
case a in
  @($x)) echo yes;;
  *) echo no;;
esac

echo
echo " With eval:"
eval "case a in
  $x) echo yes;;
  *) echo no;;
esac "

exit 0

Producing:

Code:
% ./s2

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Results:
 x is :a|b|c|d:
 Desiring yes, but getting no:
no

 Expecting yes:
yes

 Various features, expecting yes (echo included):
yes

 Various features, expecting yes (echo omitted):
yes

 With eval:
yes

The last illustration uses eval ... cheers, drl
  #5 (permalink)  
Old 11-20-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 779
Hi.

I just ran the script s2 (above) with ksh 93s+, and the case selector construction:
Code:
@($variable)) ... ;;

worked correctly. So if you have that version, you should be OK. It does not work with pdksh 5.2.14 99/07/13.2

See man ksh for details (section File Name Generation.) ... cheers, drl
  #6 (permalink)  
Old 11-22-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 779
Hi.

With ksh 93+, a solution with IFS="|" also works.

Note that in the original, IFS="$IFS_SAVE" seems to be done without the IFS_SAVE variable being first set... cheers, drl
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to convert value in a variable from upper case to lower case manmeet Shell Programming and Scripting 2 11-13-2008 02:22 PM
If or Case Statement Ernst UNIX for Dummies Questions & Answers 1 09-24-2008 06:00 PM
Using variable in case statement fialia Shell Programming and Scripting 2 05-12-2008 05:54 AM
case statement bkan77 Shell Programming and Scripting 5 09-11-2007 06:54 PM
Case Statement Zeta_Acosta Shell Programming and Scripting 19 04-06-2004 05:16 PM



All times are GMT -4. The time now is 12:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0