我的shell(bash)脚本中有以下代码,用于将文件拆分为较小的部分:
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
Creating Temp File: head -10 tmp.txt | tail -10 > tmp.txt_TMP
head: invalid trailing option -- 1
Try `head --help' for more @R_877_4045@ion.
但是命令显示为:head -10 tmp.txt |尾巴-10> tmp.txt_TMP在命令行上正确运行.
我究竟做错了什么?
解决方法:
当你把管子在变量中,shell将其解释为普通字符而不是管道.重定向运算符(如>,<,...)的同上 一个丑陋的方法是使用eval. 更好的方法是将命令拆分为不同的部分,以摆脱其中的管道和重定向运算符. 例如:
command="head -$temp $fileName | tail -$lastLines > tmpFileName";
将被写为:
cmd1="head -$temp $fileName";
cmd2="tail -$lastLines";
然后说:
"$cmd1" | "$cmd2" > tmpFileName;
而且,您不需要反引号即可执行存储在变量中的命令.简单地说:
$command
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。