[英]Getting error “cat: write error: Broken pipe” only when running bash script non-interactively
I wrote a bash script where I define a variable like this: 我写了一个bash脚本,在其中定义了这样一个变量:
var=$(cat $file_path | head -n $var2 | tail -n 1 | cut -f1)
Where $file_path
simply contains the path to a file and $var2
is an int, eg, 1 or 2. The variable is therefore assigned the value of the first field of line number var2 of the file. 其中
$file_path
仅包含文件路径,而$var2
是整数,例如1或2。因此,为变量分配了文件行号var2的第一个字段的值。
It works perfectly fine when I run this from the command line. 当我从命令行运行此程序时,它工作得很好。 However, when running the script containing this command, I get the error
但是,运行包含此命令的脚本时,出现错误
cat: write error: Broken pipe
cat:写入错误:管道损坏
Any idea why that is? 知道为什么吗?
There's no need to use cat
, since head
takes a filename argument. 不需要使用
cat
,因为head
使用文件名参数。
var=$(head -n $var2 $file_path | tail -n 1 | cut -f1)
Actually, there's no need to use any of those commands. 实际上,不需要使用任何这些命令。
var=$(awk -v line=$var2 'NR == line { print $1; exit }' $file_path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.