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 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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-08-2005
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
* 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
  #2 (permalink)  
Old 12-08-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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 (permalink)  
Old 12-08-2005
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
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
  #4 (permalink)  
Old 12-08-2005
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
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
  #5 (permalink)  
Old 12-08-2005
grasper grasper is offline
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
  #6 (permalink)  
Old 12-08-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #7 (permalink)  
Old 12-08-2005
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
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
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 On




All times are GMT -4. The time now is 05:29 PM.


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-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0