反引號又名readpipe
期望傳遞給sh
(或cmd
在Windows)的命令。 您似乎有一個bash
命令而不是sh
命令。修正:
`bash -c 'datamash -ft, -g 1 mean 3 mean 4 < <(tail -n +2 yourfile | sort -t, -k1,2) | cut -d, -f1,2,5,6'`
如果你有瓦爾內插,它看起來像
use String::ShellQuote (shell_quote);
my $qfn = '...';
my $tail_cmd = shell_quote('tail', '-n', '+2', $qfn);
my $bash_cmd = "datamash -ft, -g 1 mean 3 mean 4 < <($tail_cmd | sort -t, -k1,2) | cut -d, -f1,2,5,6";
my $sh_cmd = shell_quote('bash', '-c', $bash_cmd);
`$sh_cmd`
由於@chepner注意到,該bash
命令可以被轉換成一個簡單的命令與sh
兼容。這減少了第一個片段到以下幾點:
`tail -n +2 yourfile | sort -t, -k1,2 | datamash -ft, -g 1 mean 3 mean 4 | cut -d, -f1,2,5,6'`
這不利於我們從第二個片段使用shell_quote
脫身,但它確實降低其IT以下幾點:
use String::ShellQuote (shell_quote);
my $qfn = '...';
my $tail_cmd = shell_quote('tail', '-n', '+2', $qfn);
my $sh_cmd = "$tail_cmd | sort -t, -k1,2 | datamash -ft, -g 1 mean 3 mean 4 | cut -d, -f1,2,5,6";
`$sh_cmd`
雙引號字符串中只有四個特殊字符(包括反引號)是:'\','$','@'和分隔符。 – ikegami