Link to home
Start Free TrialLog in
Avatar of perlperl
perlperl

asked on

bash parse output to variable

I am a C++ programmer and little knowledge of bash ;)

I have $output string in format

Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed

I wanted to extract the integer before Failed and assign it to bash variable (in this case 5)
How do i do this in bash script. I want to make sure the value is "0" which means no test failed
Avatar of ozo
ozo
Flag of United States of America image

output='Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed'
F=${output%Failed*}
echo ${F##*,}
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of perlperl
perlperl

ASKER

Thats a crazy syntax, I would have never figured out this. Can you please provide me the link if you have one

so I am using this now

F=${output%Failed*}
        if [${F##*,} -ne 0 ]; then
                echo "Failed Num of errors = ${F##*,}"
        fi
see the Parameter Expansion section of
man bash
The second one sounds much easier ;)
Is there something called !~

I wanna do and operation

 output='Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed'

 if [ $? -ne 0 ] || [$output !~ ", 0 Failed"]; then
   echo "Failed"
fi
somehow this does not work

 if [ $? -ne 0 ] || ! [$output =~ ", 0 Failed"]; then
   echo "Failed"
fi
This worked. i hate bash scripting

 if [ $? -ne 0 ] || [[ ! "$output" =~ ", 0 Failed" ]]; then

so particular about spaces.  crazy syntax
[[ syntax is slightly different from [ syntax.  In particular, [ does not have the =~ operator