1

I am simply trying to gather information about the system, specifically the hostname, OS, and SSH version. I created the following script and it sets the hostname and OS but instead of setting the SSH command as variable, it executes it and I can't figure out why.

Here is the script

#! /bin/bash

set -o xtrace
rm -f ssh.log 
HOSTNAME=$(hostname)
OS=$(uname -s)
SSH_VER=$(ssh -V)

echo $HOSTNAME $OS $SSH_VER > ssh.log

cat ssh.log

Here is the output of script...

+ rm -f ssh.log
++ hostname
+ HOSTNAME=localhost.localdomain
++ uname -s
+ OS=Linux
++ ssh -V OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
+ SSH_VER=
+ echo localhost.localdomain Linux
+ cat ssh.log localhost.localdomain Linux

2 Answers 2

2

ssh -V does print its output to stderr not to stdout. To get the desired output you need to change

SSH_VER=$(ssh -V)

to

SSH_VER=$(ssh -V 2>&1)

this will redirect to output of stderr to stdout.

Visit http://www.tldp.org/LDP/abs/html/io-redirection.html for more information on bash redirections.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it!! The nuance of shell scripting is maddening to say the least
1

ssh -v outputs on stderr (>&2), not stdout (>&1). try:

 SSH_VER="$(ssh -V 2>&1)"
 echo "$HOSTNAME $OS $SSH_VER" > ssh.log #in case it's multiline

1 Comment

use ">>" if you just want to append to ssh.log, and not truncate+overwrite it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.