1.在sh腳本中獲得系統當前時間作爲變量的值
#日期
date=$(date +%F)#時間
timer=$(date +%T)
#納秒
ns=$(date +%N)
在shell中,沒有可以直接獲得毫秒的命令,通過下面的方法獲得系統當前的毫秒
#毫秒ms=${ns:0:3}
2.判斷文件是否存在,判斷路徑是否存在以及是否具有可執行權限
if [ ! -x ${filePath} ]
then
mkdir ${filePath}
fi
fileName=${date}${timer}${ns}.temp
tempfile=${filePath}${fileName}
#這裏的-f參數判斷文件是否存在,如果存在則刪除再創建一個新的空的文件
if [ -f ${tempfile} ]
then
rm ${tempfile}
touch ${tempfile}
fi
3.sh中分割輸入的字符串
command=`echo ${@}|awk -F '++' '{print $1}'`
interval=`echo ${@}|awk -F '++' '{print $2}'`
fileName=`echo ${@}|awk -F '++' '{print $3}'`
4.sh中無限循環執行一段代碼
do
echo "do something"
sleep 5
done
5.sh中的邏輯運算與或非(and、or、!)
while [ 1 -eq 1 ]
do
#日期
date=$(date +%F)
#時間
timer=$(date +%T)
#納秒
ns=$(date +%N)
#毫秒
ms=${ns:0:3}
beginStr="*******start:${date} ${timer}.${ms}"
echo ${beginStr} >> ${fileName}
${command} >> ${fileName}
echo ${endStr} >> ${fileName}
echo "producering "${command}" to "${fileName}",interval time is "${interval}
sleep ${interval}
done
else
echo "please input the parameters!!!"
fi