zoukankan      html  css  js  c++  java
  • bash编程,while嵌套case语句, file不能判断文件存在与否

    写一个脚本, 完成如下要求
    (1)脚本可接受参数 : start, stop, restart, status,
    (2)如果参数非非法, 提示使用格式后报错退出;
    (3)如果是start, 则创建/tmp/test/SCREPT_NAME, 并显示"启动成功";
    考虑, 如果事先已启动一次, 如何处理?
    (4)如果是stop, 则删除/tmp/test/SCREPT_NAME文件, 并显示"停止完成";
    考虑: 如果事先已经停止, 如何处理?
    (5)如果是restart, 先stop, 再start
    考虑: 如果原本没有start, 如何处理?
    (6)如果是status, 则
    如果/var/lock/subsys/SCREPT_NAME存在, 则显示"SCRIPT_NAME is runing..."
    如果/var/lock/subsys/SCREPT_NAME不存在, 则显示"SCRIPT_NAME is stopped..."

    其中: SCRIPT_NAME为当前服务名;


    根据要求, 需判断两个字符串, 例如 start network

    按照文件'network'存在与否, 先分为两个循环

      按照指令'start', 确认对文件的操作

    代码如下:

    #!/bin/bash
    #
    #TEST SERVICE
    # Version 0.01
    
    declare -a dir="/tmp/test/"
    echo "TEST SERVICE: 以空格分割"
    echo "例如: start network"
    echo "===quit 退出====="
    echo "================="
    while read -p 'Command # :' a;do
        flag=`echo $a | cut -d ' ' -f1` && name=`echo $a | cut -d' ' -f2` &> /dev/null
        
        if [ -e $flag -o $name ];then #如果两个字符串都存在
            if [ $flag == 'quit' ];then exit;fi
            if [ -f $dir$name ] ;then #如果$name文件已经存在, 即服务已经启动
                echo "$dir$name已存在";
                case $flag in
                    start) echo "$name服务已启动";;
                    stop) rm -f $dir$name; echo "$name服务已停止";;
                    restart) rm -f $dir$name; touch $dir$name; echo "$name已经重新创建并启动";;
                    status) echo "$name is runing...";;
                    *) echo "Wrong using! input example : start network";;
                esac
            else #如果$name文件不存在
                echo "$name不存在";
                case $flag in
                    
                    start) touch $dir$name;echo "$name服务已创建成功";;
                    stop) echo "停止失败,${name}服务已被其他用户或程序停止";;
                    restart) touch $dir$name; echo "$name尚未启动,启动中";;
                    status) echo "$name is stopped...";;
                    *) echo "Wrong using! input example : start network";;
                    
                esac
            fi
        else #如果$flag或者$name有一个不存在, 即输入参数错误
            echo "Wrong using! input example : start network" #输入错误, 提示语法
        fi
    done

    注意: 起初在判断文件是否存在时使用 file $dir$name , 但是无论怎样判断都是返回文件已经存在, 表面原因如下:

    即, 无论 $dir$name 存在与否,  file $dir$name 的状态返回值都将是0, 即表示成功

    所以不能使用file命令来判断文件是否存在.

    man file 返回值项如下:

    RETURN CODE
    file returns 0 on success, and non-zero on error. 

    
    

    If the file named by the file operand does not exist, cannot be read, or the type of the file named by the file operand cannot be determined, this
    is not be considered an error that affects the exit status.   //如果文件不存在, 打不开, 文件格式不确定, 这些情况都不会返回error的状态码;


    运行结果:

  • 相关阅读:
    期货结算 期货算法公式
    c# websocket 服务端,客户端 兼容低版本IE,支持超长数据传输 附源码
    翻译API translate api 翻译文档api 百度翻译
    asp.net c# 网页 导出excel 多表格 多个sheet
    创建单线程单元 asp.net下使用WebBrowser
    c++ 修改内存
    c++ int to byte
    webservice 第一节 .net SoapHeader验证
    myeclipse maven编译出错
    java用正则表达式获取domain
  • 原文地址:https://www.cnblogs.com/gettolive/p/8910025.html
Copyright © 2011-2022 走看看