![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 and shell scripting languages 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 09:31 PM |
| evaluating date +%m | rsf01 | UNIX for Dummies Questions & Answers | 3 | 03-03-2006 04:48 AM |
| evaluating params | abzi | UNIX for Dummies Questions & Answers | 3 | 11-22-2005 11:40 PM |
| evaluating variables | Bab00shka | UNIX for Dummies Questions & Answers | 6 | 10-07-2004 06:33 AM |
| evaluating for a number | hedrict | UNIX for Dummies Questions & Answers | 1 | 11-09-2003 06:04 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
* 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 ![]() |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
Hi Grasper,
I like your thinking, the problem I have is that in trying to simplify the issue so I don't put pages of coding on here, is that the issue is missed! I tried your script and it works, however its in a slightly different context from mine. In my script, the pattern_002=* is within a configuration file and a generic search of the configuration file is the way the pattern is pulled back. The actual code from my script is a function which is launched as follows fn_read_config "$v_search" "$v_iter" 002 in this case v_search is pattern and v_iter is 002 function fn_read_config { p_search_item=$1 p_iteration=$2 grep ^"$p_search_item"_"$p_iteration" "$c_config_dir/$c_config_file" >/dev/null v_search_return_code=$? if [ $v_search_return_code -eq 0 ] then v_search_return=`grep ^"$p_search_item"_"$p_iteration" "$c_config_dir/$c_c onfig_file"` v_search_line=`print ${v_search_return#*=}` return 0 fi } from this I wanted v_search_line to be * I can't use the noglob when doing the search as I need the variables to be expanded. I hope you understand, I did want to keep this as simple as possible! Cheers Helen |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|