It helps readability to format your commands up on several lines, preferably with indented sub-commands and adequate whitespace.
The first error I spotted is:
Don't Read Lines With For
Now perhaps it isn't really a big worry in this particular case, since you probably get a list of individual words, but it never hurts to do it properly.
You can also eliminate the use of
cut with a properly-formulated
read command.
The second thing I noticed was the
Useless Use Of Grep.
awk can do all the pattern matching itself.
Third, always try to separate the code from the data. In this case you want to send a command through ssh to another system, and that "code" is thus actually data. So store it in a variable first. That will make it easier to format, at the very least. (Note though that this is a special case. Usually you do not want to
store commands in a variable.)
And one secret for handling reserved characters in such a situation is to use the $'..' "ansi-c" quoting pattern. You can backslash both single and double quotes inside them, making it easier to format the command as a whole.
Finally, it's a good idea to use meaningful variable names, but ones that don't match existing commands or keywords. And lose the '
{}' brackets when you don't need them. They do nothing but clutter up the code.
But do quote them, always!
Code:
infile=hsts-dev
chk_command=$'df -h -P /myfile | awk \'/mapper/ { printf \"%s %s\\n\", $1, $4 }\''
while IFS=',' read -r hstname _; do
echo -n "Checking on $hstname: "
ssh "$hstname" "$chk_command" 2>/dev/null
done < "$infile"