0

I am having a steps in Jenkins pipeline to read my test passing result and fail the Stage if rate lower than 95%. Here's the code

sh ''' #!/bin/bash -xe
                PASS= `cat index.txt | grep % | tr -d " " | cut -d: -f2 | cut -d% -f1`
                if [[ "$PASS" -ge 95 ]]
                then
                    exit 0
                else
                    exit 1
                fi
            '''

But it constantly gives me an error

16:01:00 + cat index.txt
16:01:00 + grep %
16:01:00 + cut -d% -f1
16:01:00 + tr -d  
16:01:00 + cut -d: -f2
16:01:00 + PASS= 100
16:01:00 /home/xxxx/script.sh: 100: not found

What's happened? Anyone could help? Thanks

  • 1
    get rid of the space after PASS=. at the moment, your script is setting PASS to the empty string and then trying to execute the result of the cat ..... (which in your example evaluates to 100). so it is effectively PASS="" 100 – cas Aug 19 '19 at 08:10
  • @cas Oh man you saved my life LOL, you should put is as Answer instead of comment. Oh ya, another thing is use single [ instead of double [[ – hellojoshhhy Aug 19 '19 at 08:20
  • 1
    Additionally, the whole of the if statement could be replaced by [ "$PASS" -gt 95 ]. Neither exit statements are necessary as the test returns the correct exit status by itself (by virtue of being the last thing executed). – Kusalananda Aug 19 '19 at 08:20
  • @JeffPang it's a dupe question, so i can't. And if i did answer, i'd have to comment on your use of backticks instead of $(...) and that awful pipeline of cat | grep | tr | cut | cut instead of just using awk or perl. or maybe sed. – cas Aug 19 '19 at 08:24

0 Answers0