The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Evaluating Decimal values larrys721 Shell Programming and Scripting 4 06-02-2008 05:31 PM
evaluating date +%m rsf01 UNIX for Dummies Questions & Answers 3 03-03-2006 01:48 AM
evaluating params abzi UNIX for Dummies Questions & Answers 3 11-22-2005 08:40 PM
evaluating variables Bab00shka UNIX for Dummies Questions & Answers 6 10-07-2004 02:33 AM
evaluating for a number hedrict UNIX for Dummies Questions & Answers 1 11-09-2003 03:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-08-2005
Registered User
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 124
* character evaluating too soon - Help!

I have a user defined configuration file, which could contain the following type of entries:

directory_001=/a/directory/structure
pattern_001=fred*
pattern_002=*

I have a script which reads the file generically which will loop round

loop 1
genvar=”directory”
iteration=”001”

eval varcontents=”\$genvar”_”$iteration”

expected results varcontents=/a/directory/structure

loop 2
genvar=”pattern”
iteration=”001”

eval varcontents=”\$genvar”_”$iteration”

expected results varcontents=”fred*”

The above appears to work fine, however, the problem arises with loop 3

loop3
genvar=”pattern”
iteration=”002”

eval varcontents=”\$genvar”_”$iteration”

expected results varcontents=”*”
actual results are an error pattern_002: parameter not set

I realise what is happening and can resolve the problem by using pattern_002=\* in the config file. The problem is that the file is user created and it may be that someone will forget the backslash before the *.

Can someone suggest a way to resolve this so that a * without a backslash can be used in the configuration file please?

Many thanks for your help,
Helen
Reply With Quote
Forum Sponsor
  #2  
Old 12-08-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
To preserve characters with a special meaning to the shell you have to protect them - usually by quoting them. Without knowing your actual code: if using \* instead of * solves your problem chances are that somewhere in your script you use your variable containing the asterisk without quoting: $variable instead of "$variable".

I suggest you look carefully through your code for such occasions, it might very well solve your problem.

Hope this helps.

bakunin
Reply With Quote
  #3  
Old 12-08-2005
Registered User
 

Join Date: Sep 2005
Posts: 45
You could set -o noglob before the eval and then set +o noglob after.
Reply With Quote
  #4  
Old 12-08-2005
Registered User
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 124
Hi Bakunin,

The issue, I believe, is the eval statement. However I need to use the eval statement to get the contents of the variable.

Cheers
Helen
Reply With Quote
  #5  
Old 12-08-2005
Registered User
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 124
Hi Grasper,

I tried that but it gets the same error. I'm not sure what noglob does.

Out of interest it HPUX 11.00 and Korn shell.

Cheers
Helen
Reply With Quote
  #6  
Old 12-08-2005
Registered User
 

Join Date: Sep 2005
Posts: 45
Apologies, I may have been a bit hasty in suggesting that in that way. noglob prevents wildcard characters from being expanded.

I don't know exactly how you'll be extracting your variables from the file or using them in your script, but here's a bit of code which uses eval in a similar way to the way you seem to need it and allows the asterisk character to be preserved:-

#!/bin/ksh

set -o noglob
pattern_002=*
set +o noglob
echo "PATTERN $pattern_002"

genvar="pattern"
iteration="002"

varc=$"$genvar"_"$iteration"
eval varcontents=$varc

set -o noglob
echo $varcontents
set +o noglob


When I run this I get the output:-

PATTERN *
PATTERN *

Hope that's of some use
Reply With Quote
  #7  
Old 12-08-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
Hi Helen,

the eval-statement will surely leave the asterisk unprotected. In this case the only way to deal with it is to escape it in the script itself:

'sed s/\*/\\&/g'

Hope this helps even better. ;-))

PS: "noglobber" or the equivalent parameter "-f" prevents filename substitution. The problem is that an "eval ..." might take precedence (i have never tested that).

bakunin
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0