微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在shell脚本中使用<会导致语法错误?

我正在写一个名为myShellScript.sh的shell脚本,其中我有以下文本:

echo *** Print out this line **** diff <(./myProgram) <(./otherProgram)

但是,当我运行sh myShellScript.sh我得到以下错误

-bash-4.2$ sh myShellScript.sh myShellScript.sh **** Print out this line **** myShellScript.sh myShellScript.sh: line 2: Syntax error near unexpected token `(' myShellScript.sh: line 2: `diff <(./myProgram) <(./otherProgram)'

“无法统计远程文件:没有这样的文件或目录”:在shell脚本中使用FTP从* nix到Win

如果正在运行,请重新启动dropBox-daemon

maven pom在子目录中调用其他的poms

在bash文件中find最常用的行

如何从Linux中的HTTP MIME编码消息中提取文件数据?

错误时,sudo apt-get安装PHP5-cli

使用mailx和bash脚本parsing邮件正文

我如何写pid到文件

ACL如何影响ls / stat()结果?

合并sorting的文件,最小的缓冲

使用<(...)运算符的进程替换是一个bash特性。 你会得到这个错误信息,因为你的脚本被执行为别的东西(例如dash ),或者是旧版本的bash ,或者是在POSIX兼容模式下运行,而非POSIX功能,如进程替换被禁用(谢谢@ chepner !)

如果你想用一个功能的bash执行脚本,你有两个选择:

用bash运行脚本:

bash myShellScript.sh

将脚本的第一行设置为#!/bin/bash (或者是系统中bash的路径),然后像这样运行脚本:

./myShellScript.sh

你需要用bash执行你的脚本,而不是sh。

您正在使用进程替换,这不是一个标准的POSIX shell功能。 sh是POSIX兼容的shell,所以它不支持像进程替换这样的语言扩展。 如果以sh调用Bash,Bash将启用POSIX兼容性。

因此,您应该使用bash执行需要Bash特定功能的脚本。

你显然使用bash ,但是对于任何读这个需要使用shell而不支持进程替换的人来说,你可以使用下面的代码

# Instead of diff <(./myProgram) <(./otherProgram) # A pair of named pipes to avoid using disk space # for the output of myProgram and otherProgram mkfifo myProgram.output mkfifo otherProgram.output ./myProgram > myProgram.output & ./otherProgram > otherProgram.output & diff myProgram.output otherProgram.output rm myProgram.output otherProgram.output

这与bash在某些平台上如何执行流程替换几乎完全相同。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐