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

6
  • 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 5 '19 at 3:59
  • Freddy, I think there is no error message because what is being returned is stdout not stderr so I get nothing...
    – tj26
    Aug 5 '19 at 6: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 5 '19 at 6: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 5 '19 at 7:13
1

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

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
1
  • Thank you Terdon, that first option worked nicely.
    – tj26
    Aug 6 '19 at 6:13

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.