Home > database >  How to respond to a prompt within a shell script
How to respond to a prompt within a shell script

Time:05-09

I'm trying to answer a command prompt for the user within the script.

Command

ufw enable

Prompt

Command may disrupt existing ssh connections. Proceed with operation (y|n)?

Piece of code that I'm using but its not working properly:

function awk_ufw {
    ufw status | grep Status | awk '{print $2}'
}

.
.
.

    local CHECK_STATUS=$(awk_ufw)
    if [ $CHECK_STATUS == "active" ]
    then
        echo "----------------------------"
        echo "Firewall is already enabled!"
        echo "----------------------------"
    else
        read -p "Before you enable the firewall, please ensure that at least inbound port for your ssh connection is open, otherwise you'll be locked out if ssh is the only way to access the system! [Please type \"understood\" to continue]: " UNDERSTOOD_PROMPT
        if [[ $UNDERSTOOD_PROMPT == "understood" || $UNDERSTOOD_PROMPT == "UNDERSTOOD" ]]
        then
            ufw enable <EOF
            y
            EOF
            if [ $? == "0" ]
            then
                echo "-----------------"
                echo "Firewall enabled!"
                echo "-----------------"
            else
                echo "------------------------------------------------------------------------------------------------------"
                echo "Something went wrong during the process, unsure whether firewall was enabled, please recheck manually!"
                echo "------------------------------------------------------------------------------------------------------"
            fi
        else
            echo "------------------------------------------------------"
            echo "Skipping the step since \"Understood\" wasn't entered!"
            echo "------------------------------------------------------"
        fi
    fi

Code break on

            ufw enable <EOF
            y
            EOF
Before you enable the firewall, please ensure that at least inbound port for your ssh connection is open, otherwise you'll be locked out if ssh is the only way to access the system! [Please type "understood" to continue]: understood
./script.sh: line 84: EOF: No such file or directory
./script.sh: line 85: y: command not found
./script.sh: line 86: EOF: command not found
------------------------------------------------------------------------------------------------------
Something went wrong during the process, unsure whether firewall was enabled, please recheck manually!
------------------------------------------------------------------------------------------------------

Any suggestions how to correct it/improve it?

Thank you in advance!

CodePudding user response:

The end marker in heredocs cannot be indented:

cat << MARKER
  content
MARKER

works, while:

cat << MARKER
  content
  MARKER

doesn't.

And heredocs uses two lt symbols: << MARKER. < thing is a redirection.

In your case it would be:

        if [[ $UNDERSTOOD_PROMPT == "understood" || $UNDERSTOOD_PROMPT == "UNDERSTOOD" ]]
        then
            ufw enable <<EOF
            y
EOF

But you might consider piping echo y | instead:

echo y | ufw enable

Which is effectually the same in your case.

FYI awk can do pattern matching, to there is no need to you to pipe grep | awk:

awk_ufw() {
    ufw status | awk '/Status/ {print $2}'
}

Or possible just check the first field:

awk_ufw() {
    ufw status | awk '$1 == "Status" {print $2}'
}

CodePudding user response:

A quick scan of the man page shows you can avoid the problem all together:

By default, ufw will prompt when enabling the firewall while running under ssh. This can be disabled by using 'ufw --force enable'.

So using:

ufw --force enable

in your script avoids the issue of having to pass a keypress in.

  • Related