今天早上我已經閱讀了很多關于 bash 腳本中的變數擴展的內容,但我仍然無法確定是什么導致腳本停止在這行代碼中擴展變數:
CURRENT_HASH=`git log --pretty=format:"%H %s" | grep ${LIBRARY_VERSION}-SNAPSHOT | head -1 | cut -d ' ' -f 1`
這是更多背景關系,注釋顯示了每一行的輸出:
SCRIPTPATH=`pwd`
# get the application name and library name from the cloned repo in the deploy directory
DEPLOY_DIRECTORY="$(basename `pwd`)"
LIBRARY_SUFFIX="-library.txt"
cd ..
for d in */ ; do
if [[ "$d" == *"library"* ]] ; then
LIBRARY_NAME="${d%?}" # myAppLibrary
fi
if [[ "$d" != "${DEPLOY_DIRECTORY}" && "$d" != *"library"* ]] ; then
APP_NAME="${d%?}" # myAppName
fi
done
# get the latest version from the library
cd ..
LIBRARY_LATEST=$(head -n 1 $SCRIPTPATH/../${LIBRARY_NAME}/${APP_NAME}${LIBRARY_SUFFIX})
LIBRARY_VERSION=`echo $LIBRARY_LATEST | sed '$s/,//'`
echo $LIBRARY_LATEST # outputs 320,
echo $LIBRARY_VERSION # outputs 320 (no comma)
# get the hash matching the last frozen version from the app repo
cd $SCRIPTPATH/../$APP_NAME
CURRENT_HASH=`git log --pretty=format:"%H %s" | grep ${LIBRARY_VERSION}-SNAPSHOT | head -1 | cut -d ' ' -f 1`
echo "Current hash: $CURRENT_HASH" # blank
如果我從命令列執行git log命令,我會得到一個哈希,正如預期的那樣。
這段代碼上周運行良好,但現在$CURRENT_HASH每次都是空白。如果我手動包含它,${LIBRARY_VERSION}它作業正常。如果我手動填充變數,它作業正常。我也試過$LIBRARY_VERSION無濟于事。
我錯過了一些明顯的東西嗎?什么會導致變數停止作業?
uj5u.com熱心網友回復:
正如對該問題的評論中所討論的,問題的直接原因似乎是字串 in$LIBRARY_VERSION有一個尾隨回車符。
問題的一個可能的根本原因是從中LIBRARY_LATEST讀取變數值的檔案最近獲取了 Windows 文本檔案行結尾 (CRLF)。
意外的 CRLF 行結尾是與 Bash(和類似 shell)相關的問題中描述的問題的常見原因。這些參考資料可能有用:
- Stack Overflow 'bash' 資訊頁面的“在詢問有問題的代碼之前”部分的第一點
- shell 腳本對編碼和行尾敏感嗎?
- 如何在 Unix 行尾轉換 Windows 行尾(CR/LF 到 LF)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429758.html
