1

I have a bash script which will:

  1. Call a python script called get-server-status.py
  2. Pipe these results into an awk command which will get a commit-sha from this output
  3. Put the final result into a variable called "SERVERS"

The python script which is called by my bash script returns the following:

2017-09-13 14:09:15 INFO: About to query Instance:i-01010abcde54345a4 
i-01010abcde54345a4 - 10.0.1.216: commit-sha = 0123456789

I wish to get only the characters after 'commit-sha' so I pipe this into awk with the following code:

GET_SERVER_STATUS="./get-server-status.py --field=commit-sha -a"
$GET_SERVER_STATUS $SERVER_ASG_NAME | awk -F "commit-sha = " '{ print $2 }

This returns what I want:

0123456789

Similarly, if my python script finds two servers and queries two instances...:

2017-09-13 14:09:15 INFO: About to query Instance:i-01010abcde54345a4 
i-01010abcde54345a4 - 10.0.1.216: commit-sha = 0123456789
2017-09-13 14:09:15 INFO: About to query Instance:i-03949eifje34543g3 
i-03949eifje34543g3 - 10.0.2.246: commit-sha = 0123456789

I get two commit shas. Just as I wanted:

0123456789   
0123456789

But here is where it goes wrong. If I try to put these results in a variable, i.e:

SERVERS=(`$GET_SERVER_STATUS $SERVER_ASG_NAME | awk -F "commit-sha = " '{ print $2 }'`)

It will only return one value instead of two.

0123456789

What is happening here? Is the first return of awk being overwritten when placed in a variable?

0

Use Bash's modern process substitution syntax $( ... ) or the old process substitution with only backticks ` ... `:

SERVERS=$( $COMMAND $ARGUMENTS | awk -F "commit-sha = " '{ print $2 }' )

What you wrote with (` ... `) runs the code inside the backticks and then creates a list/array (that's what the parentheses do) from its words. If you then try to read the array variable like a normal one, you will only get the first array element back. To get the others, you would have to specify an index.

$ text=(few words in here)
$ echo $text
few
$ echo ${text[1]}
words

I would also say that in this use case, grep with a look-behind probably is a more straight-forward solution, although your attempt with awk seems to be correct as well. It's mostly a matter of taste I guess, but here is what I would write instead:

SERVERS=$( $COMMAND $ARGUMENTS | grep -Po '(?<=commit-sha = )\w+' )

Note: I replaced your original variable names with shorter ones so that the commands nicely fit in one line.

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.