在shell和tcsh之间的脚本中更改shell

时间:2015-08-26 18:02:01

标签: linux bash shell tcsh

我的情况是我的默认shell在脚本中设置为sh或bash。我必须更改我的shell才能在tcsh中执行某些命令。当我这样做时,我无法设置变量。

#!/bin/sh
Variables=/path/set

 tcsh
vendor_command.sh

ret=$?

if (ret!= 0)
exit "$ret"
else
echo "success"
fi

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:3)

回答字面问题:不,你不能在脚本语言之间无缝切换,在它们之间双向传递shell局部变量,而不自己实现这种数据传递协议。

可以在你的POSIX sh脚本中嵌入一个tcsh脚本,如下所示:

#!/bin/sh -a
# the -a above makes your variables automatically exported
# ...so that they can be seen by the vendor's script.

Variables=/path/set

# If vendor_command exits with a nonzero exit status, this
# ...makes the script exit with that same status.
vendor_command.sh || exit

echo "success"

...然而,当tcsh解释器退出时,它的所有变量都随之消失。要以其他方式传递状态,您需要在stdout上发出内容并读取或评估它;将内容写入文件;或者以其他方式明确地实现一些接口以在两个脚本之间传递数据。

所有这一切,你的例子都被简单地重写,根本不需要tcsh:

{{1}}