shell example01
生活随笔
收集整理的這篇文章主要介紹了
shell example01
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
條件判斷
if [[ -e ${1} ]]; thenecho "$(tput setaf 2) found ${1} $(tput sgr0)"cat ${1}
elseecho "$(tput setaf 1) not found ${1} $(tput sgr0)"exit 1
fi//簡化[[ -e ${1} && -e ${2} ]] && cat ${1} > ${2}//判斷取反txt='4.txt'if ! [[ -e ${txt} ]]; thentouch ${txt}echo "touch ${txt}; name length ${#txt}"
fi//多重選擇bold=$(tput bold)
underline=$(tput smul)
normal=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)if [[ -e 'a1.txt' ]]; thenecho "${bold}${underline}${green} search a{1..5}.txt ${normal}"ls a{1..5}.txt 2> log.txt
elif [[ -e 'b1.txt' ]]; thenecho "${bold}${underline}${green} search b{1..5}.txt ${normal}"ls b{1..5} txt 2>> log.txt
elseecho "${bold}${underline}${red} create a{1..4}.txt ${normal}"touch a{1..5}.txt
fi 顯示時間
//查找文件
start=$(date +%s)
echo 'start search'find ~/ -name '*.txt' &> log.txtecho 'end search'end=$(date +%s) //%s以秒為單位
difference=$(( end - start))
echo "Time is ${difference} seconds".//顯示具體的執行時間
echo 'start search'
time find ~/ -type f -name '*txt' &> log.txt //僅僅是文件
echo 'end search'//計時器
echo '- Count Start'
tput sc //存儲光標位置
count=0while [[ ${count} -lt 20 ]]
dolet count++sleep 1 //延遲一秒tput rc //恢復光標位置tput ed //清除從當前光標位置到行尾之間的所有內容echo "- ${count}"
doneecho "- Count End" 循環
//循環創建文件
for i in file{1..9}.txt
doif [[ -e ${i} ]]thenecho "find ${i} skip"elseecho "start create ${i}"touch ${i}
fi
done
echo "finished"//判斷空文件
files=$(echo *.txt)
normal=$(tput sgr0)
type="%-10s %-8s\n"printf "$(tput bold)${type}" name blank
printf ${normal}
for file in ${files}; docheck=truecolor=$(tput setaf 1)test -s ${file} && check=false color=$(tput setaf 2)printf "${color}${type}" ${file} ${check}printf ${normal}
done 簡單編輯
//去除空白
cat $1 | sed '/s //g' > new.log //sed 's/要被取代的字串/新的字串/g' //對特定格式的文檔計算重復值
//log.txt
account1:passwd1
account2:passwd2
account3:passwd3
account2:passwd4
account4:passwd5
account6:passwd6
account1:passwd7//
awk -F: 'x[$1]++ {print $1 " is duplicated"}' log.txt //awk -F 指定分割字符
轉載于:https://www.cnblogs.com/jinkspeng/p/5125677.html
總結
以上是生活随笔為你收集整理的shell example01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刚结婚准备要孩子,是先去做孕前检查还是顺
- 下一篇: 做一个app多少钱啊?