user1967720
user1967720

Reputation: 185

Executing command instead of setting as variable in bash

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

Upvotes: 1

Views: 532

Answers (2)

Olivier Dulac
Olivier Dulac

Reputation: 3791

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

Upvotes: 1

dwalter
dwalter

Reputation: 7468

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.

Upvotes: 2

Related Questions