How to parse the variable in .mk file?

Hi all,
I am passing a variable in .mk file as

NEED="TEST=Name WORK=Ps DEL=let"

I need to echo and export each variable like TEST, WORK. DEL

TEST=Name 
WORK=Ps 
DEL=let

We have to parse the NEED variable which may contain many elements like TEST, DEL& etc..

Any idea on this please. I am using Ubuntu

--Regards,
ricky

If:
1) your input is guaranteed to be properly delimited by single blanks always,

2) the item=value pairs will contain no further spaces

then the following may work, provided that you use a common shell (and not Ubuntus standard shell dash ). Notice that you may need to add additional tests, trims and other measures to this skeleton because real-world input might not be as well-behaved as is ideal:

input="$1"
buf=""

while [ -n "$input" ] ; do
     buf="${input##* }"               # these two lines chop off 
     input="${input% ${buf}}"         # one item=value pair after the other

     echo "$buf"
     eval export "$buf"
done

Note also that the input string is tokenized backwards, so that the input a=b c=d e=f will result in:

export e=f
export c=d
export a=b

in this order.

I hope this helps.

bakunin

Hi,
When I implemented this on my Makefile, I got errors

while [ -n "nput" ] ; do
/bin/bash: -c: line 1: syntax error: unexpected end of file

code:

	
input=NEED="TEST=Name WORK=Ps DEL=let"
all:
                while [ -n "$(input)" ] ; do
		buf="${input##* }"               # these two lines chop off 
		input="${input% ${buf}}"         # one item=value pair after the other
		echo "$buf"
		eval export "$buf"
	done

Do you have any idea on this and changes needed to get the output as needed please.

yes, and this probably gives a hint:

Notice that "$input" is not "$input" any more but "nput". I suppose half of the code (which is basically all variable expansion) is interpreted away by the make -utility itself. Also notice that make will call every command in a separate shell, therefore multi-line commands/scripts like the one i gave you will not work this way, because they need to maintain context from one line to the next.

Put the code into a script file and call that instead of directly including it into a make-rule. Furthermore, this:

looks different than what you gave as example input and will fail with the code i wrote anyway. Have you read the caveats i included in my first answer?

I hope this helps.

bakunin

Thanks Bakunin,
But When I tried your code in the script its not stopping. It keeps on printing TEST=Name infinitely. Could you give some correction on this, please

That is - as i have said already - because the format of your input string is not in the same format as in your post #1. This:

input=NEED="TEST=Name WORK=Ps DEL=let"

does not make any sense syntactically and it is not of the form item1=value1 item2=value2 item3=value3 ... which would be necessary for my algorithm to work - as i have already stated in my first answer!

Give me a REAL EXAMPLE OF WHAT YOU WANT TO PROCESS and i will give you a real solution, because what you have shown me is not only different than what you first asked to process, it will also generate a syntax error if you, to quote yourself:

because

input=NEED="TEST=Na...

is not a syntactically correct shell statement. Otherwise, tell me what the expected outcome should be. Show me the statement my script should produce from this input.

bakunin

Hi,
I am really sorry for the confusions, I tried like this way

hacker@hacker:~$ cat test.sh 
#!/bin/bash

NEED="TEST=Name WORK=Ps DEL=let"
buf=""

while [ -n "$NEED" ] ; do
     buf="${NEED##* }"               # these two lines chop off 
     NEED="${NEED% ${buf}}"         # one item=value pair after the other

     echo "$buf"
     eval export "$buf"
done

where I got my output as
TEST=Name
TEST=Name
TEST=Name
TEST=Name
``
``.
``
The output is not ending,
My Real Need:
where I have declared the variable in my MAKEFILE

NEED="TEST=Name WORK=Ps DEL=let"

with the same format with single space delimiting but it not just only three, it can be 'n' number.
Now I need to parse each item with its value and export them for further usage in the same MAKEFILE , as you said a=b, c=d , e=f , g=h etc..

Like say, I have done parsing

TEST=Name
WORK=Ps
DEL=let

In the same MAKEFILE, I have to make use of this WORK, where

echo $(WORK) 
Ps

should print. I hope I cleared the context of my need.
Any help please, I am rolling my head to get this done.

Regards,
ricky

How about:

NEED="TEST=Name WORK=Ps DEL=let"


for X in $NEED
do
       echo $X
done

Thanks Corona,
I suppose to export those parsed individual variables and use them in the MAKEFILE. Like

echo $WORK 
Ps

echo $TEST
Name 

echo $DEL
let

Any inputs please,
I tried adding

  eval export "$X"

but I can't see in printenv

---------- Post updated at 08:07 PM ---------- Previous update was at 11:16 AM ----------

Any suggestions please,
I tried the way of using sed

	sed 's/ /\n/g' <<< $NEED

which gives output as
TEST=Name
WORK=Ps
DEL=let

but, I can't export the values, any idea to export the values in Makefile

Isn't that from your makefile? Why not just set the variables you want to set?

"Any inputs please" and / or "Any suggestions please" are not too popular demands in these forums, esp. if repeated several times. If people know or find an answer to - esp. somewhat unclear like your - requests, they will come up with it now or sometime - I don't think urging them like above will make them post sth. head over heels.
If I only knew what the meaning of "export the values in Makefile" were, I might come up with a proposal. To me, a makefile is just textual data, a prescription of conditions and dependencies to be read and executed by the make tool. IF a variable is referenced in a makefile , and IF that variable has been exported in the parent shell, THEN make will be able to process the expanded value of the variable.