Shell脚本错误:"头:无效的尾随选项 - 1 QUOT; [英] Shell Script error: "head: invalid trailing option -- 1"
问题描述
我有这样的code在我的shell(bash)的脚本分割文件成较小的部分:
I have this code in my shell(bash) script for splitting a file into smaller parts:
for (( i=$start; i<=$lineCount; i=i+$interval))
do
temp=`expr $i + $interval`;
if [ $temp -le $lineCount ]
then
command="head -$temp $fileName | tail -$interval > $tmpFileName";
echo "Creating Temp File: $command";
else
lastLines=`expr $lineCount - $i`;
command="head -$temp $fileName | tail -$lastLines > tmpFileName";
echo "Creating Temp File: $command";
fi
`$command`;
done
它可以打印标准输入输出如下:
It prints the following output on stdin:
Creating Temp File: head -10 tmp.txt | tail -10 > tmp.txt_TMP
head: invalid trailing option -- 1
Try `head --help' for more information.
但打印的命令:头-10 tmp.txt |尾-10 GT; tmp.txt_TMP
正确运行在命令行上。
我在做什么错了?
推荐答案
当你把管道 |
在一个变量,外壳间$ P $点它作为一个普通字符,而不是作为一个管道。同上重定向操作符,如&GT;
,&LT;
...
When you put the pipe |
in a variable, the shell interprets it as an ordinary character and not as a pipe. Ditto for redirection operators like >
, <
, ...
这是丑陋的方式是使用评估
。
An ugly way would be to use eval
.
有一个更好的办法是,以您的命令拆分成不同的部分,从而摆脱管道和重定向运营商在里面。
A better approach would be to split your command into different parts so as to get rid of pipes and redirection operators in it.
例如:
command="head -$temp $fileName | tail -$lastLines > tmpFileName";
将被写为:
cmd1="head -$temp $fileName";
cmd2="tail -$lastLines";
和说执行:
"$cmd1" | "$cmd2" > tmpFileName;
此外,不需要反引号来执行存储在一个变量的命令。简单的说:
Moreover, you don't need backticks to execute a command that is stored in a variable. Simply say:
$command
这篇关于Shell脚本错误:&QUOT;头:无效的尾随选项 - 1 QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!