LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting rpoblem in assigning value to variable in if construct (https://www.linuxquestions.org/questions/programming-9/getting-rpoblem-in-assigning-value-to-variable-in-if-construct-399013/)

Ankit mittal 01-03-2006 07:09 AM

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

Disillusionist 01-03-2006 12:02 PM

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}"


Disillusionist 01-03-2006 01:14 PM

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}"


Ankit mittal 01-04-2006 12:16 AM

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

randyding 01-04-2006 12:48 AM

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.

Ankit mittal 01-04-2006 06:02 AM

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.

randyding 01-04-2006 10:27 PM

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.

Ankit mittal 01-04-2006 11:50 PM

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

randyding 01-05-2006 12:11 AM

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'
$


Ankit mittal 01-07-2006 09:12 AM

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.


All times are GMT -5. The time now is 01:02 PM.