bad character showing up in bash script execution

2019-09-01 07:47发布

I have a bash script that is getting an accented character appended to some strings that is causing it to fail, and I can't find where or how these characters are getting in there.

Here is some example output:

mv: cannot move â/tmp/myapp.zipâ to â/opt/myserver/myapp/deploys/myapp.1.2.21.zipâ: No such file or directory
ln: failed to create symbolic link â/opt/myserver/myapp/deploys/myapp_beta.zipâ: No such file or directory
cp: cannot stat â/opt/myserver/myapp/deploys/myapp_beta.zipâ: No such file or directory

the invalid character is the â.

The script is below:

#!/bin/bash

BRANCH=$1

SVN_LOC="https://svn/svn/myserver/"

MYAPP_REPO="myapp.git"
COREJS_REPO="core-js.git"
SPARTAN_REPO="core-spartan.git"

MYAPP_LOCATION="myapp/"
COREJS_LOCATION="corejs/"
SPARTAN_LOCATION="spartan/"

DEPLOY_LOCATION="/tmp/deploy/"
CLEANUP="${DEPLOY_LOCATION}*"

DEPLOY_STORE="/opt/myserver/myapp/deploys/"

DEPLOY_TIME=$(date +%s)

failed ()
{
    rm -rf $CLEANUP
    exit 1
}

mkdir -p $DEPLOY_LOCATION

echo "Retrieving Code from Git Branch ${BRANCH}"

echo "Retrieving myapp code"

mkdir -p "${DEPLOY_LOCATION}${MYAPP_LOCATION}"
pushd /opt/myserver/myapp/myapp
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${MYAPP_LOCATION}"
if [ $? -ne 0 ]
then
    echo "Failed retrieving code from git ${MYAPP_REPO} repo";
    failed
fi
popd

echo "Checking version numbers"
VERSION=$(php "${DEPLOY_LOCATION}${MYAPP_LOCATION}version.php" output)

DEPLOY_PACKAGE="${DEPLOY_STORE}myapp.${VERSION}.zip"

if [ -f $DEPLOY_PACKAGE ]
then
    echo "A deploy with the same version number (${VERSION}) already exists! Please increment version number or manually deal with existing ${DEPLOY_PACKAGE}";
    failed
fi

echo "Retrieving corejs code"

mkdir -p "${DEPLOY_LOCATION}${COREJS_LOCATION}"
pushd /opt/myserver/myapp/core-js
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${COREJS_LOCATION}"
if [ $? -ne 0 ]
then
    echo "Failed retrieving code from git ${COREJS_REPO} repo";
    failed
fi
popd

echo "Retrieving spartan code"

mkdir -p "${DEPLOY_LOCATION}${SPARTAN_LOCATION}"
pushd /opt/myserver/myapp/spartan
git archive $BRANCH | tar -x -C "${DEPLOY_LOCATION}${SPARTAN_LOCATION}"
if [ $? -ne 0 ]
then
    echo "Failed retrieving code from git ${SPARTAN_REPO} repo";
    failed
fi
popd

echo "Minifying js and css"

pushd "${DEPLOY_LOCATION}${MYAPP_LOCATION}Server/Deploy/"
php MinifyLyroke.php --deploytime $DEPLOY_TIME
popd

ASSETS_DEPLOY_PACKAGE="${DEPLOY_STORE}myappassets.${VERSION}.zip"
TEMP_ASSETS_ZIP_LOC="/tmp/myappassets.zip"
DEPLOY_ASSETS="${DEPLOY_LOCATION}myapp/Assets/"
ASSETS_DEPLOY_LOCATION="/tmp/assetsdeploy/"
DEPLOYED_ASSETS="${ASSETS_DEPLOY_LOCATION}myappassets_${DEPLOY_TIME}"

mkdir -p $ASSETS_DEPLOY_LOCATION

echo "Packaging assets deploy to ${ASSETS_DEPLOY_PACKAGE}"

mv $DEPLOY_ASSETS $DEPLOYED_ASSETS

pushd $ASSETS_DEPLOY_LOCATION
zip -r ${TEMP_ASSETS_ZIP_LOC} *
popd

mv ${TEMP_ASSETS_ZIP_LOC} ${ASSETS_DEPLOY_PACKAGE}
ln -sfn ${ASSETS_DEPLOY_PACKAGE} "${DEPLOY_STORE}myappassets_beta.zip"
cp "${DEPLOY_STORE}myappassets_beta.zip" "/opt/myserver/myapp/myapp/Server/Deploy/"

rm -rf $DEPLOYED_ASSETS
rm -rf $ASSETS_DEPLOY_LOCATION

echo "Packaging deploy to ${DEPLOY_PACKAGE}"

TEMP_ZIP_LOC="/tmp/myapp.zip"

pushd ${DEPLOY_LOCATION}
zip -r ${TEMP_ZIP_LOC} *
popd

mv "${TEMP_ZIP_LOC}" "${DEPLOY_PACKAGE}"
ln -sfn "${DEPLOY_PACKAGE}" "${DEPLOY_STORE}myapp_beta.zip"
cp "${DEPLOY_STORE}myapp_beta.zip" "/opt/myserver/myapp/myapp/Server/Deploy"

echo "Cleaning up"

rm -rf $CLEANUP

can anyone possibly see the issue or suggest a way I can go about finding where the issue is?

4条回答
对你真心纯属浪费
2楼-- · 2019-09-01 08:32

Those â characters are just mangled smart quotes printed from your shell. Your shell is probably outputting UTF-8, but your terminal is reading ISO-8859-1. Note that â is the rendering of a UTF-8 encoded smart quote in ISO-8859-1, with two nonprintable characters following the â. Most modern terminal emulators come with an option to enable UTF-8; see if you can enable that (it will make your life easier).

The problem is in your script, not the funny characters.

查看更多
闹够了就滚
3楼-- · 2019-09-01 08:39

From the command line, type both of these commands. One or more of the files/directories you are expecting to exist, does not exist.

ls /tmp/myapp.zip
ls /opt/myserver/myapp/deploys
查看更多
仙女界的扛把子
4楼-- · 2019-09-01 08:41

Try opening the script in another text editor like Notepad++ and see if there are any special characters present.

查看更多
来,给爷笑一个
5楼-- · 2019-09-01 08:44

The accepted answer explains the problem, thanks @nneonneo. This is what you can do for a quick fix:

A) check your locale settings with:

  locale

B) before calling your script or in the top of your bash-script try:

  export LANG=en_US.UTF-8
  export LC_ALL=C
查看更多
登录 后发表回答