[root@server2 ~]# cat /root/yesno.sh
#!/bin/bash
read -p "are you sure?" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
echo "YES"
else
echo "NO"
fi
[root@server2 ~]# sh /root/yesno.sh
are you sure?y
YES
It works fine. I have installed ssh keys between server1 and server2. When I run the script from server1 as follows, it does not prompt the question. However, if I press Y, it gives correct output. How can I get the question printed into the shell?
[root@server 1~]# ssh root@11.22.33.44 sh /root/yesno.sh
y
YES
There's a shebang #!/bin/bash in the script, but running it with sh scriptname bypasses the shebang's effect. The reote script needs to be executable. And run that way.
# one time only
ssh root@11.22.33.44 ' chmod +x /root/yesno.sh'
ssh root@11.22.33.44 ' /root/yesno.sh'
Also try to keep the remote command surrounded by ' or by " or you will get unexpected results - in general. See the examples above.
PS: root access by ssh is usually a bad idea. If this is a home network, maybe. For business, no. Would not pass a reasonable security audit.