3

This is my first post here and I am not an expert of Linux /Bash scripting yet. :-)

So I wrote the following code for my Raspberry Pi and it works fine except for one small issue.

I am using SSH to log into the raspberry Pi.When pin 17 is high, ( digital logic value of 1 aka 5 volts ), I want the message “I noticed a 1” printed on the terminal output and then I want to exit the script and still stay logged into the raspberry Pi.

The problem that I am facing is when the script sees a logic value of 1, it shuts down the entire ssh connection adn takes me back to my laptop's terminal session.

I dont want the script to exit the ssh connection when pin 17 reads a 1. I just want it to stop executing the scriot and take me back to the raspberry Pi terminal where I am still logged into the raspberry Pi so that I can do other stuff.

I basically DO NOT want to close the SSH session when the scipt exits. Could you please give me some suggestions to fix this issue?

Thank you for the reply!

pin=17
gpio mode $pin in
while true; do
    if [[ $(gpio read $pin) -eq 1 ]]; then
        echo "I noticed a 1 whcih means I noticed 5 volts!"
        return
    elif [[ $(gpio read $pin) -eq 0 ]]; then
        echo "Did not see a 1, so I will keep looping"
        echo ""
        sleep 2
    fi
done
3
  • 1
    How exactly did you run this script? Sep 17, 2020 at 14:37
  • I just typed in ./logicreadscript.sh in the Raspberry Pi's terminal. Then waited a few seconds and gave 0 volts to pin 12. It printed "Did not see a 1, so I will keep looping" Later, I gave pin 12 , 5 volts, and it printed ""I noticed a 1 whcih means I noticed 5 volts!" and then abruptly exited the SSH session. Sep 17, 2020 at 14:41
  • 1
    This doesn't appear to be on topic here. You may be able to get help at our sister sites Raspberry Pi or Unix & Linux. Sep 17, 2020 at 14:44

2 Answers 2

1

Welcome to Server Fault. Try using the following code:

pin=17
gpio mode $pin in
while true; do
    if [[ $(gpio read $pin) -eq 1 ]]; then
        echo "I noticed a 1 whcih means I noticed 5 volts!"
        kill -INT $$
    elif [[ $(gpio read $pin) -eq 0 ]]; then
        echo "Did not see a 1, so I will keep looping"
        echo ""
        sleep 2
    fi
done
5
  • Thank you for your feedback. I was busy editing mypost when you sent me the message. Any ways, having 'return' did not help. The script gives the following error and keeps running. ./logicreadscript.sh: line 6: return: can only `return' from a function or sourced script Sep 17, 2020 at 14:33
  • Well my original script doesnt throw an error. It just displays "I notices a 1 whcih means I notices 5 volts!" and then says "connection closed" to [email protected] Sep 17, 2020 at 14:39
  • I updated, please try it (I haven't the time to test it sadly).
    – LeRouteur
    Sep 17, 2020 at 14:40
  • Your last code worked. thank you very much for your time! Have a nice day! I am sorry about the accepting edit issue before. This is my first time here and the website seems to work a little different from a regular forum. Thank you again! Sep 17, 2020 at 14:46
  • No problem, we're here to learn. If you want to improve your questions, please have a look here. As you said, my answer helped you. Please consider marking as answer (the green check on my post) :) have a nice day too
    – LeRouteur
    Sep 17, 2020 at 15:08
0

I'm assuming you do something like

ssh raspberrypi /path/to/script

So, yes, when the script exits, the ssh session ends too.

However, in the script, instead of exiting, spawn an interactive shell:

    if [[ $(gpio read $pin) -eq 1 ]]; then
        echo "I noticed a 1 whcih means I noticed 5 volts!"
        exec bash -li

That replaces the running script process with an interactive login shell.

You might need ssh -t or ssh -tt, test without first.

2
  • Thanks for the reply. Are you saying that I might need to do ssh -t raspberrypi /path/to/script or ssh -tt raspberrypi /path/to/script ? Sep 17, 2020 at 16:34
  • You might need one or the other. But maybe not. Sep 17, 2020 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .