linux 脚本 之 简化工作

来源:互联网 发布:windows屏保 编辑:程序博客网 时间:2024/06/16 03:09

#!/bin/bash

####调用另一个命令,生成0-23点的不同时间的报告,之前都是在前台要进行24个报告的添加,点击量挺多的,尤其是每次测试时都要重复点击。

str1='{"action": "add", "data": {"start_day": 0, "object": "Reports", "name": "'
str2='", "start_hour":'
str3=', "flag": 1, "start_minute": 0, "type": 1, "template": 1}, "user": {"host": "10.64.29.145", "from": "WebUI", "name": "admin"}}'
str=""
f=aalfyzoe_1938hfuy03417924024520801.testtest #临时文件名,防止文件重名,起一个怪名字
if [ -a $f ];then
    echo "$f has existed!!"
    while true;do
    read -p "continue to use the file as input file??Y/N:"
    case $REPLY in   # REPLY 是read 的读取值的存储变量,系统定义的
    y|Y)
        echo "will use the file as input file"
        break;;
    n|N)
        echo "program exit"
        exit 1;;    
    *)                 # case 语法中默认处理
        continue;;
    esac
    done
fi
#调用形式为 ./文件名 -n 1,2,3 或者 ./文件名 -n -1, 其他调用,比如 参数为 -d 1,2,3   或者-n -m 1,2都是错的
while getopts :n: name  #第一个冒号表示忽略错误警示输出(当选项不是n时就会报错),n是选项,后面紧跟冒号表示该项需要跟随一个 值
do
    case $name in
    n)  
        oldIFS=$IFS
        IFS=','          
        substrs=($OPTARG)    # 上面的IFS 是系统变量, 这里的substrs就是把变量OPTARG的内容按照逗号分隔符,分割为一个数组,比如"1,2,3" 分割为数组(1 2 3)
        for substr in ${substrs[@]}  #数组操作 用 @或者* 可以取到整个数组内容, ${#a[@]} 可以取到数组长度
        do  
        echo "$substr" | grep "^[0-9]*$" > /dev/null   #判断一个字符串是否为数字
        if [ $? -ne 0 ];then
            echo "the number $substr is not number"
            exit 2
        fi  
        done
        for substr in ${substrs[@]}
        do
        echo "the number is : $substr"
        if [ $(($substr)) -eq -1 ];then
            number=24
            i=0
            stri=""
            while [ $i -lt $number ]
            do
                stri="$i"
                len=$(expr length $stri)  #求字符串长度
                if [ $len -eq 1 ];then
                    stri="0$stri"
                fi
                str="$str1$stri$str2$i$str3"
                echo "$str" > $f
                i=$((i+1))
                python /usr/vtm/pyscript/interface/ctmpyint.py < $f  #调用命令执行工作的任务
            done
        elif [ $((substr)) -ge 0 -a $((substr)) -lt 24 ];then   #计算数值表达式的值是否在0-23之间
            len=$(expr length "$substr")
            stri="$substr"
            if [ $len -eq 1 ];then
                stri="0$substr"
            fi
            str="$str1$stri$str2$substr$str3"
            echo "$str" > $f
            python /usr/vtm/pyscript/interface/ctmpyint.py < $f
        fi
        done
    ;;
    \?)   # 非 n 选项会被赋值为 ?
        echo "invalid options$substrs"
        echo "useage add_dailyReport.sh -n 1,2,4  or add_dailyReport.sh -n -1 to add 1 and 2 and 4 or 0~23"
        break;;
    *)  
        echo "unknow error the argument:$substr"

        break;;

    esac

done
原创粉丝点击