0

I'm trying to run multiple commands in the script block but it reports not found. If I add && double ampersand then it doesn't show up in the jenkins console at all and doesn't execute (as if its missing).

                    script {
                        unit_test_result = sh (
                            script: 'mvn clean test | grep \'Tests run:\' | grep -v \'Time elapsed\' & \'if [[ $? != 0 ]]; then echo \"No Unit tests to run!\"; fi\'',
                            returnStdout: true,
                        )                           
                    }
                    

This is the error I'm getting..

[Pipeline] script)
[Pipeline]e)
[Pipeline] sh
+ + grep -v Time elapsedif [[ $? == 1]]; then echo "No Unit tests to run!"; fi
/home/test/jenkins/workspace/demo-web@tmp/durable-a86a2444/script.sh: 1: if [[ $? == 1]]; then echo "No Unit tests to run!"; fi: not found
+ grep Tests run:
+ mvn clean test
[Pipeline] }
[Pipeline] // script
[Pipeline] sh (
user630702
  • 2,529
  • 5
  • 35
  • 98
  • Remove remove the `\'` around `if ... fi`. When you fixed that syntax error, there are logical errors too. For instance, you cannot retrieve the exit status `$?` for a command running as a background job (`&`). Your script looks identical to https://stackoverflow.com/q/69333285/6770384, maybe you can use the answers from that question. – Socowi Sep 26 '21 at 11:11
  • I posted that question. I tried removing `\'` around the if and still it did not work – user630702 Sep 26 '21 at 11:17
  • Strange, why did you create this question and the linked question from different accounts? Moreover, why did you open a new question without commenting/accepting the answers to your last question first? ¶ "does not work" is not helpful. Did you get any error message? I already pointed out that removing both `'` around the if won't fix your logical errors. – Socowi Sep 26 '21 at 12:27
  • Unix & is place process in background. && is AND condition. Unix || is OR condition, while | is pipe cmd. – Ian W Sep 26 '21 at 23:42
  • @John, many of your questions are essentially duplciates of each other, asking the same question in different ways or follow from each other. Consider either simply editing the original question with additional detail or steps if your Q has not been answered rather than posting same again. If questions do follow from each other and the first is answered, at least make a reference to the earlier Q and how you got to the new Q, otherwise it seems like you are blindly asking for help. – Ian W Sep 27 '21 at 04:10

0 Answers0