陷入逻辑陈述

Getting stuck in a logic statement

我正在编写一个 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="" #added 1438451428
    PATHOPT="" #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" 时,我收到一条错误消息,好像我没有满足所需的模式 ("Please follow the proper format for the DOMAIN option") ... 当这正是写的时候! ......我不明白我错过了什么,整天都在敲我的头,没有快乐。

请帮忙!

使用这个来匹配正则表达式:

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

或者这个:

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

不要引用正则表达式。