3

I have this small code snippet in a shellscript to set a password:

if [[ -z "${PASS+x}" ]]; then
    read -p "enter pass or use default [test1234]" PASS
    if [[ -z "$PASS" ]]; then
        PASS="test1234"
    fi
fi


if [ $EUID != 0 ]; then
    sudo "$0" "$@"
    exit $?
fi

When I run the script the read command is executed twice. This isn't supposed to happen. Can anybody help me with finding the error.

complete script: github (corrected already)

  • read command shouldn't be executed twice, because you call it one time. And there's no loop it your code. – webKnjaZ Feb 19 '15 at 16:20
  • that's what I thought :/ . I tried changing it to echo "enter ...." read -s PASS, but its still the same. I have no idea where the error is. – Benutzer193 Feb 19 '15 at 16:23
  • @webKnjaZ I added a link to the complete script. But I think there shouldn't be anything relevant to the error? – Benutzer193 Feb 19 '15 at 16:28
  • Can you run the script with -x option and paste the output. If read is being invoked twice, it will show up. – mkc Feb 19 '15 at 16:55
  • @Ketan thanks for your help, I just found the solution godlygeek posted (didn't know about the -x switch - good to know for debugging!). – Benutzer193 Feb 19 '15 at 17:06
2

In your full script that you link to, you have this:

if [ $EUID != 0 ]; then 
    sudo "$0" "$@" 
    exit $? 
fi 

That's done after all of the options handling and the read call that you show in your question. My guess is that you're hitting this code block, reinvoking the script using sudo, and hitting the read command twice - once before restarting the script using sudo, and once after.

  • Thanks, I just found it at the moment. (while using the -x argument) – Benutzer193 Feb 19 '15 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.