johnhenry
johnhenry

Reputation: 1333

Error while sourcing shell script

In order to complete an installation, I need to source the following .sh file:

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.

EDIT:

The output of lsb_release -a is:

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.

Upvotes: 0

Views: 411

Answers (2)

randomir
randomir

Reputation: 18687

In bash, your script is syntactically correct. But if you use sh, then there are a few errors. Check the shellcheck output:

$ 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)
  • local isn't supported in POSIX sh
  • string replacement ${//} is not supported in sh.

Upvotes: 1

Bogdan Stoica
Bogdan Stoica

Reputation: 4539

I think you're using a different shell (tcsh) rather than sh or bash. Most probably you have to adapt your source code to make it load using tcsh. Under sh/bash works just fine

root@pve1:~# echo $0
-bash

Upvotes: 3

Related Questions