SHELL2. 打印文件的最后5行
描述
经常查看日志的时候,会从文件的末尾往前查看,于是请你写一个 bash脚本以输出一个文本文件 nowcoder.txt中的最后5行#include<iostream> using namespace std; int main() { int a = 10; int b = 100; cout << "a + b:" << a + b << endl; return 0; }
int a = 10; int b = 100; cout << "a + b:" << a + b << endl; return 0; }
Bash 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2021-05-11
#!/bin/bash while read line do row=$((row+1)) done<nowcoder.txt while read line do if [ $row -lt 6 -a $row -gt 0 ] then echo $line fi row=$((row-1)) done<nowcoder.txt
Bash 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2021-05-23
#! /bin/bash while read line do row=$((row+1)) done<nowcoder.txt while read line do if [ $row -lt 6 -a $row -gt 0 ] then echo $line fi row=$((row-1)) done<nowcoder.txt
Bash 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2021-05-11
row=0 while read line do row=$((row+1)) done < nowcoder.txt while read line do if [ $row -lt 6 -a $row -gt 0 ] then echo $line fi row=$((row-1)) done<nowcoder.txt
Bash 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2021-05-08
row=0 while read line do ((row++)) done < ./nowcoder.txt while read line do if [ $row -le 5 -a $row -ge 1 ] then echo $line fi row=$(($row-1)) done < ./nowcoder.txt
Bash 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2021-04-26
#!/bin/bash row=0 while read line do row=$((row+1)) done<nowcoder.txt while read line do if [ $row -lt 6 -a $row -gt 0 ] then echo $line fi row=$((row-1)) done<nowcoder.txt