Problem assigning command output to variable
by banderas20 from LinuxQuestions.org on (#6QWH0)
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:
Code:OUTPUT=$(timeout 60s ssh user@server)Possible expected values of "OUTPUT":
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: Code: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!
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:
Code: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.
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: Code: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!