运行简单的 bash 脚本失败,出现语法错误:单词意外(预期“)”)

运行简单的 bash 脚本失败,出现语法错误:单词意外(预期“)”)

我正在尝试在 ubuntu14.04 中安装 Anaconda3,如下所示:

seyyedhossein@hossein:~$ sh Anaconda3-4.2.0-Linux-x86_64.sh 

我得到:

Anaconda3-4.2.0-Linux-x86_64.sh: 16: Anaconda3-4.2.0-Linux-x86_64.sh: 0: not found
Anaconda3-4.2.0-Linux-x86_64.sh: 61: Anaconda3-4.2.0-Linux-x86_64.sh: 0: not found
Anaconda3-4.2.0-Linux-x86_64.sh: 75: Anaconda3-4.2.0-Linux-x86_64.sh: Syntax error: word unexpected (expecting ")")

这是什么问题?
我已经成功地将它安装在另一个帐户上(最初是从该帐户下载的)。但当我登录到我的新帐户时,它就是无法运行!

更新:
cat 命令的输出:

#!/bin/bash
# Copyright (c) 2012-2016 Continuum Analytics, Inc.
# All rights reserved.
#
# Name: Anaconda3
# Version: 4.2.0
# Packages: 195
# PLAT:  linux-64
# DESCR: 4.1.1-889-g7ce9b7f
# BYTES: 478051940
# LINES: 558
# MD5:   1ee1f5cb1d92a230e59cc5fce0dca5ba

unset LD_LIBRARY_PATH
echo "$0" | grep '\.sh$' >/dev/null
if (( $? )); then
    echo 'Please run using "bash" or "sh", but not "." or "source"' >&2
    return 1
fi

THIS_DIR=$(cd $(dirname $0); pwd)
THIS_FILE=$(basename $0)
THIS_PATH="$THIS_DIR/$THIS_FILE"
PREFIX=$HOME/anaconda3
BATCH=0
FORCE=0

while getopts "bfhp:" x; do
    case "$x" in
        h)
            echo "usage: $0 [options]

Installs Anaconda3 4.2.0

    -b           run install in batch mode (without manual intervention),
                 it is expected the license terms are agreed upon
    -f           no error if install prefix already exists (force)
    -h           print this help message and exit
    -p PREFIX    install prefix, defaults to $PREFIX
"
            exit 2
            ;;
        b)
            BATCH=1
            ;;
        f)
            FORCE=1
            ;;
        p)
            PREFIX="$OPTARG"
            ;;
        ?)
            echo "Error: did not recognize option, please try -h"
            exit 1
            ;;
    esac
done

# verify the size of the installer
wc -c "$THIS_PATH" | grep 478051940 >/dev/null
if (( $? )); then
    echo "ERROR: size of $THIS_FILE should be 478051940 bytes" >&2
    exit 1
fi

if [[ $BATCH == 0 ]] # interactive mode
then
    if [[ `uname -m` != 'x86_64' ]]; then
        echo -n "WARNING:
    Your operating system appears not to be 64-bit, but you are trying to
    install a 64-bit version of Anaconda3.
    Are sure you want to continue the installation? [yes|no]
[no] >>> "
        read ans
        if [[ ($ans != "yes") && ($ans != "Yes") && ($ans != "YES") &&
              ($ans != "y") && ($ans != "Y") ]]

答案1

问题出在脚本上:尽管它在自己的文档中声称它可以由 运行sh,即由任何标准 POSIX shell 运行,但它实际上需要bash

该构造(( $? ))不是有效的 POSIX sh,当为空($ans != "yes")时也不是$ans。它们在两者中都几乎无效bash(老实说,这是我 20 年来第一次看到这个习语(( $? ))),但显然bash让它通过了。

解决方案:使用 bash 运行:bash Anaconda3-4.2.0-Linux-x86_64.sh

答案2

您给出的 sh 命令假定 .sh 文件位于您的主目录中。它可能位于其他地方,在这种情况下,您必须为其指定文件所在位置的路径名。

例如:如果文件 Anaconda3-4.2.0-Linux-x86_64.sh 位于您的桌面上,您可以:

cd ~/Desktop
bash Anaconda3-4.2.0-Linux-x86_64.sh

或者

bash ~/Desktop/Anaconda3-4.2.0-Linux-x86_64.sh

如果这不能解决您的问题,请编辑您的问题以包含以下内容:

cat Anaconda3-4.2.0-Linux-x86_64.sh | head -n 76

答案3

我使用 bash 代替 sh,脚本运行得很好!
看来 sh 不适合与这个一起使用!

相关内容