Shell脚本片段大全

[TOC]

MISC

  1. 使用telnet连接服务器,在没有装客户端的情况下

    telnet 10.11.2.212 1332
    auth fjionesafnei #如果有密码
    
  2. 文件中读取一行赋值给变量

    cat $SOURCE | grep ERROR | while read i
    do
        echo $i
    done
    
  3. 执行命令赋值给变量

    read j <<< `$REDIS hget $MAPKEY ${i}`
    
  4. 一种优雅的判断

    [ $? -eq 0 ] && rm -f $file
    
  5. SSH登陆命令

    SSH="sudo -u $SSH_USER ssh"
    $SSH $host "uname -s" 2>/dev/null
    $SSH $host "export LANGUAGE=$LANGUAGE;$cmd"
    
  6. 获取当前项目的绝对路径

    THIS_FILE=`pwd`"/"$0
    DEPLOY_TOOLS_DIR=`dirname $THIS_FILE`
    
  7. 使用expect实现省输入密码登陆

    #!/usr/bin/expect
    set timeout 60
    set username [lindex $argv 0]
    set password [lindex $argv 1]
    set src_file [lindex $argv 2]
    set dest_file [lindex $argv 3]
    set host [lindex $argv 4]
    spawn scp $username@$host:$src_file $dest_file
    expect {
        "(yes/no)?"
        {
            send "yes\n"
            expect "*assword:" { send "$password\n"}
        }
        "*assword:"
        {
            send "$password\n"
        }
    }
    expect "100%"
    expect eof
        
    SCP="sudo -u $SSH_USER expect ./expect_scp_from_remote $SSH_USER $SSH_PWD"
    $SCP $REMOTE_DIR$REMOTE_FILE $LOCAL_DIR $host
    
  8. 数学计算

    no=`echo "$no + 1" | bc`
    
  9. 打印数组,上面不带下标下面带下标

    for element in ${array[@]}
    #也可以写成for element in ${array[*]}
    do
        echo $element
    done
        
        
        
    for i in "${!arr[@]}";
    do
        printf "%s\t%s\n" "$i" "${arr[$i]}"
    done
    

    MySQL

  10. MySQL连接操作。

     USER='mysqldev'
     PWD='xxxxxxxxxxxxx'
     PORT='3306'
     IP='10.16.15.157'
     DBNAME='admin_tools'
     MYSQL="/usr/local/mysql/bin/mysql -u$USER -p$PWD -P$PORT -h$IP $DBNAME"
    
  11. MySQL简单sql操作。

     $MYSQL -e "$SQL"
    
  12. MySQL从文件中读取SQL执行

     $MYSQL <$FILE >$OUT
    
  13. MySQL结果赋值给数组(declare代表未初始化也能直接赋值)

     declare RAWLIST=(`$MYSQL -e "$SQL" --skip-column-name`)
    

REDIS

  1. REDIS连接

    RHOST='10.142.234.60'
    RPORT='5316'
    RPASS='xxxxxxxxxxxxx'
    REDIS="/usr/local/bin/redis-cli -h $RHOST -p $RPORT -a $RPASS"
    
  2. 执行命令并传到变量中

    read j <<< `$REDIS hget $MAPKEY ${i}`
    

传参

  1. 一个参数的检测

    if [ $# -eq 0 -o ! -f "$1" ];then
        echo "Params error!"
        exit 1
    fi
    
  2. 帮助命令

    if [ $# -ne 1 ] || [ "-h" = "$1" ] || [ "--help" = "$1" ]
    then
        echo "用法: $0 date"
        exit 0;
    fi
    

常用值

  1. 服务器IP

    IP=`ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk '{print $1}'`
    
  2. 服务器名称

    HOSTNAME=`hostname`
    
  3. 日期时间

    DATE=`date +%Y%m%d`
    DATES=`date +%Y-%m-%d_%H:%I:%S`
    
  4. 获取当前时间戳,含毫秒

    current=`date "+%Y-%m-%d %H:%M:%S"`
    timeStamp=`date -d "$current" +%s`
    currentTimeStamp=$(($timeStamp*1000+`date "+%-N"`/1000000))
    
  5. 获取过去某个时间

    YESTODAY=`date -d "1 day ago" +"%Y%m%d"`
    LASTMONTH=`date -d "1 month ago" +"%y%m%d"`
    

AWK编程

  1. 正则匹配

    awk '/white/ {print $1}'
    
  2. 调用系统函数

    export -f ip2int
    awk 'NF>1{system("ip2int "$1" "$2)}' $date > out
    
  3. 输出到文件

      awk '{print $2 > file}'
    
  4. 多个分隔符

      awk 'BEGIN{FS="[=&]"}'
      awk -F '":"|","|"},"' #支持正则,用|分割
    
  5. 变量符合正则

      awk 'BEGIN{FS="[=&]"}($3 == "m" && $4!~/HTTP/){print $4}' #不匹配正则,匹配就是~
    
  6. 去重,使用数组a,对$2进行去重

      awk 'BEGIN{FS="[=&]"}($2!~/HTTP/ && !a[$2]++){print $0}'
    
  7. 使用if,else if,else, for

      awk  'BEGIN{FS="[ ,]"}{if($2=="" || $3=="")print $0;else for(i=2;i<=NF;i++){print $1" "$i}}'
    
  8. 数组累计计数

      cat file | awk '{array[$2]++;ary[$2]+=$3}END{for(i in array) print i"\t"array[i]"\t"ary[i]/array[i]}'
    

← MySQL快速实践手册  Go语言语法 →