• Shell 备忘录


    此文收集工作中用到的Shell备忘,随用随机:

    1.比较

    -eq       等于,如:if [ "$a" -eq "$b" ]
    -ne       不等于,如:if [ "$a" -ne "$b" ]
    -gt       大于,如:if [ "$a" -gt "$b" ]
    -ge       大于等于,如:if [ "$a" -ge "$b" ]
    -lt       小于,如:if [ "$a" -lt "$b" ]
    -le       小于等于,如:if [ "$a" -le "$b" ]

      -z       字符串为"null".就是长度为0. if [ -z "$a" ]

      -n       字符串不为"null" if [ -n "$a" ]

    2.检测变量类型

    # check value type
    function check(){
            local a="$1"
            printf "%d" "$a" &>/dev/null && echo "integer" && return
            printf "%d" "$(echo $a|sed 's/^[+-]?0+//')" &>/dev/null && echo "integer" && return
            printf "%f" "$a" &>/dev/null && echo "number" && return
            [ ${#a} -eq 1 ] && echo "char" && return
            echo "string"
    }

    3. 计算文件行数

    max_count=$(wc -l ./host.list |awk '{ print $1 }')

    4.for 循环读文件

    for ip in $(head -n 20 ./host.list)
    do
    echo $ip
    ssh $ip 'hostname'
    done;

    5.用密码自动登录

    auto_login_ssh () {
        expect -c "set timeout -1;
                    spawn -noecho ssh -o StrictHostKeyChecking=no $2 ${@:3};
                    expect *assword:*;
                    send -- $1
    ;
                    interact;";
    }
    auto_login_ssh password root@10.10.10.10


    6.执行远程机器命令,如查找文件

        fr=$(echo `ssh root@$ip "find / -name $1"`)

         if [ "$fr" == "" ]

            then

              echo "Not Found "

            else

              echo "Found it: $fr"

            fi

    7.while 遍历文件

    while read line
    do
      echo $line
    done < ./host.list

    8.输入隐藏并验证密码,echo 不换行

    while [ -z $bigpass ] || [ "$bigpass" !=  "$bigpass2" ]
    do
      if [ -n "$bigpass" ]
      then
        echo "Sorry, passwords do not match. pls retry"
      fi
      echo -n "Password:"
      stty -echo
      echo ""
      read bigpass
      stty echo
      echo -n "Confirm password:"
      stty -echo
      read bigpass2
      stty echo
      echo ""
    done

    9.输出本地格式日期

    echo  "$(date +%Y%m%d%H%M%S).old"

     10.算术运算

    运算符号依旧是 + - * /
    不过使用时候要注意:
    
    r=`expr 4 + 5`
    r=$(( 4 + 5 ))
    r=$[ 4 + 5 ]
    let r=4 + 5
    
    r=`expr 4 * 5`
    r=$(( 4 * 5 ))
    r=$[ 4 * 5 ]
    let r=4 * 5
    
    r=`expr 40 / 5`
    r=$(( 40 / 5 ))
    r=$[ 40 / 5 ]
    let r=40/5 
    
    乘幂 (如 23 次方)
    r=$(( 2 ** 3 ))
    r=$[ 2 ** 3 ]
    expr 沒有乘幂

     11. Eclipse 的Shell 编辑插件ShellEd

    http://sourceforge.net/apps/trac/shelled/wiki/Documentation/InstallGuide

    12. 输出彩色字符

    #!/bin/sh
    NORMAL=$(tput sgr0)
    GREEN=$(tput setaf 2; tput bold)
    YELLOW=$(tput setaf 3)
    RED=$(tput setaf 1)
    
    function red() {
        echo -e "$RED$*$NORMAL"
    }
    
    function green() {
        echo -e "$GREEN$*$NORMAL"
    }
    
    function yellow() {
        echo -e "$YELLOW$*$NORMAL"
    }
    
    # To print success
    green "Task has been completed"
    
    # To print error
    red "The configuration file does not exist"
    
    # To print warning
    yellow "You have to use higher version."

     13. Shell 中的特殊变量

    $0:当前脚本的文件名
    
    $num:num为从1开始的数字,$1是第一个参数,$2是第二个参数,${10}是第十个参数
    
    $#:传入脚本的参数的个数
    
    $*:所有的位置参数(作为单个字符串)
    
    $@:所有的位置参数(每个都作为独立的字符串)。
    
    $?:当前shell进程中,上一个命令的返回值,如果上一个命令成功执行则$?的值为0,否则为其他非零值,常用做if语句条件
    
    $$:当前shell进程的pid
    
    $!:后台运行的最后一个进程的pid
    
    $-:显示shell使用的当前选项
    
    $_:之前命令的最后一个参数

     14. 删除文件中的某一行

    比如:在1.txt里有以下内容:
    HELLO=1
    NI=2
    WORLD=3
    I Love China.
    Love all
    ....
    
    如果是要删除第三行:
    sed -i '3d' 1.txt
    
    如果删除以Love开头的行
    sed -i '/^Love/d' 1.txt
    
    删除包含Love的行
    sed -i '/Love/d' 1.txt
  • 相关阅读:
    初来乍到
    windows小技巧:远程桌面连接rdp文件
    CSS小试牛刀
    Windows phone 7之样式与模板
    Pulldowntorefresh a WP7 ListBox or ScrollViewer(向上向下的手势)
    Windows Phone 7 文件下载进度和速度显示
    WPF Binding
    wp7学习笔记(转)
    WP7 网络请求之WebClient
    Windows Phone Tilt effect on HubTile and other Controls
  • 原文地址:https://www.cnblogs.com/biangbiang/p/3287640.html
Copyright © 2020-2023  润新知