将 csh 脚本转换为 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10933888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:27:02  来源:igfitidea点击:

Convert csh script to bash script

bashcsh

提问by Harpal

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

我目前正在将 linux 上的 csh 脚本转换为 Mac OS X lion 上的 bash 脚本。

The csh script looks like:

csh 脚本如下所示:

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:

我转换为 Mac bash 脚本如下所示:

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:

当我尝试获取 Mac bash 脚本的源代码时,出现以下错误:

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

-bash:cns_solve_env:第 10 行:语法错误:文件意外结束

I cannot understand why I am getting this error.

我不明白为什么我会收到这个错误。

回答by John Lawrence

You should use firather than endifand you aren't closing the first if at all:

如果有的话,您应该使用fi而不是endif并且您不会关闭第一个:

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.

*编辑:改变了第二个测试,正如威廉珀塞尔指出的那样,它不会像在 bash 中那样工作。

回答by William Pursell

$?CNS_ARCHmeans something different in sh. Use:

$?CNS_ARCH在 sh 中意味着不同的东西。用:

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.

请注意,这些含义略有不同。如果 CNS_ARCH 已经设置但为空字符串,第一个将分配给 CNS_ARCH,而如果 CNS_ARCH 已经设置但为空,则第二个不会更改 CNS_ARCH,这是$?在 csh 中所做的,但可能不是你实际的想。