bash - 为什么进程替换在 shell 脚本中不起作用?

标签 bash shell

我正在尝试在 shell 脚本中使用 comm 命令但出现错误:

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 行:

#!/bin/sh

或者您在测试时使用 sh script.sh 而不是 bash script.sh,或者您有 SHELL=/bin/sh 或环境中设置的类似内容。你的失败在 process substitution 上代码。当 Bash 作为 sh(在 POSIX mode 中)运行时,进程替换不可用:

  1. Process substitution is not available.

你需要写:

#!/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


破译难以破译的评论

在一个难以辨认的comment ,信息似乎包括:

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。这些中的一个或两个可能是问题的主要部分。

关于bash - 为什么进程替换在 shell 脚本中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371672/

相关文章:

linux - 如何在后台自动运行bash脚本?

linux - 使用查找和 grep 的 Bash 脚本

git - 在 git 中,如何仅从更改的行中删除 Windows 行结尾?

bash - grep 从两个变量

c - 重定向 I/O,在 C 中实现 shell

linux - Bash 句柄退出多个进程

bash - 为什么我的 Curl 命令大部分时间都无法下载文件,但有时却可以?

linux - Bash 脚本 : if any argument is "N" then function has extra options

php - netstat 和 lsof 的执行问题

bash - 如何将列表作为环境变量传递给 Terraform 使用?