shell example01
2016-01-12 21:26
591 查看
条件判断
if [[ -e ${1} ]]; then echo "$(tput setaf 2) found ${1} $(tput sgr0)" cat ${1} else echo "$(tput setaf 1) not found ${1} $(tput sgr0)" exit 1 fi //简化 [[ -e ${1} && -e ${2} ]] && cat ${1} > ${2} //判断取反 txt='4.txt' if ! [[ -e ${txt} ]]; then touch ${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' ]]; then echo "${bold}${underline}${green} search a{1..5}.txt ${normal}" ls a{1..5}.txt 2> log.txt elif [[ -e 'b1.txt' ]]; then echo "${bold}${underline}${green} search b{1..5}.txt ${normal}" ls b{1..5} txt 2>> log.txt else echo "${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.txt echo '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=0 while [[ ${count} -lt 20 ]] do let count++ sleep 1 //延迟一秒 tput rc //恢复光标位置 tput ed //清除从当前光标位置到行尾之间的所有内容 echo "- ${count}" done echo "- Count End"
循环
//循环创建文件 for i in file{1..9}.txt do if [[ -e ${i} ]] then echo "find ${i} skip" else echo "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}; do check=true color=$(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 指定分割字符
相关文章推荐
- Bash定制化
- erlang的shell历史记录
- SSH Secure Shell Client 连接Linux 乱码
- Shell快捷键
- 查看服务器RAID卡信息的SHELL脚本及MegaCLI命令介绍
- OS之子进程简单模拟shell解释环境(再次)
- bash脚本编程 条件判断 二
- linux shell中 "" '' ``的区别
- shell中大于、等于、小于
- jdbc与 Beanshell PostProcessor 对多条结果的处理
- .bashrc 文件中增加头文件路径和库文路径
- Linux shell 学习
- shell判断文件是否有变化
- xshell密钥登录和密钥登录注意
- Linux shell下的颜色含义
- linux shell 快捷键
- ls -bash: ls: command not found (大多数命令不灵)
- echo命令写shell
- bash脚本编程 变量 一
- 搭建spark cluster过程中 scp传送文件 文件权限管理 磁盘及文件使用情况