GDRCU_shell_script.sh

#!/bin/sh
#===================

功能: 判斷文件是否存在,存在顯示頭N行,不存在輸出 “抱歉,文件 ${filename} 不存在!”

#===================
usage(){
cat <<!
用法:
$0 filename number
filename 文件名
number 顯示行數

#sh -x /home/huang/shell_script.sh /home/huang/ods/20200522/shgold_20200522.json.txt 2
#sh /home/huang/shell_script.sh /home/huang/ods/20200522/shgold_20200523.json.txt 2
exp:sh $0 /home/huang/ods/20200522/shgold_20200522.json.txt 2
!
exit -1
}

if [ $# -lt 2 ]
then
echo “參數有誤,請檢查…”
usage
fi

得到輸入參數

filename=$1
number=$2

日誌文件

LOGFILE="/home/ap/ods/file_path.log"

記錄日誌的函數

log_echo()
{
echo “[date +'%Y-%m-%d %T'] $1” | tee -a ${LOGFILE}
}

main

#log_echo “INFO: | 程序KaTeX parse error: Expected 'EOF', got '#' at position 11: 0開始運行..." #̲log_echo "INFO:…{LOGFILE}”

if [ -f ${filename} ]
then
cat ${filename} |head -n ${number}
else
log_echo “抱歉,文件 ${filename} 不存在!”
exit
fi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章