Save Alternate Output to Variable in Bash instead of Main Output?

73 Views Asked by At

Linux novice here so bear with me here.

I am writing a Bash Script for school (On a CentOS 8 VM) and I am attempting to save the output of Siege (Load Tester) to a variable so I can compare values.

Here is the issue I am running into: The HTTP lines between "The server is now under siege" and "Lifting the server siege..." are what are being stored in the variable, and not the nice little summary after "Lifting the server siege..."

[root@prodserver siege-4.1.1]# siege -c 1 -t 1s 192.168.1.3
** SIEGE 4.1.1
** Preparing 1 concurrent users for battle.
The server is now under siege...
HTTP/1.1 200     0.00 secs:    6707 bytes ==> GET  /
HTTP/1.1 200     0.01 secs:    2008 bytes ==> GET  /assets/images/taste_bug.gif
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
...Shortened for readability...
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
HTTP/1.1 200     0.01 secs:    2008 bytes ==> GET  /assets/images/taste_bug.gif
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif

Lifting the server siege...
Transactions:                149 hits
Availability:             100.00 %
Elapsed time:               0.22 secs
Data transferred:           3.95 MB
Response time:              0.00 secs
Transaction rate:         677.27 trans/sec
Throughput:            17.97 MB/sec
Concurrency:                1.00
Successful transactions:         149
Failed transactions:               0
Longest transaction:            0.01
Shortest transaction:           0.00

Currently this is how I am attempting to store the variable in bash:

SIEGE="$(siege -c $1 -t $2 [ip])"

As mentioned before, when I echo $SIEGE, it turns out the variable stored all the HTTP lines and NOT the Summary after "Lifting the siege..."

My question is how can I store that Summary in a variable.

1

There are 1 best solutions below

2
On

NOTE: I'm not familiar with siege so I have no idea if all of the output is going to stdout or if some of the output could be going to stderr.

Assuming all siege output is going to stdout ... a couple ideas depending on which lines need to be ignored:

# grab lines from `^Lifting` to end of output:

SIEGE="$(siege -c $1 -t $2 [ip] | sed -n '/^Lifting/,$ p')"

# ignore all lines starting with `^HTTP`

SIEGE="$(siege -c $1 -t $2 [ip] | grep -v '^HTTP')"

If it turns out some of the output is being sent to stderr, change the siege call to redirect stderr to stdout:

# from

siege -c $1 -t $2 [ip]

# to

siege -c $1 -t $2 [ip] 2>&1

Though I'd probably opt for saving all output to a file and then parsing the file as needed, ymmv ...