简体   繁体   English

采购Shell脚本时出错

[英]Error while sourcing shell script

In order to complete an installation, I need to source the following .sh file: 为了完成安装,我需要获取以下.sh文件的来源:

function addvar () {
local tmp="${!1}" ;
tmp="${tmp//:${2}:/:}" ; tmp="${tmp/#${2}:/}" ; tmp="${tmp/%:${2}/}" ;
export $1="${2}:${tmp}" ;
} 

if [ -z "${PATH}" ]; then 
PATH=/share/MontePython/plc-2.0/bin
export PATH
else
addvar PATH /share/MontePython/plc-2.0/bin
fi
if [ -z "${PYTHONPATH}" ]; then 
PYTHONPATH=/share/MontePython/plc-2.0/lib/python2.7/site-packages
export PYTHONPATH
else
addvar PYTHONPATH /share/MontePython/plc-2.0/lib/python2.7/site-packages
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/ipp/../compiler/lib/intel64
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/ipp/../compiler/lib/intel64
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/compiler/lib/intel64/
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/compiler/lib/intel64/
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/compiler/lib/intel64
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/compiler/lib/intel64
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/lib64
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /lib64
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/lib
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /lib
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/ipp/../compiler/lib/intel64/
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /share/apps/intel/l_ics_2015.1.133/composer_xe_2015.1.133/ipp/../compiler/lib/intel64/
fi
if [ -z "${LD_LIBRARY_PATH}" ]; then 
LD_LIBRARY_PATH=/share/MontePython/plc-2.0/lib
export LD_LIBRARY_PATH
else
addvar LD_LIBRARY_PATH /share/MontePython/plc-2.0/lib
fi
CLIK_PATH=/share/MontePython/plc-2.0
export CLIK_PATH

CLIK_DATA=/share/MontePython/plc-2.0/share/clik
export CLIK_DATA

CLIK_PLUGIN=rel2015
export CLIK_PLUGIN

but when I source it, I get the following error: 但是当我获取它时,出现以下错误:

() not correctly positioned

Any idea why? 知道为什么吗? The curious thing is that this error is happening when I operate on a cluster, while I don't have it on my PC. 奇怪的是,当我在群集上操作而我的PC上没有该错误时,就会发生此错误。

EDIT: 编辑:

The output of lsb_release -a is: lsb_release -a的输出是:

LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.9 (Final)
Release:    6.9
Codename:   Final

The output of echo $0 is -tcsh . echo $0的输出是-tcsh

I think you're using a different shell (tcsh) rather than sh or bash. 我认为您使用的是其他Shell(tcsh),而不是sh或bash。 Most probably you have to adapt your source code to make it load using tcsh. 很可能您必须调整源代码才能使用tcsh加载它。 Under sh/bash works just fine 在sh / bash下工作正常

root@pve1:~# echo $0
-bash

In bash , your script is syntactically correct. bash ,您的脚本在语法上是正确的。 But if you use sh , then there are a few errors. 但是,如果使用sh ,则将出现一些错误。 Check the shellcheck output: 检查shellcheck输出:

$ shellcheck script.sh 

In script.sh line 3:
function addvar () {
^-- SC2112: 'function' keyword is non-standard. Delete it.


In script.sh line 4:
local tmp="${!1}" ;
^-- SC2039: In POSIX sh, 'local' is undefined.
           ^-- SC2039: In POSIX sh, indirect expansion is undefined.


In script.sh line 5:
tmp="${tmp//:${2}:/:}" ; tmp="${tmp/#${2}:/}" ; tmp="${tmp/%:${2}/}" ;
     ^-- SC2039: In POSIX sh, string replacement is undefined.
                              ^-- SC2039: In POSIX sh, string replacement is undefined.
                                                     ^-- SC2039: In POSIX sh, string replacement is undefined.

In summary: 综上所述:

  • function keyword is not needed (or even recommended) 不需要function关键字(甚至不推荐)
  • local isn't supported in POSIX sh POSIX sh不支持local
  • string replacement ${//} is not supported in sh . sh不支持字符串替换${//}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM