Shell Function string variables result blank after

2019-07-15 03:02发布

Why i not getting echo "User password is= $typo" result How i can get result of typo in the end after while loop.

#!/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条回答
太酷不给撩
2楼-- · 2019-07-15 03:42

As people said before, parts of your script running in sub-shells, so $typo is lost.

Try this, it works:

#!/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"

In my solution, run while loop passing funtion_final_output arguments to it like:

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

To be able to pass values, you must not just echo them in functions, but assign it to variables (part of funtion_final_output:)

Was:

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

Now:

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

I've made variables assignments like that in each if in funtion_final_output)

Next, when you pass variables in the final loop, you must read them.

while : rewritten to while read -r ret typo

Enjoy.

查看更多
Fickle 薄情
3楼-- · 2019-07-15 03:52

You are assinging typo like a local variable. In the main body of your script, the "typo variable was unset".

By other way, I don't understand the pourpose of the "funtion_cancle()" function. If the "us_output" variable is "User_Cancel" you return "User_Cancel", else if "us_output" is "a_valid_pass" you return the same...This function doesn't do anything

I recommend you make this:

  1. Delete de "funtion_cancle()"
  2. Assign "typo" at the beggining of the script.

An example:

#!/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"
查看更多
登录 后发表回答