How to parse the variable in .mk file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse the variable in .mk file?
# 1  
Old 09-04-2017
How to parse the variable in .mk file?

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

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

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

Code:
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
# 2  
Old 09-05-2017
Quote:
Originally Posted by ricky-row
I am passing a variable in .mk file as

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

I need to echo and export each variable like TEST, WORK. DEL
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:

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

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

in this order.

I hope this helps.

bakunin
# 3  
Old 09-05-2017
Hi,
When I implemented this on my Makefile, I got errors

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

code:

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.
# 4  
Old 09-05-2017
Quote:
Originally Posted by ricky-row
Do you have any idea on this and changes needed to get the output as needed please.
yes, and this probably gives a hint:
Quote:
Originally Posted by ricky-row
Code:
while [ -n "nput" ] ; do
/bin/bash: -c: line 1: syntax error: unexpected end of file

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:

Quote:
Originally Posted by ricky-row
Code:
input=NEED="TEST=Name WORK=Ps DEL=let"

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
# 5  
Old 09-05-2017
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
# 6  
Old 09-05-2017
Quote:
Originally Posted by ricky-row
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:

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

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

Code:
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
# 7  
Old 09-05-2017
Hi,
I am really sorry for the confusions, I tried like this way

Code:
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
Code:
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
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

Help about parse the variable

I'm using bash the code is QEMU_CMD="qemu-system-x86_64 -smp 2 -m 512 $QEMU_PARAMETER -hda $GUEST_IMAGE -kernel $GUEST_KERNEL -append \"root=/dev/hda rw console=ttyS0,115200 ip=$IP_PARAMETER \" -nographic" echo "..............................." echo "qemu command is... (9 Replies)
Discussion started by: yanglei_fage
9 Replies

3. Shell Programming and Scripting

How to parse xml file in variable-string?

In the wake of the post: how-parse-following-xml-file Thank you for the very useful chakrapani response 302355585-post4 ! A close question. How to pass a file to xmllint in variable? For example, let it be: NEARLY_FILE='<?xml version="1.0" encoding="iso-8859-1"?><html><set label="09/07/29"... (0 Replies)
Discussion started by: OleM2k
0 Replies

4. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

5. Shell Programming and Scripting

Parse file and copy value to variable in sh script.

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is not constant (eg: it could be BR1234 or BR22233). And there is only 1 BRxxxxx in a file at a given... (6 Replies)
Discussion started by: script2010
6 Replies

6. Shell Programming and Scripting

Parse text file in shell & store to variable

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is in constant. And there is only 1 BRXXX in a file at a given time. .txt file: BR276828... (1 Reply)
Discussion started by: script2010
1 Replies

7. Shell Programming and Scripting

how to parse value of the variable

I have a variable which has a full path to the file, for example : A=/t1/bin/f410pdb Does anybody know the command to parce this variable and assign the result to 3 other variables so each subdirectory name will be in a new variable like this B=t1 C=bin D=f410pdb Many thanks -A (5 Replies)
Discussion started by: aoussenko
5 Replies

8. Shell Programming and Scripting

Parse String from a Variable

Hello, Is there a quick way to parse the values from a variable? The variable has the following sample input: TA= The values of the TA variable is not fixed/hardcoded Basically I need to get the IV_Test and PF_SAPP_FWK values. I created a script that first use sed to remove ,... (3 Replies)
Discussion started by: racbern
3 Replies

9. Shell Programming and Scripting

parse variable

I have a variable (it is a date actually -> 2007-01-03) which would be passed in as parameter, what I want is to parse in and put year, month, and day in separate variables, I have tried the following but doesn't work echo $dt | awk -F- '{print $1 $2 $3}' | read y m d Thanks in... (2 Replies)
Discussion started by: mpang_
2 Replies

10. Shell Programming and Scripting

parse a string variable

Hello all, need a little help. I have an input variable such as ARGV which equals something like /use/home/name/script/test.dat I need to be able to get just the "test.dat" (i.e. the file name) at the end of the directory and the directory can be anything and any length. To put it another... (3 Replies)
Discussion started by: methos
3 Replies
Login or Register to Ask a Question