卡在逻辑陈述中

时间:2015-08-02 05:01:58

标签: bash logic

我正在编写一个BASH脚本来从Web服务器清除缓存。该脚本旨在从位置参数中获取参数。 “ShellCheck.net”告诉我,我的剧本在功能上是正确的,但是当我测试它时,我会收到错误,我不应该......所以我想我会要求一些人重新审视它。看一看,我将在下面继续描述我的问题:

    #!/bin/bash

    #
    # Verify the user running is root, if not, fail.
    if [[ "$UID" -ne "0" ]]; #added-1438279711
        then
            echo 'Only ROOT may run this script.';
            exit 1;
    fi
    #
    # Set the variables
    BASE="/path/to/folder/foo/bar/" #added-1438279711
    DOMAINOPT="$1" #added 1438451428
    PATHOPT="$2" #added 1438451428
    #
    # Define Functions
    function usage() { #added-1438382631
        echo -en "Proper Usage:\n\n"
        echo -en "\tSpecify the domain to be used\n"
        echo -en "\tUsage: \"cleancache.sh abc.com\"\n"
        echo -en "\t\tNote: This option will search for files and folders, recursively, within the domain folder, and remove them.\n\n"
        echo -en "\tSpecify the URI you'd like to act upon within the domain\n"
        echo -en "\tUsage: \"cleancache.sh abc.com /path/to/folder/\"\n"
        echo -en "\t\tNote: This option will search for files and folders, recursively,\n\t\twithin the specified path, and remove them. Removing a single file is not currently supported with this script.\n\n"
    }
    #
    # Validate the input
    if [[ ! -z "$DOMAINOPT" ]] && [[ "$DOMAINOPT" != "^[A-Za-z0-9-]*[\.][a-z]*$" ]] #added-1438462778
        then
            clear
            echo -en "Please follow the proper format for the DOMAIN option\n\n"
            usage
            exit 1
    elif [[ ! -z "$DOMAINOPT" ]] && [[ "$DOMAINOPT" = "^[A-Za-z0-9-]*[\.][a-z]*$" ]]
        then
            DOMAINOPT="$DOMAINOPT"
    else
        clear
        echo -en "Please enter a domain!\n\n"
        usage
        exit 1
    fi
    if [[ ! -z "$PATHOPT" ]] && [[ "$PATHOPT" != "^[\/][\S]*[\/]$" ]] #added-1438456371
        then
            clear
            echo "Please follow the proper format for the PATH option"
            usage
            exit 1
    elif [[ ! -z "$PATHOPT" ]] && [[ "$PATHOPT" = "^[\/][\S]*[\/]$" ]]
        then
            PATHOPT="$PATHOPT"
    else
        echo ""
    fi
    #
    # Doing Stuff
    if [[ "$#" -gt "2" ]]
        then
            echo -en "Too many arguments!\n\n"
            usage
            exit 1
    elif [[ "$#" -eq "2" ]]
        then
            echo "Purging Cache in \"$BASE$DOMAINOPT$PATHOPT\""
            find "$BASE""$DOMAINOPT""$PATHOPT" -type d -exec rm -rf {} \;
            find "$BASE""$DOMAINOPT""$PATHOPT" -type f -exec rm -f {} \;
            echo "Purging Complete"
            exit 0
    else
        echo "Purging Cache in \"$BASE$DOMAINOPT\""
        find "$BASE" -type d -name "$DOMAINOPT" -exec rm -rf {} \;
        mkdir -p "$BASE$DOMAINOPT" && chown apache:apache "$BASE$DOMAINOPT" && chmod 755 "$BASE$DOMAINOPT"
        echo "Purging Complete!"
        echo "Creating \".stat\" file"
        echo "" > "$BASE""$DOMAINOPT""/.stat"
            if [[ -f "$BASE""$DOMAINOPT""/.stat" ]] #added-1438387045
                then
                    echo "$BASE$DOMAINOPT/.stat file created!"
            fi
    fi
    echo "All Operations Complete, exiting now!"

如果您运行没有任何参数的脚本(请输入域名),一切都会正常响应,如果您尝试在域之前输入路径,它会正常响应...但是当我正确地执行此操作时,当我键入:“cleancache .sh abc.com“,我得到一个错误,就像我没有达到所需的模式(”请遵循DOMAIN选项的正确格式“)......当这是完全写的! ......我不明白我错过了什么,整天都在敲我的头,没有快乐。

请帮助!

1 个答案:

答案 0 :(得分:2)

使用它来匹配正则表达式:

[[ "$DOMAINOPT" =~ ^[A-Za-z0-9-]*[\.][a-z]*$ ]]

或者这个:

[[ ! "$DOMAINOPT" =~ ^[A-Za-z0-9-]*[\.][a-z]*$ ]]

不要引用正则表达式。