1

Within a Qt5 application I have a bash script which runs to check version numbers from certain files on other remote machines (that I ssh into).

I have over 100 machines that I can run this script on. If the machine I have sshed into has the file I am looking for the script output is nicely displayed, but if the file on the remote machine does not exist then my 2 lines join together.

e.g

Should look like this:

Operating System: 1.5.64 
sw_install: 1.16
Kate 1.1

but if remote files don't exist I get

Operating System:  sw_install
Kate: 1.1

Any ideas to get the lines to be separate if the remote files don't exist (if does happen). I don't just want to put an 'echo' line in between the 2 ssh commands (or remove the -n) as the output is not the desired look when the files do exist.

Hoping there is a really simple answer out there please.

Thank you very much for your help!!

echo -n "Operating System: "
ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "cat /home/user/Version.txt"
echo -n "sw_intall: "
ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "grep VERSION= /home/user/sw_install | cut -d'=' -f2 | tr -d '\"' |  head -1"
ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "rpm -qv kate --qf \"         Kate:  %{VERSION}.%{RELEASE}\""

Centos 7.2

tj26
  • 113
  • It should work if you remove the -n and put the ssh command into a command substitution like echo "Operating System: $(ssh -t …)". I'm just wondering why don't get an error message if the file is missing. – Freddy Aug 05 '19 at 03:59
  • Freddy, I think there is no error message because what is being returned is stdout not stderr so I get nothing... – tj26 Aug 05 '19 at 06:54
  • I have added "|| echo unknown version" to the cat line and get the correct answer. This solution doesn't work for the grep line though... – tj26 Aug 05 '19 at 06:55
  • How about supressing the error with grep -s and catching the empty string like this: VERSION=$(grep -s VERSION= /home/user/sw_install | cut -d'=' -f2 | tr -d '\"' | head -1);echo ${VERSION:="no-text returned"} – Philippos Aug 05 '19 at 07:13
  • Btw, I don't know the very format of your sw_install file, but all your piping and greping and cutting and tring and heading can probably be replaced by a simple sed '/.*VERSION=/!d;s///;s/[\"]//g;q' – Philippos Aug 05 '19 at 07:19

2 Answers2

1

You could pipe cat to tr -d '\n' and explicitly add a newline after the command.

Tanami
  • 136
  • 7
1

Don't use echo at all. Use printf and add the newline explicitly. Change this:

echo -n "Operating System: "
ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "cat /home/user/Version.txt"
echo -n "sw_intall: "
ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "grep VERSION= /home/user/sw_install | cut -d'=' -f2 | tr -d '\"' |  head -1"
ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "rpm -qv kate --qf \"         Kate:  %{VERSION}.%{RELEASE}\""

To this:

os=$(ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "cat /home/user/Version.txt")
swInstall=$(ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "grep VERSION= /home/user/sw_install | cut -d'=' -f2 | tr -d '\"' |  head -1")
kate=$(ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "rpm -qv kate --qf \"         Kate:  %{VERSION}.%{RELEASE}\"")

printf 'Operating System: %s\nsw_intall: %s\nKate: %s\n' "$os" "$swInstall" "$kate"

Alternatively, use a heredoc:

cat<<EoF
Operating System: $(ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "cat /home/user/Version.txt")
sw_install: $(ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "grep VERSION= /home/user/sw_install | cut -d'=' -f2 | tr -d '\"' |  head -1")
Kate: $(ssh -t -o LogLevel=QUIET -o '''StrictHostKeyChecking no''' $NODENAME "rpm -qv kate --qf \"         Kate:  %{VERSION}.%{RELEASE}\"")
EoF
terdon
  • 242,166