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

标签 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

有办法吗?

最佳答案

回答字面上的问题:不,你不能在脚本语言之间无缝切换,在它们之间双向传递 shell-local 变量,而不用自己实现数据传递协议(protocol)。

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

#!/bin/sh
# this is run with POSIX sh
export var1="tcsh can see this"
var2="tcsh cannot see this"

tcsh <<'EOF'
# this is run with tcsh
# exported variables (not shell-local variables!) from above are present
var3="calling shell cannot see this, even if exported"
EOF

# this is run in the original POSIX sh shell after tcsh has exited
# only variables set in the POSIX shell, not variables set by tcsh, are available

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


总而言之,您的示例被简单地重写,根本不需要 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"

关于linux - 在 shell 和 tcsh 之间的脚本中更改 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233587/

相关文章:

linux - 如何使用 "getopts"选项下的 2 个参数调用函数

linux - 来自键盘的变量等于bash

linux - Perl 搜索和替换

python - 如何过滤所有包含 N 个或更多字符的单词?

linux - 文件处理排序和去重

linux - zsh menu completion 导致 zle reset-prompt 后出现问题

java - JSch 执行 sudo 命令 : How to access sudo command output text?

linux - 是否有 VNC 服务器监听 Unix 域套接字而不是标准端口 (5900)?

shell - "Event not found"unix中的shell命令错误

shell - 如何理解 ${CONFIG+x} 中的 x