条件判断
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 指定分割字符