Shell command with (

12 views
Skip to first unread message

Jérôme Godbout

unread,
Jun 30, 2020, 12:28:10 PM6/30/20
to jenkins...@googlegroups.com

Hi,

I’m trying to run the following command:

 

String name = ‘”download”’;

sh(script: "rm -fr -v !(${name})");

 

this results into the following string:

rm -fr -v !(“download”)

 

This run just fine into bash shell, I also tried with

/bin/bash rm -fr -v !(“download”)

But I always  end up with:

script.sh: Syntax error: "(" unexpected

 

What the hell is sh is doing? I can understand the bash is required but why is he trying to parse the command before running it?

 

Thanks,

Jerome

 

Slide

unread,
Jun 30, 2020, 1:15:16 PM6/30/20
to Jenkins User Mailing List
The shell is parsing the script and found an error in the syntax, it's not sh that is checking syntax. You can add a shebang line if you have something specific to bash

sh """#!/bin/bash
rm -fr -v !(${name})"""

The sh step just creates a shell script file (.sh file) and executes it, which would use the default shell. If that is not bash, then adding the shebang will cause the script to be executed under bash.

Regards,

Alex  

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/YQBPR0101MB107495F3847A77E4B8A95B27CD6F0%40YQBPR0101MB1074.CANPRD01.PROD.OUTLOOK.COM.


--

Daniel Beck

unread,
Jul 1, 2020, 6:25:28 AM7/1/20
to jenkins...@googlegroups.com


> On 30. Jun 2020, at 18:27, Jérôme Godbout <godb...@amotus.ca> wrote:
>
> What the hell is sh is doing?

It's not automatically applying your .bash_profile or .bashrc to a standalone script, as such an environment dependency would severely limit the reusability of shell scripts.

You need to set the extglob shell option for this shell script if you want to use this feature.

Compare:

1. bash -c 'ls !(foo)'
2. bash -O extglob -c 'ls !(foo)'

See `man bash`, specifically EXPANSION -> Pathname Expansion -> Pattern Matching.

Reply all
Reply to author
Forward
0 new messages