在While循环和大小写之后,Shell函数字符串变量结果为空

时间:2018-02-12 15:19:27

标签: bash macos shell terminal

为什么我没有得到echo "User password is= $typo"结果 我怎么能在while循环后得到拼写错误的结果。

#!/bin/sh

pass_mes()
{
    read -sp 'Password: ' passvar
    echo $passvar           
}

password_verify()
{
    pass_ver=$(dscl /Search -authonly skull $typo 2>&1 | grep -i Authentication | awk '{print $1$5}')
        if [ "$pass_ver" == "Authenticationfailed." ]; then
            echo "Wrong_Password"
        else 
            echo "Correct_Password"
        fi
}

funtion_cancle()
{
        us_output=$(pass_mes)
            if [ "$us_output" == "User_Cancel" ]; then
                echo "User_Cancel"
            else
                echo "$us_output"
            fi
}


funtion_final_output()
{
    typo=$(funtion_cancle)
        if [ "$typo" == "User_Cancel" ]; then
            echo "User_Cancel"
        elif [ -z $typo ]; then
            echo "empty"
        else
            if [ "$(password_verify)" == "Correct_Password" ]; then
                echo "WORKING_SKULL"
            else
                #echo "Wrong_Password"
                echo "NOT_WORKING_SKULL"
            fi  
        fi      
}

retry_funtion()
{
    echo "****Wrong Password Typed****"
}

cancel_funtion()
{
    echo "***User Cancled ***"
}


############## Script start ##################

echo "Script start"

while :
do
    case    $(funtion_final_output) in
        "NOT_WORKING_SKULL") retry_funtion
        ;;
        "empty") retry_funtion
        ;;
        "User_Cancel") cancel_funtion exit
        ;;
        "WORKING_SKULL") break
        ;;
    esac
done

echo "User password is= $typo"

echo "Continue script using password $typo"

2 个答案:

答案 0 :(得分:2)

你正在像一个局部变量一样输入拼写错误。在脚本的主体中,“拼写错误变量未设置”。

换句话说,我不理解“funtion_cancle()”函数的目的。如果“us_output”变量为“User_Cancel”,则返回“User_Cancel”,否则如果“us_output”为“a_valid_pass”则返回相同的...此函数不执行任何操作

我建议你这样做:

  1. 删除de“funtion_cancle()”
  2. 在脚本开始时分配“拼写错误”。
  3. 一个例子:

    #!/bin/sh
    
    
    pass_mes()
    {
        read -sp 'Password: ' passvar
        echo $passvar           
    }
    
    password_verify()
    {
        pass_ver=$(dscl /Search -authonly skull $typo 2>&1 | grep -i Authentication | awk '{print $1$5}')
            if [ "$pass_ver" == "Authenticationfailed." ]; then
                echo "Wrong_Password"
            else
                echo "Correct_Password"
            fi
    }
    
    
    
    funtion_final_output()
    {
            if [ "$typo" == "User_Cancel" ]; then
                echo "User_Cancel"
            elif [ -z $typo ]; then
                echo "empty"
            else
                if [ "$(password_verify)" == "Correct_Password" ]; then
                    echo "WORKING_SKULL"
                else
                    #echo "Wrong_Password"
                    echo "NOT_WORKING_SKULL"
                fi
            fi
    }
    
    retry_funtion()
    {
        echo "****Wrong Password Typed****"
        typo="$(pass_mes)"
    }
    
    cancel_funtion()
    {
        echo "***User Cancled ***"
    }
    
    
    ############## Script start ##################
    
    echo "Script start"
    typo="$(pass_mes)"
    
    while :
    do
        case    $(funtion_final_output) in
            "NOT_WORKING_SKULL") retry_funtion
            ;;
            "empty") retry_funtion
            ;;
            "User_Cancel") cancel_funtion; exit
            ;;
            "WORKING_SKULL") break
            ;;
        esac
    done
    
    echo "User password is= $typo"
    
    echo "Continue script using password $typo"
    

答案 1 :(得分:1)

正如之前人们所说,脚本的一部分在子shell中运行,因此$typo会丢失。

试试这个,它有效:

#!/bin/sh

pass_mes()
{
    read -sp 'Password: ' passvar
    echo $passvar           
}

password_verify()
{
    pass_ver=$(dscl /Search -authonly skull $typo 2>&1 | grep -i Authentication | awk '{print $1$5}')
        if [ "$pass_ver" == "Authenticationfailed." ]; then
            echo "Wrong_Password"
        else 
            echo "Correct_Password"
        fi
}

funtion_cancle()
{
        us_output=$(pass_mes)
            if [ "$us_output" == "User_Cancel" ]; then
                echo "User_Cancel"
            else
                echo "$us_output"
            fi
}


funtion_final_output()
{
    typo=$(funtion_cancle)
        if [ "$typo" == "User_Cancel" ]; then
        ret="User_Cancel"
            echo "${ret}"
        elif [ -z $typo ]; then
        ret="empty"
            echo "${ret}"
        else
            if [ "$(password_verify)" == "Correct_Password" ]; then
        ret="WORKING_SKULL"
                echo "${ret} ${typo}"
            else
                #echo "Wrong_Password"
        ret="NOT_WORKING_SULL"
                echo "${ret}"
            fi  
        fi      
}

retry_funtion()
{
    echo "****Wrong Password Typed****"
}

cancel_funtion()
{
    echo "***User Cancled ***"
}


############## Script start ##################

echo "Script start"

while read -r ret typo
do
    case    $ret in
        "NOT_WORKING_SKULL") retry_funtion
        ;;
        "empty") retry_funtion
        ;;
        "User_Cancel") cancel_funtion exit
        ;;
        "WORKING_SKULL") break
        ;;
    esac
done < <(echo $(funtion_final_output))

echo "User password is= $typo"

echo "Continue script using password $typo"

在我的解决方案中,运行while循环传递funtion_final_output参数,如:

while ... do ... done < <(echo $(funtion_final_output))

为了能够传递值,您不能只在功能中echo,而是将其分配给变量(funtion_final_output的一部分:)

当时:

if [ "$(password_verify)" == "Correct_Password" ]; then
                echo "WORKING_SKULL"if [ "$(password_verify)" == "Correct_Password" ]; then
                echo "WORKING_SKULL"

现在:

if [ "$(password_verify)" == "Correct_Password" ]; then
        ret="WORKING_SKULL"
                echo "${ret} ${typo}"

我已在if中的每个funtion_final_output中进行了类似的变量分配

接下来,当您在最终loop中传递变量时,您必须阅读它们。

while :改写为while read -r ret typo

享受。