我不得不出错,期望操作数和模棱两可的重定向。这是我的代码:
#!/bin/bash
read input >| inputfile
file_name=$(cut -d" " -f1 inputfile)
i=$(cut -d" " -f2 inputfile)
j=$(cut -d" " -f3 inputfile)
k=$(cut -d" " -f4 inputfile)
l=$(cut -d" " -f5 inputfile)
maxlinetoget=$[$l-$k]
currentlinecount=1
result=0
while read line
do
if [ $currentlinecount -ge $k && $currentlinecount -le $l ]
then
echo -n $line >| linefile
echo -n $line
for number in linefile
do
echo $number
result=$[$result+$number]
done
fi
currentlinecount=$[$currentlinecount+1]
done < $file_name
echo $result
和错误:
./P4.4: line 8: -: syntax error: operand expected (error token is "-")
./P4.4: line 24: $file_name: ambiguous redirect
第8行是:maxlinetoget=$[$l-$k]
第24行是:done < $file_name
我不知道怎么了,请告诉我。
谢谢。
两种错误都是由于两者$l
都不$file_name
具有值的结果。当未引用的变量为空时,Bash有时会产生神秘的错误消息。(如果您使用$((l-k))
而不是已弃用的内容$[$l-$k]
,或者特别是引用了您的替换内容,则可能会得到更明智的错误消息"$file_name"
。)
read line >| inputfile
从中读取一行stdin
并将其放入变量中$line
。它不产生任何输出,因此inputfile
为空。因此,以下所有cut
命令均不产生任何输出(无声无声:臭名昭著的NINO)。
您显然想做的是
read file_name i j k l rest
((maxlinetoget = l - k))
# Or maxlinetoget=$((l-k))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句