bash 脚本执行中出现错误字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18081038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:09:00  来源:igfitidea点击:

bad character showing up in bash script execution

bashshellubuntu

提问by Tristan

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.

我有一个 bash 脚本,它在一些导致它失败的字符串上附加了一个重音字符,但我找不到这些字符在哪里或如何进入那里。

Here is some example output:

这是一些示例输出:

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

the invalid character is the a.

无效字符是a

The script is below:

脚本如下:

#!/bin/bash

BRANCH=

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?

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

回答by nneonneo

Those acharacters 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 ais the rendering of a UTF-8 encoded smart quote in ISO-8859-1, with two nonprintable characters following the a. Most modern terminal emulators come with an option to enable UTF-8; see if you can enable that (it will make your life easier).

这些a字符只是从你的 shell 打印出来的乱七八糟的智能引号。您的 shell 可能正在输出 UTF-8,但您的终端正在读取 ISO-8859-1。请注意,这aISO-8859-1 中UTF-8 编码的智能引号的呈现,a后面有两个不可打印的字符。大多数现代终端模拟器都带有启用 UTF-8 的选项;看看你是否可以启用它(它会让你的生活更轻松)。

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

问题在于你的剧本,而不是有趣的角色。

回答by David Bianco

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

回答by Schlacki

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

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

A) check your locale settings with:

A) 检查您的区域设置:

  locale

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

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

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

回答by Vinod

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

尝试在 Notepad++ 等其他文本编辑器中打开脚本,看看是否存在任何特殊字符。