![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
* 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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
You could set -o noglob before the eval and then set +o noglob after.
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |