Home > Software engineering >  My bash script suddenly stopped variable expansion in one line of code
My bash script suddenly stopped variable expansion in one line of code

Time:02-19

I have read a lot about variable expansion in bash scripts this morning but I have still not been able to identify what has caused the script to stop expanding the variable in this line of code:

CURRENT_HASH=`git log --pretty=format:"%H %s" | grep ${LIBRARY_VERSION}-SNAPSHOT | head -1 | cut -d ' ' -f 1`

Here is more context, with comments showing the output of each line:


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

If I do the git log command from the command line I get a hash, as expected.

This block of code was working perfectly last week, but now $CURRENT_HASH is blank every time. If I manually include the ${LIBRARY_VERSION} it works fine. If I manually populate the variable, it works fine. I have also tried $LIBRARY_VERSION to no avail.

Am I missing something obvious? What would cause a variable to stop working?

CodePudding user response:

As discussed in the comments on the question, the immediate cause of the problem appears to be that the string in $LIBRARY_VERSION has a trailing carriage return character.

A likely underlying cause of the problem is that the file from which the LIBRARY_LATEST variable value was read has recently acquired Windows text file line endings (CRLF).

Unexpected CRLF line endings are a common cause of problems described in questions relating to Bash (and similar shells). These references may be useful:

  • Related