isocppforbids
isocppforbids

Reputation: 389

ifeq doesn't compare two variable

ifeq doesn't compare two variable

I'm making a program to move a file to the desktop, independently of the language chosen in the OS. In portuguese the desktop folder is called "Área de trabalho". Since "Área de Trabalho" are three separated words, you need to add single quotation marks ('Área de Trabalho') when writting the directory, otherwise the command results in error.

    mv Minesweeper $HOME/Área de Trabalho/  #command

    mv: target 'Trabalho/' is not a directory  #Error

I'm using this command to get the directory of the desktop folder of the user:

    xdg-user-dir DESKTOP

And if the directory is equals to the home directory plus "/Área de Trabalho", I want to change

Here's the code:

    desktop=$(shell xdg-user-dir DESKTOP) # I'm using this command to get the directory of the desktop folder of the user
    desktopVar="$$HOME/Área de Trabalho" # And if the directory is equals to the home directory plus "/Área de Trabalho"

    move:
    ifeq (${desktop},${desktopVar})
        desktop=${HOME}/'Área de Trabalho' # I want to change the directory to ${HOME} plus "/'Área de Trabalho'", so the command doesn't results in error
        @echo yes
    endif
        @echo ${desktopVar}
        @echo ${desktop}

But the ifeq says both variables are equal and doesn't change anything.

Here's the output:

    /home/daniel/Área de Trabalho
    /home/daniel/Área de Trabalho

Upvotes: 0

Views: 169

Answers (2)

HardcoreHenry
HardcoreHenry

Reputation: 6377

Here's a makefile that should work (no, really this time :-) I actually tested this one:)

desktop:=$(shell xdg-user-dir DESKTOP)
desktopVar:=${HOME}/Área de Trabalho  #note: using _MAKE_ variable ${HOME} here

$(info desktop:     [${desktop}])
$(info desktopVar:  [${desktopVar}])

    # note: using double quotes around ifeq arguments -- not required
    #       here, but it's good practice:
    # also note: this is not inside of a recipe, so it's done by the
    #            makefile at parse time

ifneq ("${desktop}","${desktopVar}")
$(info changing desktop)
desktop:=${HOME}/Área de Trabalho
endif


$(info desktop:     [${desktop}])
$(info desktopVar:  [${desktopVar}])

all:
        @echo running $@

which outputs:

scripts> make -f test.mk
desktop:     [/users/foo/Desktop]
desktopVar:  [/users/foo/Área de Trabalho  ]
changing desktop
desktop:     [/users/foo/Área de Trabalho]
desktopVar:  [/users/foo/Área de Trabalho  ]
running all

Upvotes: 1

choroba
choroba

Reputation: 241758

The double quotes are part of the variable value, but they aren't shown in the output because the shell interprets them.

desktop = a
desktopVar = "a"

test:
ifeq ($(desktop),$(desktopVar))
    @echo yes
else
    @echo no
endif
    echo $(desktop)
    echo $(desktopVar)

So, the solution is to include the double quotes in the definition:

desktop = "$(shell xdg-user-dir DESKTOP)"

Don't overuse @, seeing what Makefile actually tries to run can often help you when debugging.

Upvotes: 1

Related Questions