Solved

bash parse output to variable

Posted on 2014-03-04
9
712 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Fine Tune your automatic Updates for Ubuntu / Debian
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 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

19 Experts available now in Live!

Get 1:1 Help Now