为什么进程替换在Shell脚本中不起作用? [英] Why does process substitution not work in a shell script?

查看:98
本文介绍了为什么进程替换在Shell脚本中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Shell脚本中通信命令,但出现错误:

I am trying to comm command in shell script but getting an error:

a.sh: command substitution: line 1: syntax error near unexpected token `('
a.sh: command substitution: line 1: `comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l'

代码段-

temp=`comm -12 <( sort /home/xyz/a.csv1 | uniq) <( sort /home/abc/tempfile | uniq) | wc -l`
echo $temp

推荐答案

目前尚不完全清楚,但是很有可能脚本顶部的shebang行不正确:

It isn't entirely clear yet, but the chances are very high that you either have an incorrect shebang line at the top of the script:

#!/bin/sh

,或者在测试时使用的是sh script.sh而不是bash script.sh,或者您在环境中设置了SHELL=/bin/sh或类似的设置.您的失败是在流程替换代码上.当Bash以sh身份运行时(在 POSIX模式下),则无法使用进程替换:

or you are using sh script.sh instead of bash script.sh while testing it, or you have SHELL=/bin/sh or something similar set in the environment. Your failure is on the process substitution code. When Bash is run as sh (in POSIX mode), then process substitution is not available:

  1. 无法使用进程替换.

您需要写:

#!/bin/bash

temp=$(comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l)
echo $temp

甚至简单地:

#!/bin/bash

comm -12 <(sort -u /home/xyz/a.csv1) <(sort -u /home/abc/tempfile) | wc -l

将获得与回声捕获相同的效果.在测试时,请使用bash -x script.shbash script.sh.

which will achieve the same effect as the capture followed by the echo. When testing, use bash -x script.sh or bash script.sh.

以难以理解的评论,该信息似乎包括:

In an indecipherable comment, the information appears to include:

BASH =/bin/sh
BASHOPTS = cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progco‌mp:promptvars:sourcepath
BASH_ALIASES =()
BASH_ARGC =()
BASH_ARGV =()
BASH_CMDS =()
BASH_LINENO =([0] ="0")
BASH_SOURCE =([0] ="a.sh")
BASH_VERSINFO =([0] ="4" [1] ="1" [2] ="2" [3] ="1" [4] =版本" [5] ="x86_64-redhat-linux-gnu )
BASH_VERSION ='4.1.2(1)-发行版'
CVS_RSH = ssh
SHELL =/bin/bash
SHELLOPTS = braceexpand:hashall:interactive-comments:posix
SHLVL = 2

BASH=/bin/sh
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progco‌mp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="a.sh")
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
CVS_RSH=ssh
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=2

请注意,BASH=/bin/shSHELLOPTS=braceexpand:hashall:interactive-comments:posix.这两者之一或两者都可能是问题的主要部分.

Note that BASH=/bin/sh and SHELLOPTS=braceexpand:hashall:interactive-comments:posix. Either or both of these might be a major part of the problem.

这篇关于为什么进程替换在Shell脚本中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆