题解 | #打印文件的最后5行#
打印文件的最后5行
https://www.nowcoder.com/practice/ff6f36d357d24ce5a0eb817a0ef85ee2
#!/bin/bash arr=() # read file to arr while read p;do arr+=("$p") done < nowcoder.txt; # compare min length of filelines length=5 index=${#arr[@]} if test $index -lt 5;then length=$index;fi length=[[ $index -ge 5 ]] && 5 || $index # read last 5 line for ((i=$length;i>0;i--)); do index2=$((index - i)) echo ${arr[$index2]} done