Сравнение равенства не работает в скрипте bash, несмотря на то, что эхо показывает, что переменные равны - PullRequest
0 голосов
/ 08 марта 2020

Мне нужно определить, что одно значение, локально установленное в скрипте, соответствует одному из двух значений, хранящихся в репо git в файле .txt. Я успешно извлекаю эти значения и сохраняю их в переменные currentVersion и newVersion. Я также делаю это с dispGitTime и gitMESSAGE.

. Странно то, что эти два сравнения работают, как ожидалось:

if [ "$dispGitTime" = "" ]; then dispGitTime=0; fi
if [ ! "$gitMESSAGE" = "" ]; then clear; echo "$gitMESSAGE"; sleep "$dispGitTime"; fi

Но эти два сравнения не работают! else всегда вызывается!

if [ "$currentVersion" = "$scriptVersion" ]; then
        upToDate="true"
        printf "\n%*s" $((COLS/2)) "This script is up-to-date!"; sleep 1
    elif [ "$newVersion" = "$scriptVersion" ]; then
        upToDate="true"
        printf "\n%*s" $((COLS/2)) "This script is up-to-date!"; sleep 1

Вся функция для контекста: (см. Ниже вывод этой функции)

gitConfigs(){
        terminalPath=""; terminalPath=$(pwd)
        rm -rf ~/upt; mkdir ~/upt; cd ~/upt || return

        # clone repo or update it with git pull if it exists already
        (CMD_gitGet); wait
        cd "$terminalPath" || return

        # get config values from the master branch's properties.txt
        currentVersionLine=$(grep -n "_version " ~/upt/$gitName/properties.txt); currentVersion="${currentVersionLine##* }"; echo $currentVersion
        newVersionLine=$(grep -n "_newVersion " ~/upt/$gitName/properties.txt); newVersion="${newVersionLine##* }"; echo $newVersion
        gitMESSAGELine=$(grep -n "_gitMESSAGE " ~/upt/$gitName/properties.txt); gitMESSAGE="${gitMESSAGELine##* }"
        dispGitTimeLine=$(grep -n "_dispGitTime " ~/upt/$gitName/properties.txt); dispGitTime="${dispGitTimeLine##* }"

        echo $scriptVersion; sleep 2

        # set scriptTitle to match config, else use default
        if scriptTitle=$(grep -n "_scriptTitle " ~/upt/Android-Installer/properties.txt); then
            scriptTitle="${scriptTitle##* }"
        else scriptTitle="$scriptTitleDEF"; fi

        if [ "$currentVersion" = "$scriptVersion" ]; then
            upToDate="true"
            printf "\n%*s" $((COLS/2)) "This script is up-to-date!"; sleep 1
        elif [ "$newVersion" = "$scriptVersion" ]; then
            upToDate="true"
            printf "\n%*s" $((COLS/2)) "This script is up-to-date!"; sleep 1
        else
            upToDate="false"
            printf "\n\n\n\n\n%*s\n" $((COLS/2)) "This script: v$scriptVersion"
            printf "\n%*s\n" $((COLS/2)) "Latest version: v$currentVersion"
            printf "%*s\n" $((COLS/2)) "Version in progress: v$newVersion"

            printf "\n%*s" $((COLS/2)) "Update required..."; sleep 2
            #update
        fi

        # display gitMESSAGE if there is one
        if [ "$dispGitTime" = "" ]; then dispGitTime=0; fi
        if [ ! "$gitMESSAGE" = "" ]; then clear; echo "$gitMESSAGE"; sleep "$dispGitTime"; fi
    }

Ouput:

1.1.6-beta
1.1.7-release
1.1.7-release





                                            This script: v1.1.7-release

                                           Latest version: v1.1.6-beta
                                   Version in progress: v1.1.7-release

                                                     Update required...

1 Ответ

0 голосов
/ 08 марта 2020

Проблема была найдена с использованием предложения declare -p varname | cat -v @JohnKugelman.

Действительно, в конце этих переменных было возвращено каретки ^ M.

Я нашел способ их удалить здесь: unix .stackexchange

Решение? Добавление:

currentVersion=${currentVersion%$'\r'}

newVersion=${newVersion%$'\r'}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...