2014马哥Linux0217-4中LineCount的小程序补完版

上一篇因为没学会字符测试,所以写的文件数行脚本,运行起来并不是那么完美,看完0218-1课后,修改代码成如下,基本没有不理想的状态发现了:

#!/bin/bash

#

read -t 10 -p "Please enter a file path in 10 seconds:" fileName #超时设置成10秒

fileName=${fileName:-null} #变量默认值的设定

if [ "$fileName" == null ];then #字符测试的语句

  echo -e "\033[31mTime out!Bad file name input.\033[0m" #这句写成了红色

  exit 4 #自定义退出代码,可以随意

elif grep "^$" $fileName &> /dev/null;then

  echo "$fileName has `grep -v "^$" $fileName | wc -l` lines and `grep "^$" $fileName | wc -l` space lines."

else

  echo "$fileName has `grep -v "^$" $fileName | wc -l` lines and has no space line."

fi


这下算比较完美了,知识盲点会导致程序bug,呵呵。接下来还有文件测试的内容要学习。

你可能感兴趣的:(linux,颜色,if,字符测试)