1

I need to get /mysqlsharedetails from all the MySQL servers. I have written a script using a while read loop, but it's getting the details of only the first server,

#!/bin/ksh
file="/home/mysqladm/server_list/server_list.txt"
# while loop
while IFS= read -r a
do
        # display line or do something on $line
        output=`ssh $a df -h | grep mysqlshare`
#echo $a,$output
echo $a ,$output >> /home/mysqladm/server_list/output.txt
done < "$file"

I have server_list.txt file in which there are two servers like,

server_name1,
server_name2,

it's going to server_name1 and doing df -h and getting out of script.  Why is the loop not working?

  • 1
    Welcome to Stack Exchange.  What have you done to try to debug this?  What happens when you comment out the ssh and just print the server name?  By the way, you should always quote your shell variable references (e.g., "$a" and "$output") unless you have a good reason not to, and you’re sure you know what you’re doing.  Also, just for clarity, you might want to change \…`` to $(…) — see this, this, and this.  And please indent correctly. – G-Man Says 'Reinstate Monica' Dec 16 '15 at 06:33
  • I am new to scripting, trying from my end to get it.Do loop when we use while read in it? i used ====================================for server in cat /home/mysqladm/server_list/ms_mysql_all_servers_list ;

    do

    share=ssh $server df -h /mysqlshare* $share >>/home/mysqladm/server_list/share.txt done...............this one is working fine but unable to get it using while loop.

    – harinath Dec 16 '15 at 06:58

1 Answers1

0

SSH tries to read all the remaining stdin, so it breaks the loop. You need to connect it's stdin to null with < /dev/null or do something else like that.

Here is how I would write script to achieve that:

#!/bin/bash
IFS=,
file=servers.txt
logfile=log.txt
while read server
do
        ssh $server<<<"df -h | grep mysqlshare" 2>&1>>$logfile
done < $file
StefanR
  • 1,392