#和之间的空间!在shebang(#!/ usr / bin / ksh)

问题描述 投票:1回答:1

我正在编写一个Korn shell脚本,它使用< <()进行进程替换,如下所示:

array=()
while IFS= read -r -d '' x;do
    array+=( "$x" )
done < <(some command)

这是试图将array返回的所有字符串插入some command。奇怪的是,当我的shebang看起来像这样时,这是有效的:

# !/usr/bin/ksh

这当然是不寻常的(注意#!之间的空间)。另一方面,当我的shebang看起来像#!/usr/bin/ksh(显然是正确的方式)时,这个脚本失败并出现错误syntax error: '< ' unexpected。为什么是这样?在shebang中有空间有什么区别?谷歌给了我几个答案,说!#!/usr...之间的空间是可以的,但没有任何关于!#之间的空间。

shell ksh shebang
1个回答
2
投票

# ! is an invalid shebang, and entirely ignored. Behavior of a script with no shebang depends on how you invoke it.

如果从shell调用:有些shell使用/bin/sh来运行这样的脚本;其他人将自己用于此目的。大概是你在测试时交互式使用的shell(并且发现脚本仅与无效的shebang一起工作)在后一组中,所以你的脚本实际上是用bash运行的,或者当时是你的活动交互式shell。

如果没有shell调用:大多数操作系统都会拒绝执行这样的二进制文件。


Real David Korn ksh93 supports process substitution correctly, but some 3rd-party clones and ancient ksh implementations don't.

如果您打算使用ksh,那么使用正版David Korn ksh93(不是mksh,pdksh或其他第三方克隆)是首选,并且(到您的直接观点)将确保过程替换支持。

© www.soinside.com 2019 - 2024. All rights reserved.