在bash脚本执行中出现的坏字符

时间:2013-08-06 13:04:46

标签: bash shell ubuntu

我有一个bash脚本,它会在某些字符串后面附加一个重音字符,导致它失败,而且我找不到这些字符在哪里或如何进入。

以下是一些示例输出:

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

无效字符是â

脚本如下:

#!/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

任何人都可以看到问题或建议我可以找到问题的方法吗?

4 个答案:

答案 0 :(得分:3)

那些â个字符只是从你的shell打印出的错误的智能引号。您的shell可能输出UTF-8,但您的终端正在读取ISO-8859-1。请注意,â是ISO-8859-1中UTF-8编码的智能引用的呈现,其后有两个不可打印的字符。大多数现代终端仿真器都提供启用UTF-8的选项;看看你是否可以启用它(它会让你的生活更轻松)。

问题在于你的脚本,而不是有趣的人物。

答案 1 :(得分:0)

尝试在另一个文本编辑器(如Notepad ++)中打开脚本,看看是否存在任何特殊字符。

答案 2 :(得分:0)

在命令行中,键入这两个命令。您希望存在的一个或多个文件/目录不存在。

ls /tmp/myapp.zip
ls /opt/myserver/myapp/deploys

答案 3 :(得分:0)

已接受的答案可以解释问题,谢谢@nneonneo。这是您可以快速解决的方法:

A)使用以下方法检查您的语言环境设置:

  locale

B)在调用脚本之前或在bash脚本顶部尝试:

  export LANG=en_US.UTF-8
  export LC_ALL=C