Solved Problem assigning command output to variable

Solved issue

banderas20

Active Member
Joined
Aug 1, 2018
Messages
131
Reaction score
50
Credits
1,035
Hi!

I'm writing a shellscript to check whether a server is reachable via SSH.

I assign the SSH command output to a shell variable so I can tell if it works or not.
I also limit the time with the timeout command. So my script looks like this:

OUTPUT=$(timeout 60s ssh user@server)

Possible expected values of "OUTPUT":

  • empty string -> time runs out and "timeout" cancels the SSH command.
  • "user@server's password:" -> the server answers and prompts for password.
  • "ssh: connect to server port 22: Connection timed out" -> the system exceeds the timeout and I receive this specific error.

I have a problem with the value of the variable.

From the prompt, ssh user@server succeeds in less than one minute, giving the expected output.
timeout 60s ssh user@server also gives the expected result.

But If I run the script, OUTPUT variable is always empty. As if the server was always unreachable.
The same happens If I run from the prompt: OUTPUT=$(timeout 60s ssh user@server). echo $OUTPUT shows nothing.

Maybe it has something to do with the assignation to the variable?

Thanks for the help!
 


To summarize a little bit:
Code:
localuser@debian:~/Documents$ ssh user@server
ssh: connect to host server port 22: Connection timed out

localuser@debian:~/Documents$ OUTPUT=$(ssh user@server)
ssh: connect to host server port 22: Connection timed out
localuser@debian:~/Documents$ echo $OUTPUT

"OUTPUT" holds nothing...
 
"user@server's password:" -> the server answers and prompts for password.
In this case the ssh process is not finished, so as I think you can't save this output to a variable yet. You may have to use Expect or something.
Bash:
man expect
 
with output=$(ssh user@server 2>&1) I was able to capture the error.

Also, if the timeout command runs, the SSH command is cancelled with no string at all.
 
with output=$(ssh user@server 2>&1) I was able to capture the error.

Also, if the timeout command runs, the SSH command is cancelled with no string at all.
This is what came to mind when I saw the post. An error is normally printed to stderr instead of stdout and I didn't know if $(cmd) would print both of them together. I'm glad things worked for you. Thank you for sharing your solution so the rest of us can learn from your troubles.

Signed,

Matthew Campbell
 
Hi!

I'm writing a shellscript to check whether a server is reachable via SSH.

I assign the SSH command output to a shell variable so I can tell if it works or not.
I also limit the time with the timeout command. So my script looks like this:

OUTPUT=$(timeout 60s ssh user@server)

Possible expected values of "OUTPUT":

  • empty string -> time runs out and "timeout" cancels the SSH command.
  • "user@server's password:" -> the server answers and prompts for password.
  • "ssh: connect to server port 22: Connection timed out" -> the system exceeds the timeout and I receive this specific error.

I have a problem with the value of the variable.

From the prompt, ssh user@server succeeds in less than one minute, giving the expected output.
timeout 60s ssh user@server also gives the expected result.

But If I run the script, OUTPUT variable is always empty. As if the server was always unreachable.
The same happens If I run from the prompt: OUTPUT=$(timeout 60s ssh user@server). echo $OUTPUT shows nothing.

Maybe it has something to do with the assignation to the variable?

Thanks for the help!

Just run the FULL command (within the round brackets) first to see the output behaviour, see what is the normal output, see what is the error output, and make sure the output is as wanted ...

... then assign it to a variable.

Also take in mind variables were not meant to hold multiple lines of text. if you want to still have such info, you should not be using a variable for that.




To debug any command or script:
first make sure your command is either non-interactive, or you KNOW what he will be asking, and enter it when it being asked (but without any question on your screen because all output is redirected)

anycommand anyparameters > /tmp/normal_output.log 2> /tmp/error_output.log

Typically, these kind of things get also done a lot:

command123 >> /some_path/command123.log 2>> /some_path/command123.err
 
Last edited:
This is what came to mind when I saw the post. An error is normally printed to stderr instead of stdout and I didn't know if $(cmd) would print both of them together. I'm glad things worked for you. Thank you for sharing your solution so the rest of us can learn from your troubles.

Signed,

Matthew Campbell

If Linux made such much effort in separating normal and error output, why would they have this kind of system where you get both of them together as a default action ... and having no way of splitting them again ?
If you want them both, tell it to the system, otherwise all is split up.
Note that some commands use error code incorrectly, but as a user you can't do anything about that. When you see that, doubt the quality of the command in its entire form. Same with exit codes. Exit codes are a quality measurement. If missing, the creator didn't know or didn't care, either is bad.
 
If Linux made such much effort in separating normal and error output, why would they have this kind of system where you get both of them together as a default action ... and having no way of splitting them again ?
If you want them both, tell it to the system, otherwise all is split up.
Note that some commands use error code incorrectly, but as a user you can't do anything about that. When you see that, doubt the quality of the command in its entire form. Same with exit codes. Exit codes are a quality measurement. If missing, the creator didn't know or didn't care, either is bad.
You make a good point, but it was Unix that started that.

Signed,

Matthew Campbell
 


Latest posts

Top