Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash shell脚本中的粘贴命令_Bash_Shell_Sh_Paste - Fatal编程技术网

Bash shell脚本中的粘贴命令

Bash shell脚本中的粘贴命令,bash,shell,sh,paste,Bash,Shell,Sh,Paste,我正在尝试在以下脚本中合并来自两个不同文件的列: #!/bin/sh # # echo "1 1 1" > tmp1 echo "2 2 2" >> tmp1 echo "3 3 3" >> tmp1 echo "a,b,c" > tmp2 echo "a,b,c" >> tmp2 echo "a,b,c" >> tmp2 paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f

我正在尝试在以下脚本中合并来自两个不同文件的列:

#!/bin/sh
#
#

echo "1 1 1" > tmp1
echo "2 2 2" >> tmp1
echo "3 3 3" >> tmp1

echo "a,b,c" > tmp2
echo "a,b,c" >> tmp2
echo "a,b,c" >> tmp2

paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f 1-2 tmp2)
但是,当我运行时,它不起作用

bash test.sh
sh test.sh
我得到以下错误消息

test.sh: line 13: syntax error near unexpected token `('
test.sh: line 13: `paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f 1-2 tmp2)'
test.sh:第13行:意外标记“(”附近的语法错误

test.sh:第13行:'paste-d':'在您的系统上,
sh
可能未设置为
bash
dash
可能是?)


进程替换,
您可以使用文件描述符以可移植的方式实现这一点

while
    IFS=" " read -r x rest <&3
    IFS="," read -r y z rest <&4
do
    echo "$x:$y:$z"
done 3<tmp1 4<tmp2

使用dash进行测试,并且是独立的程序(但在某些系统上,出于兼容性原因,
sh
指向
bash
)。实际上没有理由使用
sh
@dimo414,当然,除非您试图编写一个脚本,该脚本将在任何符合POSIX标准的系统上工作,无论是否安装了
bash
。@chepner因此“有效”:但是,如果您的目的是编写一个Bash脚本,而不是试图支持每个只能使用POSIX的死水环境,那么遵从
sh
是一个不必要的麻烦。在“不需要
sh
兼容性”和“没有理由使用
sh
”之间存在着巨大的差异,“有效”一词无法表达的差异。
1:a:b
2:a:b
3:a:b
trap 'rm p1 p2' EXIT
mkfifo p1 p2
cut -d " " -f1 tmp1 > p1 &
cut -d " " -f 1-2 tmp2 > p2 &

paste -d':' p1 p2