printf in bash sometimes prints new line, can't figure out why -


if have file below contents:

127.0.0.1 127.0.0.2 127.0.0.3 8.8.8.8 127.0.0.4 

and run command test ssh connection success or failure:

while read host  ssh -n -q -o batchmode=yes -o connecttimeout=5 $host "echo 2>&1" && \  printf "%-15s%-45s%s" `id -un` $host "ssh_ok" || \  printf "%-15s%-45s%s" `id -un` $host "ssh_bad" done <host_list 

i get:

myname   127.0.0.1                                    ssh_ok myname   127.0.0.2                                    ssh_ok myname   127.0.0.3                                    ssh_okmyname   8.8.8.8                                      ssh_bad myname   127.0.0.4                                    ssh_ok[myname@server ~]$ 

as can see, looks nasty because doesn't print newine ssh_bad output or last line bash prompt tagged end of last check.

i can fix adding \n prinf have double spaces on except bad line.

can tell me please printf getting newline when don't specify one, , why prints 1 ssh_ok lines not ssh_bad lines.

is there good/easy way fix this?

thank flo

the newlines coming echo 2>&1 you're running on remote system. when ssh successful, command executes, , newline printed before printf. when ssh fails, command doesn't run, no newline before printf.

i suggest use command doesn't display anything, , put \n in both printf lines:

while read host  ssh -n -q -o batchmode=yes -o connecttimeout=5 $host "true" && \  printf "%-15s%-45s%s\n" `id -un` $host "ssh_ok" || \  printf "%-15s%-45s%s\n" `id -un` $host "ssh_bad" done <host_list 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -