LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2006, 07:09 AM   #1
Ankit mittal
LQ Newbie
 
Registered: Sep 2005
Location: india
Posts: 11

Rep: Reputation: 0
getting rpoblem in assigning value to variable in if construct


Hi all,

i am trying to assignn value to varible in if construct.
but it does not work.
i have attched code snipshot for refernece.


ADMIN_LANG = de-de es-es es-ES fr-fr fr-CA it-it ja-ja ko-ko

LOCALE = ja-ja
admin = ADMIN
runtype = RUNTIME

abc : check

check:

( for x in $(ADMIN_LANG) ; do\
if [ $$x = "$(LOCALE)" ] ; then\
echo "enter in for loop" ;\
LANG_TYPE = $(admin);\
else \
LANG_TYPE = $(runtype);\
fi ;\
done ) ;

echo $(LANG_TYPE);


after that when i am trying to echo LANG_TYPE then it will show nothing.
its very basic thing but i am facing problem.
i have tried various combination to solve it but i am not succeded.
please take a look into it.


thanks,
Ankit
 
Old 01-03-2006, 12:02 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
It looks like you have a typo:

Quote:
if [ $$x = "$(LOCALE)" ] ;
$$ is a reserved variable (Process ID)

If you do have two $ symbols infront of the x try removing one.

What are you writing this in?

Suggest a slight re-write:

Code:
ADMIN_LANG = "de-de es-es es-ES fr-fr fr-CA it-it ja-ja ko-ko"
LOCALE = ja-ja
admin = ADMIN
runtype = RUNTIME

for x in ${ADMIN_LANG}
do
  if [ "$x" = "${LOCALE}" ]
  then
    echo "enter in for loop"
    LANG_TYPE = ${admin}
  else
    LANG_TYPE = ${runtype}
  fi
done

echo "Language: ${LANG_TYPE}"
 
Old 01-03-2006, 01:14 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Sorry, my mind has been wandering a bit today!

Code:
#!/bin/bash
ADMIN_LANG="de-de es-es es-ES fr-fr fr-CA it-it ja-ja ko-ko"
LOCALE=${LOCALE:-ja-ja}
check_type=0

for x in ${ADMIN_LANG}
do
  if [ "$x" = "${LOCALE}" ]
  then
    check_type=`expr ${check_type} + 1`
  fi
done

echo "Language Type: \c"

if [ ${check_type} -eq 0 ]
then
  export LANG_TYPE="RUNTIME"
else
  export LANG_TYPE="ADMIN"
fi

echo "${LANG_TYPE}"
 
Old 01-04-2006, 12:16 AM   #4
Ankit mittal
LQ Newbie
 
Registered: Sep 2005
Location: india
Posts: 11

Original Poster
Rep: Reputation: 0
both of suggeted code still does not work.i am writing makefile. try to run it on sygwin and linux both.
firstly it shows ...command commensed before first target..stop.

then i declare one target as check..

after that it again flag error message as.

for x in "de-de es-es..."
syntax error : end of file unexpected

please take a look into this issue.

Thanks,
Ankit
 
Old 01-04-2006, 12:48 AM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
I think you are confusing makefile macros with shell variables.
You can't set a shell variable inside a rule and have it persist after the rule, nor will it turn into a macro after the rule is done.
 
Old 01-04-2006, 06:02 AM   #6
Ankit mittal
LQ Newbie
 
Registered: Sep 2005
Location: india
Posts: 11

Original Poster
Rep: Reputation: 0
i am not mixing shell variable in makefile.
but i can set value of macro(variable) in rule.
in my first code snipshot , i tried to set value of LANG_TYPE depends upon the condition.after setting it i just check its value.but it still does not show anything.
I declare macro(variable) LANG_TYPE with value runtime then i tried to set its value in if construct but it didnot change.
it shows only previous value.
i want to clear onething i am writing makefile not any script.
 
Old 01-04-2006, 10:27 PM   #7
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Actually, you are writing a script inside the makefile,
the line that begins "for x in..." is a shell script.
The make utility passes this text to the sh interpreter.

Any variable you set inside this script is a real shell variable (not a makefile macro) and will not be visible after the sh script exits. You can not change makefile macros from within one of these embedded shell scripts, nor can you have a shell variable retain its value from one line in the rule to the next.

In the rule you set a "shell variable" LANG_TYPE, it is not a macro.
In the next line you're trying to echo the contents of LANG_TYPE.
It does not work because they are in 2 different scripts because as stated above each line in the makefile rule is executed in a different subshell.

So therefore the statement echo $(LANG_TYPE);
has 2 problems, first it should be echo $$LANG_TYPE because LANG_TYPE is a shell variable not a macro. Second $$LANG_TYPE is still going to be "" empty string because its being executed in a different subshell from where you set it.

Last edited by randyding; 01-04-2006 at 10:50 PM.
 
Old 01-04-2006, 11:50 PM   #8
Ankit mittal
LQ Newbie
 
Registered: Sep 2005
Location: india
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks a lot.
it clarify my doubt as well..
can u tell me any way to set varibale value depends upon condition.
i give some idea about my problem.
i have two target.
i pass locale to makefile.
depends upon the type of locale , i want to run perticular target.
i have string of ADMIN_LANG = "de-de es-es es-ES fr-fr fr-CA it-it ja-ja ko-ko"
i check whether locale is in ADMIN_LANG or not.
so i need one if construct that check value of locale and set another macro or anyyhing that would be workable in this scenerio.
plz suggest me any way.
i stucked on this problem.

Thanks and Regards,
Ankit
 
Old 01-05-2006, 12:11 AM   #9
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Two thinkgs come to mind,
first you can pass macro assigments to the make utility.
Code:
$ cat makefile
all :
    @echo $(foo)

$ make foo=bar
bar
$
You can also invoke make from another rule.
Code:
[0 randyd@entropy t]$ cat makefile
all :
        $(MAKE) othertarget foo=bar

othertarget :
        @echo $(foo)

$ make
make othertarget foo=bar
make[1]: Entering directory `/home/randyd/testmake'
bar
make[1]: Leaving directory `/home/randyd/testmake'
$
 
Old 01-07-2006, 09:12 AM   #10
Ankit mittal
LQ Newbie
 
Registered: Sep 2005
Location: india
Posts: 11

Original Poster
Rep: Reputation: 0
actully makefile called from another file during compilation.we donot want to change at palce from where we call makefile.
now i am trying to call bash file in which i put logic.
thanks for yr concern.
anybody have ant suggestion .plz give me.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
how-to construct execution trees when using yacc sibtay Programming 0 12-30-2004 06:32 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM
kde rpoblem safrout Linux - General 1 05-07-2002 09:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration