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,

# while loop
while IFS= read -r a
        # 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,


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. Dec 16 '15 at 6: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 6:58

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:

while read server
        ssh $server<<<"df -h | grep mysqlshare" 2>&1>>$logfile
done < $file

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.