Bash Shell 小功能实现收集

 判断命令执行后的返回值

  • 执行一个命令后,会有相应的返回值,表示该命令执行的情况
  • 脚本中捕获命令执行后的返回值
    # hello 是一本不存在的命令或者脚本
    hello
    
    if [ 127 -eq $? ]; then
            echo '127 -ne ~'
    fi
    View Code

判断参数是否为空

  • 参数不能为空
    if [ -z "$*" ]; then
            echo -e "	Warning: must has option. Please run with '-h' get help informations.
    "
            exit 1
    fi
    View Code

    判断时,双引号不能少 

     

判断当前用户是不是 root

  • 判断当前用户,不是 root 就退出
    if [ "root" == $(whoami) ]; then
            echo $(whoami)
    else
            echo -e "  Please login with root privlegess.
    "
            exit 2
    fi
    View Code

    适用必须是 root 的场合

     
  • check for root user to use script
    #check for root user
    if [ "$(id -u)" -ne 0 ] ; then
            echo "You must run this script as root. Sorry!"
            exit 1
    fi
    View Code

Bash Shell 的参数扩展

  • 三个选项,一个无参,两个有参
    while getopts ":g:s:h" opt; do
    
        case "$opt" in
            # Valid options.
            "g")
                echo -e "	Package Name: $OPTARG."
                    get $OPTARG
                ;;
            "s")
                    echo -e "	Package Name: $OPTARG."
                    set $OPTARG
                ;;
            "h")
                    help
                    ;;
            ":")    # Silent, without arg
                echo -e "	Option $OPTARG has't parameter."
                ;;
            "?")    # Not silent, without arg. And invalid option.
                echo -e "	Invalid option $OPTARG."
                ;;
            "*")    # Impossible to implement.
                echo -e "	unkown error from arg."
                ;;
        esac
    done
    View Code

服务托管

  • sysv
    #! /bin/bash
    #
    # Daemo up/down
    #
    # chkconfig: 35 10 90
    # description: start at boot time.
    #
    
    PID=
    
    case "$1" in
      start)
        /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &>/dev/null
        PID=$(pidof redis-server)
        echo $PID
        ;;
      stop)
        ;;
      status)
        echo "你来写?牛的不行!"
        ;;
      restart|reload|force-reload)
        ;;
      *)
            echo $"Usage: $0 {start}"
    esac
    exit 1
    View Code

    托管的思想:就是在控制服务启动的脚本文件中添加 【

    # chkconfig: 35 10 90
    # description: start at boot time.

    】 这么两行内容,chkconfig 指定了启动的级别(35),启动顺序,关闭顺序。description 作为脚本的功能描述。

判断参数格式

  • 代码:判断是不是以 - 段横杠开头的?
    # cat beginWith.sh 
    #!/bin/bash
    
    if [[ $1 =~ ^-.* ]]; then
        echo "是-开头的: $1" 
    else
        echo "不是-开头的: $1"
    fi
    View Code
  • 测试

    [root@seven jars]# ./beginWith.sh D
    不是-开头的: D
    [root@seven jars]# ./beginWith.sh -D
    是-开头的: -D
    View Code
  •  判断取反

    if ! [[ $1 =~ ^-.* ]]; then
    View Code

    测试通过

设定一个计数器

  • 数字赋值时,等号两侧不带空格。
    declare -i num=0
    
    until false; do
        echo $num
        if [ $num -gt 3 ]; then
            echo "ge 3"
            break
        else
            let num+=1
        fi  
    
        sleep 1
    done
    View Code

变量内字符替换

  • 把以 .jar 结尾的变量,变成以 .log 结尾。
    CURRENT_DIR=`dirname $0`
    AppName=`ls -1 $CURRENT_DIR | grep ".jar$"`
    LOGFILE=${CURRENT_DIR}/$(echo $AppName | sed 's/.jar/.log/')
    
    echo $CURRENT_DIR
    echo $AppName
    echo $LOGFILE
    View Code

一切代码都是为了生活,一切生活都是调剂
原文地址:https://www.cnblogs.com/argor/p/13042118.html