• shell example01


    条件判断

    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
    "
    
    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 指定分割字符 
    
  • 相关阅读:
    第10节--单行函数之数学函数
    第九节--单行函数之日期函数
    【译】什么是游戏开发实体系统框架(终)
    【译】什么是游戏开发实体系统框架(一)
    【译】游戏《实体系统框架》译文——序
    IDEA 快捷键
    Java学习笔记1
    Java面试题
    Java学习笔记——集合
    Java学习笔记——String类常用方法
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/5125677.html
Copyright © 2020-2023  润新知