脚本不会继承ksh set -o pipefail

时间:2015-05-26 18:21:43

标签: ksh

我想设置-o pipefail set" always" (.kshrc)但请考虑以下简单示例:

#!/bin/ksh
  function fun {
      return 99
  }
if [[ $1 == 'pipe' ]] ; then
   set -o pipefail
fi
if fun | tee /tmp/bork.txt ; then 
    print "fun returned 0"
else
    print "fun nonzero"
fi

This results in:
/home/khb>./b pipe    
fun nonzero                          GOOD what we want
/home/khb>./b                        
fun returned 0                       What we expect without pipefail!
/home/khb>set -o pipefail
/home/khb>./b
fun returned 0                 BAD: expected the set to impact inferior shells

毫无疑问,这应该是显而易见的,但除了创建一个环境变量并让每个脚本引用它...或者获取一组通用定义之外......还有哪些其他选项可以安排此选项为&# 34;实测值"在每个脚本中?

提前致谢。

1 个答案:

答案 0 :(得分:1)

稍微尴尬的解决方案是在set -o pipefail文件中包含~/.profile,然后编写始终调用 ksh 作为登录shell的脚本,即使用{{1作为hash-bang行。

较少(?)尴尬的解决方案是将#!/bin/ksh -l放入set -o pipe fail指向的文件中,然后使用$ENV调用 ksh (而不是-E上面-l)。但是,解析$ENV的shell通常是交互式shell ...