为什么进程替换在Shell脚本中不起作用

为什么进程替换在Shell脚本中不起作用

本文介绍了为什么进程替换在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/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脚本中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:20