1

I am currently converting a csh script on linux to a bash script on Mac OS X lion.

The csh script looks like:

setenv CNS_SOLVE '/Users/ucbthsa/Documents/haddock2.1/software/bin/'
setenv CNS_SOLVE $CNS_SOLVE
if ( -d $CNS_SOLVE ) then
    if ( ! $?CNS_ARCH ) setenv CNS_ARCH `$CNS_SOLVE/bin/getarch`
else
    setenv CNS_ARCH 'unknown'
endif

My conversion to a Mac bash script looks as follows:

export CNS_SOLVE='/Users/ucbthsa/Documents/haddock2.1/software/bin/cns_solve_1.3'
export CNS_SOLVE=$CNS_SOLVE

if [ -d $CNS_SOLVE ]; then
  if [ ! $?CNS_ARCH ]; then
    export CNS_ARCH='$CNS_SOLVE/bin/getarch'
else
  export CNS_ARCH='unknown'
endif

When I try and source the Mac bash script I get the following error:

-bash: cns_solve_env: line 10: syntax error: unexpected end of file

I cannot understand why I am getting this error.

2
  • 1
    "fi" instead of "endif"? Commented Jun 7, 2012 at 14:33
  • Oh and there's also the second if that's not closed Commented Jun 7, 2012 at 14:36

2 Answers 2

3

You should use fi rather than endif and you aren't closing the first if at all:

export CNS_SOLVE='/Users/ucbthsa/Documents/haddock2.1/software/bin/cns_solve_1.3'
export CNS_SOLVE=$CNS_SOLVE

if [ -d $CNS_SOLVE ]; then
  if [ -z $CNS_ARCH ]; then
    export CNS_ARCH="$CNS_SOLVE/bin/getarch"
  fi
else
  export CNS_ARCH='unknown'
fi

*edit: changed the second test, as William Pursell pointed out, it wouldn't work as it was in bash.

Sign up to request clarification or add additional context in comments.

2 Comments

! $?CNS_ARCH will always evaluate false, since the string being tested will always be XCNS_ARCH where X is the value of $?.
You should double-quote when you expand variables: [ -d "$CNS_SOLVE" ] and especially [ -z "$CNS_ARCH" ] to avoid parsing confusion. Also, single-quotes prevent variable expansion, so use export CNS_ARCH="$CNS_SOLVE/bin/getarch"
0

$?CNS_ARCH means something different in sh. Use:

test -z "$CNS_ARCH" && CNS_ARCH=$($CNS_SOLVE/bin/getarch)

or

CNS_ARCH=${CNS_ARCH-$( $CNS_SOLVE/bin/getarch)}

Notice that these have slightly different meanings. The first is will assign to CNS_ARCH if CNS_ARCH is already set but is the empty string, while the second will not change CNS_ARCH if it is already set, but is empty, which is what the $? does in csh, but is probably not what you actually want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.