1

This code will run only for the first match because of the lines under functions: service httpd start but it will display echo from all functions if I remove all ssh... lines. Could someone explain it to me what is happening and maybe some solution how I can use it.

#!/bin/bash
 function s1 {
        echo "running 1 "
        ssh user@server_1_IP service httpd start
}
    function s2 {
        echo "running 2"
        ssh user@server_2_IP service httpd start
} 
function s3 {
        echo "running 3"
        ssh user@server_3_IP service httpd start
}
whiptail --title "Test" --checklist --separate-output "Choose:" 20 78 15 \
"Server1" "" on \
"Server2" "" on \
"Server3" "" on 2>results

while read choice  
do
        case $choice in
                Server1) s1
                ;;
                Server2) s2
                ;;
                Server3) s3
                ;;
                *)
                ;;
        esac
done < results
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Shimpu
  • 13
  • 5

1 Answers1

1

In your code, stdin that comes from file results is given both to while read ... and to ssh (in s1, s2 and s3). ssh will eat whatever was not read by read in the first loop.

Try saving stdin before the loop for later use with ssh:

exec {stdin}<&0
while read choice  
do
        case $choice in
                Server1) s1 <&$stdin ;;
                Server2) s2 <&$stdin ;;
                Server3) s3 <&$stdin ;;
        esac
done < results

Or use < /dev/null if you don't need stdin in ssh.

xhienne
  • 17,075
  • 2
  • 52
  • 68
  • I'm new here. I accepted your answer do I have to do something more? To give you some points or something? – Shimpu Aug 03 '17 at 10:14
  • @Shimpu Welcome, then! See https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work You can also upvote the answer when you have enough reputation – xhienne Aug 03 '17 at 10:42