我的 bash 脚本突然在一行代码中停止了变量扩展
My bash script suddenly stopped variable expansion in one line of code
今天早上我在 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
没用。
我是不是遗漏了什么明显的东西?什么会导致变量停止工作?
正如问题评论中所讨论的,问题的直接原因似乎是 $LIBRARY_VERSION
中的字符串有一个尾随 carriage return 字符。
问题的一个可能根本原因是从中读取 LIBRARY_LATEST
变量值的文件最近获得了 Windows 个文本文件行结尾 (CRLF)。
意外的 CRLF 行结尾是与 Bash(和类似 shell)相关的问题中描述的问题的常见原因。这些参考可能有用:
- Stack Overflow 'bash' Info page
的“询问有问题的代码之前”部分的第一点
- How to convert Windows end of line in Unix end of line (CR/LF to LF)
今天早上我在 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
没用。
我是不是遗漏了什么明显的东西?什么会导致变量停止工作?
正如问题评论中所讨论的,问题的直接原因似乎是 $LIBRARY_VERSION
中的字符串有一个尾随 carriage return 字符。
问题的一个可能根本原因是从中读取 LIBRARY_LATEST
变量值的文件最近获得了 Windows 个文本文件行结尾 (CRLF)。
意外的 CRLF 行结尾是与 Bash(和类似 shell)相关的问题中描述的问题的常见原因。这些参考可能有用:
- Stack Overflow 'bash' Info page 的“询问有问题的代码之前”部分的第一点
- How to convert Windows end of line in Unix end of line (CR/LF to LF)