Variable indirection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable indirection
# 8  
Old 05-26-2012
Quote:
Originally Posted by bakunin
It is indeed possible, but you have to be aware about the different number of times the line gets evaluated with and without eval.

If you use eval var='$(...)' to execute it you should probably print it with the same number of evals. But to be honest i have a feeling that your whole script is not planned very well because the structures you use are looking wrong anyway. I may be wrong, but if you post your whole script and tell us what you want to achieve we could find a better way to do it than with such crooked measures.

I hope this helps.

bakunin
My script is not doing more than the example I gave.

Code:
#!/bin/bash
#
My_CMD1="some texte and parameter $PARAM1 $PARAM2  without quote"
# Want to print to screen
# for debugging
echo "The command is : $MY_CMD1"
# now execute it
$MY_CMD1
# OK no problem with this case
#
#Trying this one
#
PARAM1="No"
PARAM2="Not installed"
PARAM3="FreeNX"
PARAM4="My_freenx_Directory"
PARAM5="/tmp/install_test/freenx.log"
#
zypper info -r $PARAM4 > $PARAM5
#
# We try to verify if the log file contains the information
# that prove that the software is not installed
# 4 parameters are nedeed
#
# For good understanding I replace all quote (simple or double) with this : %
# and each time the single quote ( ' )  is necessary I put it in the right place
# So the next line is not the real one
#
MY_CMD2=%CNT=$( grep -c  -e  %some_text1 $PARAM1%  -e  %some_text2 '$PARAM2'%  -e  %some_text3-1 $PARAM3-1 some_text3-2 '$PARAM4'%  -e %some_text4% $PARAM4%    $PARAM_5 ) %
#
# If the software is not installed, CNT = 4
#
# now try to print the command to screen ( very important for debugging )
echo "The command is : $MY_CMD2"
# now execute it
$MY_CMD2
# Problem with this case
#

I get this working with 2 conditions :
1°)I must separate the variable content (because of ( " ) and ( ' ) ) for printing or for execute
2°)I use eval for execute --> eval $MY_CMD2

Code:
#!/bin/bash
#
My_CMD1="some texte and parameter $PARAM1 $PARAM2  without quote"
# Want to print to screen
# for debugging
echo "The command is : $MY_CMD1"
# now execute it
$MY_CMD1
# OK no problem with this case
#
#Trying this one
#
PARAM1="No"
PARAM2="Not installed"
PARAM3="FreeNX"
PARAM4="My_freenx_Directory"
PARAM5="/tmp/install_test/freenx.log"
#
zypper info -r $PARAM4 > $PARAM5
#
# We try to verify if the log file contains the information
# that prove that the software is not installed
# 4 parameters are nedeed
#
# For good understanding I replace all quote (simple or double) with this : %
# and each time the single quote ( ' )  is necessary I put it in the right place
# So the next line is not the real one
#
# MY_CMD3 is use for echoing to the screen and have to put some more single and double quote.
#
MY_CMD3=%CNT=$( grep -c  -e  %some_text1 $PARAM1%  -e  %some_text2  '$PARAM2'%  -e  %some_text3-1 $PARAM3-1 some_text3-2 '$PARAM4'%  -e  %some_text4% $PARAM4%    $PARAM_5 ) %
#
# MY_CMD4 use some different single and double quote ,where single quote are mandatory.
#
$MY_CMD4=%CNT=$( grep -c  -e  %some_text1 $PARAM1%  -e  %some_text2  %%'$PARAM2'%%  -e  %some_text3-1 $PARAM3-1 some_text3-2  %%'$PARAM4'%%  -e  %some_text4% $PARAM4%    $PARAM_5 ) %
 #
# now try to print the command to screen ( very important for debugging )
echo "The command is : $MY_CMD3"
# now execute it
eval $MYCMD4
# No Problem with this case
#

CMD_3 -->
Code:
CMD='CNT=$(  grep -c -e  "Non-option program arguments: '"'$PACKAGE_1'"'"  -e  "Checking whether to refresh metadata for '$REPO_1'" -e  "Selecting '"'FreeNX-0.7.3-22.1.x86_64'"' from repository '"'$REPO_1'"' for installation."    '$LOG_FILE_1' ) '

CMD_4 -->
Code:
CMD='CNT=$(  grep -c -e  "Non-option program arguments: '"'"'$PACKAGE_1'"'"'"  -e  "Checking whether to refresh metadata for $REPO_1" -e  "Selecting '"'"'FreeNX-0.7.3-22.1.x86_64'"'"' from repository '"'"'$REPO_1'"'"' for installation."  $LOG_FILE_1 ) '


Last edited by Scrutinizer; 05-27-2012 at 02:06 AM.. Reason: code tags instead of quote tags
# 9  
Old 06-04-2012
Quote:
Originally Posted by Scrutinizer
If you try double quotes instead of single quotes, I think it becomes a bit more straight forward, you should then escape the double quotes with a backslash, plus the $-signs that you wish to protect from evaluation during assignment:
Code:
CMD="CNT=\$( grep -c -e \"Non-option program arguments: '$PACKAGE_1'\" -e \"Checking whether to refresh metadata for $REPO_1\" -e \"Selecting '$PACKAGE_1-3.5.0-7.x86_64' from repository '$REPO_1' for installation.\" \"$LOG_FILE_1\" )"

Code:
eval CMD$

Works great if quoted correctly as you show me.

The thread is closed.

Thank you every body for your help and for your patience.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question