Solved

bash parse output to variable

Posted on 2014-03-04
9
757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 84

Expert Comment

by:ozo
ID: 39904797
see the Parameter Expansion section of
man bash
0
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

621 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