1

So for some reason, it always returns either ssh or 'ok' regardless of how I modify the test statement and have no idea what to do. I left the code below.

echo 'Wanna connect to the raspberry pi? (y/n)'
read $sssh

if [[ $sssh == "y" ]]
then 
        ssh pi@192.168.0.56
else 
        echo 'ok'
fi
  • and you expect it to return something else? – jsotola Aug 15 at 5:15
  • is this all on a single line? – fpmurphy Aug 15 at 5:31
0

The read utility expects the name of a variable as its argument. With read $sssh, you give it the value of the sssh variable rather than the name of a variable.

I'm assuming that you'd want to read into the sssh variable, in which case you should have used read sssh.

Your modified script (incorporating a read loop that iterates until valid input is given by the user):

#!/bin/bash

while true; do
    read -p 'Connect to RPi? (y/n): ' yesno
    case $yesno in
        y) ssh ...; break ;;
        n) echo ok; break ;;
        *) echo invalid input >&2
    esac
fi

or, longer,

#!/bin/bash

while true; do
    read -p 'Connect to RPi? (y/n): ' yesno
    if [[ $yesno == 'y' ]]; then
        ssh ...
        break
    elif [[ $yesno == 'n' ]]; then
        echo ok
        break
    fi
    echo invalid input >&2
fi
| improve this answer | |
  • Also beware that the behaviour of read is dependant on the current value of the $IFS variable, so it's good practice to always set IFS for its invocation. You'll probably want to pass -r as well unless you have a good reason for it to treat backslashes specially. So IFS= read -rp .... See also zsh and its read -q for yes/no questions. – Stéphane Chazelas Aug 15 at 7:00

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.