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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在bash脚本执行中出现的坏字符相关的知识,希望对你有一定的参考价值。

我有一个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

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

答案

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

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

另一答案

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

另一答案

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

ls /tmp/myapp.zip
ls /opt/myserver/myapp/deploys
另一答案

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

A)检查您的区域设置:

  locale

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

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

以上是关于在bash脚本执行中出现的坏字符的主要内容,如果未能解决你的问题,请参考以下文章

Bash脚本之函数定义和调用

确定字符串“a”在“x”小时内是不是相同然后执行“Y”的 Bash 脚本

bash脚本之函数简单介绍应用及函数的简单递归调用

关于bash如何进行并发执行!

在 Bash 脚本中执行 Apache Spark (Scala) 代码

bash - 错误或超时关闭脚本[重复]