尽管 echo 显示变量相等,但相等比较在 bash 脚本中不起作用
Equality comparison not working in bash script despite echo showing variables are equal
我需要确定脚本中本地设置的一个值与存储在 .txt 文件中的 git 存储库中的两个值之一相匹配。我成功地检索了这些值并将它们保存到变量 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
}
输出:
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...
问题是根据@JohnKugelman 的declare -p varname | cat -v
建议发现的。
这些变量的末尾确实有^M回车returns。
我在这里找到了删除它们的方法:unix.stackexchange
解决方案?添加:
currentVersion=${currentVersion%$'\r'}
newVersion=${newVersion%$'\r'}
我需要确定脚本中本地设置的一个值与存储在 .txt 文件中的 git 存储库中的两个值之一相匹配。我成功地检索了这些值并将它们保存到变量 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
}
输出:
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...
问题是根据@JohnKugelman 的declare -p varname | cat -v
建议发现的。
这些变量的末尾确实有^M回车returns。
我在这里找到了删除它们的方法:unix.stackexchange
解决方案?添加:
currentVersion=${currentVersion%$'\r'}
newVersion=${newVersion%$'\r'}