在shell脚本中模仿尾巴命令时获取错误,而无需使用内置的尾巴命令
我编写了这段代码来模仿 tail 命令,而不使用内置命令,尽管语法正确,但它在 for 循环中给出错误! 我无法弄清楚错误到底在哪里? 还有预期输出: 它应该打印文件中的最后 n 行。 n 是从用户处扫描的。
#!/bin/sh echo -n "输入文件名:" read file_name echo -n "输入行数:" read n touch $file_name str="end" temp="temp" echo "开始输入文件数据并写 end 停止:" while [ 1 ] do read string touch $temp echo $string > ; $temp grep -w "$str" $temp if [ $? -eq 0 ] 然后中断 else cat $temp >> $file_name fi 完成 rm $temp echo "---------------------------------------- ----------------”total_lines=wc-l< $file_name echo $total_lines a=
expr $total_lines - $nexec <; $file_name for ((i=$a;i<$total_lines;i++ )) do read a1 echo $a1 done
I have written this code to imitate tail command without using inbuilt command and it is giving error in for loop though syntax is correct!
I am not able to figure it out where actually the error is?
Also Expected output:
It should print last n number of lines from the file.
n is scanned from user.
#!/bin/sh echo -n "Enter file name: " read file_name echo -n "Enter number of lines: " read n touch $file_name str="end" temp="temp" echo "Start entering data of file and write end for stopping: " while [ 1 ] do read string touch $temp echo $string > $temp grep -w "$str" $temp if [ $? -eq 0 ] then break else cat $temp >> $file_name fi done rm $temp echo "---------------------------------------------------------" total_lines= wc -l < $file_name echo $total_lines a=
expr $total_lines - $nexec < $file_name for ((i=$a;i<$total_lines;i++ )) do read a1 echo $a1 done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论