Solved

bash parse output to variable

Posted on 2014-03-04
9
703 Views
Last Modified: 2014-03-04
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
0
Comment
Question by:perlperl
  • 5
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39904767
output='Command line asked for 29 of 639 tests
Of those: 0 Skipped, 5 Failed, 0 Warned, 24 Passed'
F=${output%Failed*}
echo ${F##*,}
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39904782
Of just to check:
[[ $output =~ ", 0 Failed" ]] && echo "No test failed"
0
 

Author Comment

by:perlperl
ID: 39904786
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 39904797
see the Parameter Expansion section of
man bash
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:perlperl
ID: 39904798
The second one sounds much easier ;)
0
 

Author Comment

by:perlperl
ID: 39904816
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
0
 

Author Comment

by:perlperl
ID: 39904835
somehow this does not work

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

Author Comment

by:perlperl
ID: 39904844
This worked. i hate bash scripting

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

so particular about spaces.  crazy syntax
0
 
LVL 84

Expert Comment

by:ozo
ID: 39904850
[[ syntax is slightly different from [ syntax.  In particular, [ does not have the =~ operator
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now