* character evaluating too soon - Help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting * character evaluating too soon - Help!
# 8  
Old 12-08-2005
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
# 9  
Old 12-08-2005
Hi Bakunin,

Thanks for your reply. The pattern may not always be a * so I could not run sed on it every time. I cannot check to see if the pattern is a * as it has already been evaluated! Smilie

Cheers
Helen
# 10  
Old 12-08-2005
Hi Helen

I think the issue with variable-expansion and noglob is only a problem when doing your 'eval'. Setting noglob to on shouldn't affect it otherwise. I think I'm roughly using your code-structure in the following:-

#!/bin/ksh

function fn_read_config {

p_search_item=$1
p_iteration=$2

grep ^"$p_search_item"_"$p_iteration" ./paramfile >/dev/null
v_search_return_code=$?


if [ $v_search_return_code -eq 0 ]
then
v_search_return=`grep ^"$p_search_item"_"$p_iteration" ./paramfile`
set -o noglob
v_search_line=`print ${v_search_return#*=}`
echo "PATTERN $v_search_line"
set +o noglob
return 0
fi
}

v_search=pattern
v_iter=002

fn_read_config "$v_search" "$v_iter"

set -o noglob
echo "PATTERN $v_search_line"
set +o noglob


And I'm still able to get the output:-

PATTERN *
PATTERN *


I suppose it could depend on how you were using the resulting variable value, but I'm guessing it could still be possible by manipluating noglob.

Cheers
# 11  
Old 12-08-2005
Maybe i don't get your point, but in the code snippet you provided i see several occasions where a quote would help. I tried the following code and it worked for every special char i tried in the input file - even without setting noglob. It should cover all the operations you used in your script save for the eval:

Code:
#!/bin/ksh

function func {

typeset var1="$1"
typeset var2="$2"

print - "Output from func(): var1=\"$var1\"\tvar2=\"$var2\""

return 0
}

# main()

typeset mvar1=""
typeset mvar2=""

cat input_file_with_codes | while read line ; do
     mvar1="$(print - "$line" | sed 's/  */ /g' | cut -d' ' -f1)"
     mvar2="$(print - "$line" | sed 's/  */ /g' | cut -d' ' -f2)"
     print - "mvar1=\"$mvar1\"\tmvar2=\"$mvar2\""
     func "$mvar1" "$mvar2"
done

exit 0

To write a small sed function which escapes every special character for exactly the eval should be no problem:

Code:
#!/bin/ksh

function escape_it {
typeset input="$1"
typeset output=""

output="$(print - "$input" | sed 's/[*?|]/\\&/g')"
print - "$output"

return 0
}

# main()

# ... do whatever ...

tmpvar="$(escape_it "$var")"
eval $tmpvar

# ... rest of your code ...

exit 0

# 12  
Old 12-13-2005
MySQL

Hi Bakunin and Grasper,

Thank-you both for your help. In the end I pre-parsed the config file with the sed statement to escape any special characters. I then used the resulting output as the config file. This worked fine.

Many thanks for all your time and patience!
Cheers
Helen Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Comparing and Evaluating

Hi Guys, Good day ULF :) I hope you can help me again with my problem. I have a file which looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server3 Dec 18 21:31:12 AHM04 nseventmgr: EVENT-SET:... (4 Replies)
Discussion started by: rymnd_12345
4 Replies

2. Shell Programming and Scripting

Evaluating a variable

Does anyone know of a way to force a variable name held in another variable to return the value of the first variable? Best if I give an example, that does not work: /usr/local/bin >cat mike.sh NUM1ref=16 NUM2ref=32 echo "==============" for VAR in NUM1 NUM2 do XXXX=${VAR}ref echo $XXXX... (4 Replies)
Discussion started by: mikejordan
4 Replies

3. Shell Programming and Scripting

if condition not evaluating as expected

Hi all, Thanks in advance for your time. I have a data file like this: 1 7.465753425 2 8.980821918 1 1.717808219 1 6.550684932 0 5.432876712 I wish to write a bash script to check both columns and output a 1 if col1==1 AND col2<3. Otherwise I want to output a 0. In the above... (5 Replies)
Discussion started by: jem8271
5 Replies

4. Shell Programming and Scripting

$$# is evaluating to 1 when no value

I have the following in my makefile: RESULT=`../${TOOLS_ROOT_PATH}/ext_tools.sh 11`; \ set $$RESULT > tMp; \ rm tMp; \ if ; then \ echo copying external-local tool: $< \($$*\); \ mkdir -p ${EXTERNAL_LOCAL_BIN_DIR}/$<; \ cp -f... (4 Replies)
Discussion started by: jake_ryan
4 Replies

5. Shell Programming and Scripting

K Shell evaluating value to a variable

Hi, I have the following requirement. V="First" R="V" echo $$R The output should be First. How do i achieve this. how do we evaluate the $R and evaluate it to $V as $R contains V and $V is First. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

6. Shell Programming and Scripting

Evaluating Decimal values

How can I evaluate a decimal value in an if statement? echo "Enter limit:" read limit (enter a decmal value, ie: 2.5) decimallimit=`echo $limit+0|bc|quit` echo $decimallimit if then echo $decimallimit else echo "failed" fi (4 Replies)
Discussion started by: larrys721
4 Replies

7. UNIX for Dummies Questions & Answers

evaluating date +%m

how do i evaluate the value of date if ( $(date +%m) > 8 ) then FY_STAMP=FY$(echo $(($(date +%Y) + 1)) | cut -c3-4) else FY_STAMP=FY$(date +%y) fi i want this to make the FY_STAMP increment by 1 if the month is september and up. but cant seem to make it work (3 Replies)
Discussion started by: rsf01
3 Replies

8. UNIX for Dummies Questions & Answers

evaluating params

Hi all, I ve a script like.... TBL=employee sql=`cat abhi.sql` \\ abhi.sql contains ------- select a from $TBL echo $TBL echo $sql SQL=`echo $sql` echo $SQL now i want SQL as select a from employee and as select a from $TBL How can I achieve this? Help appriciated (3 Replies)
Discussion started by: abzi
3 Replies

9. UNIX for Dummies Questions & Answers

evaluating variables

Hi I have a script in which I have several variables var1 var2 var3 var4 etc...... and field1 field2 field3 field4 etc....... The script similar to this: (6 Replies)
Discussion started by: Bab00shka
6 Replies

10. UNIX for Dummies Questions & Answers

evaluating for a number

I apologize for the simple question but can someone please help me with how to evaluate a number? I will be reading in a file and if a number is >= 100000000, I will do something, if not, I will exit the if statement. Thanks in advance (1 Reply)
Discussion started by: hedrict
1 Replies
Login or Register to Ask a Question