1

I have a script which stores output of a command inside a variable like this:

my_var=$(command)

However, some of the output is not being stored in the variable, I am assuming because it is delayed... I am sure its delayed because it outputs the line I want to the screen after it stored the output in the variable and I tried processing it...

[EDIT]IMPORTANT! It turns out the output was going to stderr[/EDIT]

How do I get it to wait for the exit code (when it is 100% done outputting) before storing the output into the variable?

I did not post an example because I thought the users would complain it is nefarious, however it is not, and I am running the script on my own servers. Example as requested:

# Grab the output, so we can check for error... If error then retry same password after wait time
outpt="$(hydra -l $hydra_user -P $tmp_passwds $hydra_all_params $hydra_host $hydra_module)"

# Grep for "ssh protocol error" line
banln=$(echo "$outpt" | grep 'ssh protocol error')

# Show ban status, update wait_time
if [ "$banln" != "" ];then
    echo "[STATUS] Oops, we got banned! (increasing wait_time + 1 second)"
wait_time=$wait_time+1

# Start retry procedure until not banned

fi

The problem in this example is that the line I am looking for ([ERROR] ssh protocol error) seems to come after the command's output is stored in the variable

migrated from serverfault.com Mar 20 '15 at 19:58

This question came from our site for system and network administrators.

  • 1
    Please provide an example of what is happening, sticking as close to the actual command being run as possible. The current format of this question presents a XY problem. – Andrew B Mar 18 '15 at 0:43
  • Uhhm... I just read the xy problem, its nothing close to it... But I added an example for clarity, as you asked for one. – kthx Mar 18 '15 at 1:06
  • The problem was that you were making an assumption about what was happening, and asking a question based on the assumption without providing us enough information to verify your point of view. Moving on from that...what happens if you append a space prefixed 2>&1 to the end of your hydra command? Does the problem go away? – Andrew B Mar 18 '15 at 1:10
  • The assumption was not really an assumption, its just how I chose to word it... I know for a fact the part I want is being output after the capturing was done. I apologize for my wording habits... I will try that and let you know so you can post it as an answer and I can accept it :) – kthx Mar 18 '15 at 1:20
  • YESSSS!!! I guess it was outputting to stderr... Thanks lots! Post as an answer and I will accept it :D – kthx Mar 18 '15 at 1:24
2

Unless this process is backgrounding itself or forking, it would not be due to the delay. You can verify this with a quick test:

# FOO=$(sleep 10; echo whee;); echo $FOO
whee

This command does not complete until the sleep finishes executing, and the message that is sent to standard out afterwards is successfully stored to the variable. From this, we can surmise a few things:

  • The variable is not stored until the command completes.
  • STDOUT remains connected for the full duration of the command.

At this point, one of two things is happening here:

  • The message isn't being written to STDOUT: it was probably being written to STDERR instead.
  • The command is in fact completing. (backgrounded, forked a subprocess, etc.)

Usually the problem is the former.

  • Yes Andrew B just figured it out, he had a hunch that it was going to stderr and it was :) I was going to accept his answer, but he hasnt posted it as an answer yet O-o – kthx Mar 18 '15 at 1:30
  • @kthx (psst, it's me) – Andrew B Mar 18 '15 at 1:30
  • Hahahhaa oops... Im a stoner, but havent been stoned in weeks lol... sorry didnt notice :P – kthx Mar 18 '15 at 1:33

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.