Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ksh ENV脚本中检测交互式shell_Shell_Unix_Ksh - Fatal编程技术网

在ksh ENV脚本中检测交互式shell

在ksh ENV脚本中检测交互式shell,shell,unix,ksh,Shell,Unix,Ksh,确定给定ksh调用是否运行交互式shell的首选方法是什么 我在ENV文件中有一些命令,对于非交互式ksh调用(例如,在执行shell脚本时),我希望跳过这些命令 我已经看到: …甚至不采购.kshrc,除非确定外壳使用以下神秘方式进行交互: 在bash中,这两种方法通常在~/.bashrc中使用: 检查stdin是否为tty: [ -t 0 ] || return 或 检查是否设置了提示($PS1): 但是我不知道如何在ksh中实现这一点。我发现检查$-变量中的'I'标志是ksh中最

确定给定ksh调用是否运行交互式shell的首选方法是什么

我在
ENV
文件中有一些命令,对于非交互式ksh调用(例如,在执行shell脚本时),我希望跳过这些命令

我已经看到:

…甚至不采购
.kshrc
,除非确定外壳使用以下神秘方式进行交互:


bash中,这两种方法通常在
~/.bashrc
中使用:

  • 检查
    stdin
    是否为tty:

    [ -t 0 ] || return
    

  • 检查是否设置了提示(
    $PS1
    ):


但是我不知道如何在ksh中实现这一点。

我发现检查$-变量中的'I'标志是ksh中最好的方法

if [[ $- = *i* ]]; then
    #do interactive stuff     
fi

也可以使用“tty-s”:

返回以下退出值:
0标准输入是一个终端。
1标准输入不是终端。

运行脚本时,正确的KSH shell不应调用$ENV。然而,在Sun的
ksh88
方言中,这是一个长期存在的bug,实际上在Solaris10的后期进行了修补。(相当令人惊讶,因为Sun传统上非常不愿意改变现有的行为,因为这样的修复可能会破坏工作区。在这种情况下,标准占了上风。)

为了防止sun的ksh88在运行脚本时读取$ENV,通常使用
#/bin/ksh-p
解决方案,如果没有
/etc/suid_配置文件
的话,它是安全的

但是,$-是交互式shell的可靠指示器,除非您伪造它

$ cat interactive.sh
echo \$- = $-
[[ $- == *i* ]] && echo interactive || echo not interactive
当作为脚本运行时,这将提供:

$ ksh  interactive.sh
$- = hB
not interactive
当我们伪造
-i
标志时,您将得到您想要的:

$ ksh  -i interactive.sh
$- = imBE
interactive
当从交互式shell中获取时,它将做正确的事情:

$ . ./interactive.sh
$- = imsBEl
interactive

“-t N”是确定的吗?是否存在与“$-=i”不同的情况?
test-t0
用于测试,无论
stdin
是否为终端。这与shell是否是交互式的不同。例如,您可以运行一个shell脚本,从tty读取。@MikhailT shell脚本通常不源于~/.bashrc,是吗?您的意思是,
~/.kshrc
,@grawity。ksh和bash仅当它们是交互式shell时才读取各自的
~/.foorc
文件。没有必要在那里检查它。但是,如果您确实进行了检查,那么最好正确地进行检查…@MikhailT您可能希望通知Debian这一点,因为他们的bash副本的编译方式不同,并且对于批SSH登录也读取.bashrc。
$ cat interactive.sh
echo \$- = $-
[[ $- == *i* ]] && echo interactive || echo not interactive
$ ksh  interactive.sh
$- = hB
not interactive
$ ksh  -i interactive.sh
$- = imBE
interactive
$ . ./interactive.sh
$- = imsBEl
interactive