语法错误:“(”意外 - Ubuntu 终端

语法错误:“(”意外 - Ubuntu 终端

我正在运行以下脚本,该脚本以#!/bin/bash

#!/bin/bash
#

############################################
# User Configuration
############################################

# adapt this path to your needs
gptPath="/usr/local/snap6/bin/gpt.sh"

############################################
# Command line handling
############################################

# first parameter is a path to the graph xml
graphXmlPath="$1"

# second parameter is a path to a parameter file
parameterFilePath="$2"

# use third parameter for path to source products
sourceDirectory="$3"

# use fourth parameter for path to target products
targetDirectory="$4"

# the fifth parameter is a file prefix for the target product name, typically indicating the type of processing
targetFilePrefix="$5"


############################################
# Helper functions
############################################

# Borrowed from http://www.linuxjournal.com/content/normalizing-path-names-bash
function normalizePath() {
    # Remove all /./ sequences.
    local path="${1//\/.\//\/}"

    # Remove first dir/.. sequence.
    local npath=$(echo "$path" | sed -e 's;[^/][^/]*/\.\./;;')

    # Remove remaining dir/.. sequence.
    while [[ "$npath" != "$path" ]]; do
        path="$npath"
        npath=$(echo "$path" | sed -e 's;[^/][^/]*/\.\./;;')
    done
    echo "$path"
}

getAbsolutePath() {
    file="$1"

    if [ "${file:0:1}" = "/" ]; then
        # already absolute
        echo "$file"
        return
    fi

    absfile="$(pwd)/${file}"
    absfile="$(normalizePath "${absfile}")"
    echo "${absfile}"
}

removeExtension() {
    file="$1"

    echo "$(echo "$file" | sed -r 's/\.[^\.]*$//')"
}


############################################
# Main processing
############################################

# Create the target directory
mkdir -p "${targetDirectory}"

IFS=$'\n'
for F in $(ls -1 "${sourceDirectory}"/S3*.SEN3/xfdumanifest.xml); do
  sourceFile="$(getAbsolutePath "$F")"
  targetFile="${targetDirectory}/${targetFilePrefix}_$(removeExtension "${F}").dim"
  procCmd="${gptPath}" "${graphXmlPath}" "/-e -p /" "${parameterFilePath}" "/ -t /" "${targetFile}" "${sourceFile}"
  "${procCmd}"
done

但是,当使用

sh /shared/Downloads/Chipre/processDataset_V4.bash /shared/Downloads/Chipre/myGraph_v3_for_GPT_v2_2.xml variables.properties "/shared/Downloads/Chipre/June" "/shared/Downloads/Chipre/GPT/June" ndvi

我收到错误:

/shared/Downloads/Chipre/processDataset_V4.bash: 36: /shared/Downloads/Chipre/processDataset_V4.bash: Syntax error: "(" unexpected

我已经尝试过这个帖子但它不起作用。有什么想法吗?

编辑 -

ls -l /shared/Downloads/Chipre

total 64
drwxr-xr-x  3 rus rus 4096 Mar  1 13:30 GPT
drwxr-xr-x  9 rus rus 4096 Feb 22 08:15 July
drwxr-xr-x 10 rus rus 4096 Mar  1 13:28 June
drwxr-xr-x  9 rus rus 4096 Feb 21 12:22 May
drwxr-xr-x  5 rus rus 4096 Mar  1 13:28 graph
-rw-r--r--  1 rus rus 2635 Feb 16 08:33 myGraph_v3.xml
-rw-r--r--  1 rus rus 3205 Feb 27 14:34 myGraph_v3_for_GPT_v2.xml
-rw-r--r--  1 rus rus 2518 Feb 28 08:28 myGraph_v3_for_GPT_v2_1.xml
-rwxr-xr-x  1 rus rus 1573 Feb 28 08:37 myGraph_v3_for_GPT_v2_2.xml
-rwxr-xr-x  1 rus rus 2155 Mar  1 13:45 processDataset_V4.bash
-rw-r--r--  1 rus rus 2155 Mar  1 13:45 processDataset_V4.sh
-rw-r--r--  1 rus rus 2157 Mar  1 13:25 processDataset_V4_backup.bash
-rw-r--r--  1 rus rus 8711 Feb 28 08:06 subset_WKT.odt
-rwxr-xr-x  1 rus rus  780 Feb 28 08:28 variables.properties
  • 编辑 -

从脚本中删除后function,错误不再出现,但现在我得到:

/shared/Downloads/Chipre/processDataset_V4.bash: 54: /shared/Downloads/Chipre/processDataset_V4.bash: Bad substitution
/shared/Downloads/Chipre/myGraph_v3_for_GPT_v2_2.xml: 2: /shared/Downloads/Chipre/myGraph_v3_for_GPT_v2_2.xml: Syntax error: newline unexpected
/shared/Downloads/Chipre/processDataset_V4.bash: 84: /shared/Downloads/Chipre/processDataset_V4.bash: : Permission denied

答案1

问题可能是:

function normalizePath() {

便携式替代方案是:

normalizePath() {

但是,您的脚本中还有许多其他 bashism,因此您需要修复所有这些 bashism,或者仅使用系统中的bash任何链接来代替。sh

bash /shared/Downloads/Chipre/processDataset_V4.bash /shared/Downloads/Chipre/myGraph_v3_for_GPT_v2_2.xml variables.properties "/shared/Downloads/Chipre/June" "/shared/Downloads/Chipre/GPT/June" ndvi

或者直接调用它:

/shared/Downloads/Chipre/processDataset_V4.bash /shared/Downloads/Chipre/myGraph_v3_for_GPT_v2_2.xml variables.properties "/shared/Downloads/Chipre/June" "/shared/Downloads/Chipre/GPT/June" ndvi

相关内容