LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2019, 04:13 AM   #1
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Rep: Reputation: Disabled
Groovy to Linux variable


Hello all,
I'm struggling to convert a groovy variable to use in a bash command. Any help would be great. Can anyone see what I am doing wrong?

def teamname = "${loweritam}"
sh '''
appteam="""$teamname-REL"""
team=ap123456
echo $appteam $team
'''
Output:
+ appteam=-REL
+ team=ap123456
+ echo -REL ap123456
-REL ap123456

appteam should look like - 12345tr-REL
I've tried using single quotes like so: appteam='''+teamname+'-REL''''

but that does not work for me either.

thanks,
Fluke
 
Old 09-10-2019, 04:22 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Spaces around the equal?
Code:
teamname = "${loweritam}"
 
Old 09-10-2019, 04:54 AM   #3
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Spaces around the equal?
Code:
teamname = "${loweritam}"
Do you mean remove the spaces around the =?
def teamname="${loweritam}"

just tried it there but no difference. I think the issue must be in the quotes in the shell part. but I've tried combinations and I can't get it to work.

This worked for me before:
def teamname = "${loweritam}"
sh '''
./cli create-folder '''+teamname+'-REL''''
'''

So the issue must be with setting it as a variable (appteam="""$teamname-REL""")
 
Old 09-10-2019, 05:06 AM   #4
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by flukeyLinux View Post
Do you mean remove the spaces around the =?
def teamname="${loweritam}"

just tried it there but no difference. I think the issue must be in the quotes in the shell part. but I've tried combinations and I can't get it to work.

This worked for me before:
def teamname = "${loweritam}"
sh '''
./cli create-folder '''+teamname+'-REL''''
'''

So the issue must be with setting it as a variable (appteam="""$teamname-REL""")
Having just said the last line, I realized I don't really need to set vars in the shell. So I have removed them and tried the following:

sed -i "s/placeteamhere/'''+teamname+'-REL''''/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'''+teamname+''''/ create-job $appteam

Which resulted in 'script.sh: line 5: syntax error: unterminated quoted string'

Could just be a missing extra single quote. Does anyone know the meaning of the quotes and why there are 3 at the start but 4 or 5 at the end?

Last edited by flukeyLinux; 09-10-2019 at 05:15 AM.
 
Old 09-10-2019, 05:21 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,844

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
Kindly use [code] and [/code] tags when quoting code.

Do you wish to execute shell command within your program, or you wish to set a shell-variable, after your program terminated?

The latter is not possible, but there is a well known substitution: the program writes a shell command onto the standard output.

Example:
Code:
$ resize -s
COLUMNS=136;
LINES=35;
export COLUMNS LINES;
$ eval $(resize -s)
$ echo $COLUMNS $LINES
136 35 # the new values

Last edited by NevemTeve; 09-10-2019 at 06:19 AM.
 
1 members found this post helpful.
Old 09-10-2019, 05:49 AM   #6
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Kindley use [code] and [/code] tags when quoting code.

Do you wish to execute shell command within your program, or you wish to set a shell-variable, after your program terminated?

The latter is not possible, but there is a well known substitution: the program writes a shell command onto the standard output.

Example:
Code:
$ resize -s
COLUMNS=136;
LINES=35;
export COLUMNS LINES;
$ eval $(resize -s)
$ echo $COLUMNS $LINES
136 35 # the new values
ok thanks. I'm not sure this is related. But I think the answer to this question will help me:

Code:
echo '''+teamname+'-REL''''
                      echo '''+teamname+'''''
The first echo works, the second does not.
 
Old 09-10-2019, 06:16 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,995

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by flukeyLinux View Post
Having just said the last line, I realized I don't really need to set vars in the shell. So I have removed them and tried the following:
Code:
sed -i "s/placeteamhere/'''+teamname+'-REL''''/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'''+teamname+''''/ create-job $appteam
Which resulted in 'script.sh: line 5: syntax error: unterminated quoted string'

Could just be a missing extra single quote. Does anyone know the meaning of the quotes and why there are 3 at the start but 4 or 5 at the end?
I don't know groovy

Code:
'
sh '''sed -i "s/placeteamhere/'''+teamname+'''-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'''+teamname+'''/ create-job $appteam
'''
'
''' groovy is protecting the middle '
'some protected things'this bit groovy expands ' again this is hardcoded'

anything inside ' ' is protected from the interpreter, outside the interpreter interperates

so above Magenta and the result of bold is passed the OS


so you would be passing
Code:
sh 'sed -i "s/placeteamhere/'+teamname+'-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'+teamname+'/ create-job $appteam'
I don't think that is correct

Code:
'
sh '''sed -i "s/placeteamhere/'+teamname+'-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'+teamname+'/ create-job $appteam'''
'
should pass
Code:
sh 'sed -i "s/placeteamhere/valueofteamname-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-valueofteamname/ create-job $appteam'
I've only tested that in my head, so no idea if it will actually work
I used to do things like that with python and awk, a long time ago.


I have turned a blind eye to the UUOC
 
1 members found this post helpful.
Old 09-10-2019, 06:28 AM   #8
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
I don't know groovy

Code:
'
sh '''sed -i "s/placeteamhere/'''+teamname+'''-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'''+teamname+'''/ create-job $appteam
'''
'
''' groovy is protecting the middle '
'some protected things'this bit groovy expands ' again this is hardcoded'

anything inside ' ' is protected from the interpreter, outside the interpreter interperates

so above Magenta and the result of bold is passed the OS


so you would be passing
Code:
sh 'sed -i "s/placeteamhere/'+teamname+'-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'+teamname+'/ create-job $appteam'
I don't think that is correct

Code:
'
sh '''sed -i "s/placeteamhere/'+teamname+'-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-'+teamname+'/ create-job $appteam'''
'
should pass
Code:
sh 'sed -i "s/placeteamhere/valueofteamname-REL/g" templates/config.xml
/bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-valueofteamname/ create-job $appteam'
I've only tested that in my head, so no idea if it will actually work
I used to do things like that with python and awk, a long time ago.


I have turned a blind eye to the UUOC

This is helpful and I'll test it out. I've been going around in circles for a while. In a bid to get away from using so many single quotes, I tried the following:
Code:
                    def teamname="${loweritam}"
                    sh '''
                      echo '''+teamname+'-REL'''' > rel
                      loweritamREL=$(cat rel)
                      loweritam=${loweritamREL::-4}
                      sed -i "s/placeteamhere/$loweritamREL/g" templates/config.xml
                      /bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-$loweritam/ create-job $loweritamREL
                      '''
But for some reason the simple redirect wont work.
Code:
                      java.lang.NullPointerException: Cannot get property ' > rel
                      loweritamREL=$(cat rel)
                      loweritam=${loweritamREL::-4}
                      sed -i "s/placeteamhere/$loweritamREL/g" templates/config.xml
                      /bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-$loweritam/ create-job $loweritamREL
                      ' on null object
 
Old 09-10-2019, 06:34 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,995

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Code:
echo '''+teamname+'-REL'''' > rel
would pass
Code:
#echo '+teamname+'-REL'' > rel
# correction ?
echo '+teamname+-REL' > rel
Edit actually, it wouldn't
I don't know what groovy would do with -REL
it could be it breaks the whole thing so nothing is passed
ahh, yeah..
Quote:
java.lang.NullPointerException: Cannot get property ' > rel
Code:
echo ''''+teamname+'-REL''' > rel
would pass
Code:
echo 'valueofteamname-REL' > rel

Last edited by Firerat; 09-10-2019 at 06:48 AM.
 
Old 09-10-2019, 06:41 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,079

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
running external commands in a different lang (Groovy)
It looks to me you're wanting to run sed command in Groovy , again I do not know Groovy either.
https://community.smartbear.com/t5/S...vy/td-p/116735

https://stackoverflow.com/questions/...in-java-groovy

https://www.rosettacode.org/wiki/Exe...system_command
 
Old 09-10-2019, 06:49 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,844

Rep: Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320Reputation: 1320
@OP: Could you please quote a complete minimal example that shows the problem? (If it is not solved yet.)
 
Old 09-10-2019, 07:52 AM   #12
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
@OP: Could you please quote a complete minimal example that shows the problem? (If it is not solved yet.)
Hey. Sure, here is the relevant code:
Code:
                    def teamname="${loweritam}"
                    sh '''
                      echo '''+teamname+'-REL'''' > rel
                      loweritamREL=$(cat rel)
                      loweritam=${loweritamREL::-4}
                      sed -i "s/placeteamhere/$loweritamREL/g" templates/config.xml
                      /bin/cat templates/config.xml | ./cli -s https://jenkinscore.com/teams-$loweritam/ create-job $loweritamREL
                      '''
{loweritam} is a parameter that is added by a user. something like jenkins-123456. What I want to do is when a user creates a jenkins team, they get two folders by default.

If I do
Code:
 echo '''+teamname+'-REL''''
I get exactly what I want. jenkins-123456-REL. But for some reason '''+teamname+''''' doesn't work and redirecting
Code:
echo '''+teamname+'-REL''''
to a file doesn't work.

Last edited by flukeyLinux; 09-10-2019 at 07:55 AM.
 
Old 09-10-2019, 08:06 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,995

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
see post 9
 
Old 09-10-2019, 08:07 AM   #14
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat.
Posts: 31

Original Poster
Rep: Reputation: Disabled
Code:
echo ''''+teamname+'-REL''' > rel
would pass
Code:
echo 'valueofteamname-REL' > rel
[/QUOTE]

Tried this but no joy either. The echo comes back blank.
 
Old 09-10-2019, 08:17 AM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,995

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by flukeyLinux View Post
Code:
echo ''''+teamname+'-REL''' > rel
would pass
Code:
echo 'valueofteamname-REL' > rel
Tried this but no joy either. The echo comes back blank.[/QUOTE]

post the whole section of code
even if ( I assume java now ) didn't concatenate +teamname+ into the sting the echo should have -REL
 
  


Reply

Tags
jenkins, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Escaping a variable in Groovy to show the ascii character in println? edomingox Programming 1 04-25-2009 07:24 PM
running Bash commands in Groovy or Beanshell scripts edomingox Programming 6 04-24-2009 07:07 AM
LXer: Google Mashups get their Groovy on LXer Syndicated Linux News 0 03-29-2008 04:30 AM
LXer: Reduce Code Noise with Groovy LXer Syndicated Linux News 0 09-21-2006 06:54 AM
Terratec 6Fire and Groovy DeadRat _Mhz Linux - Hardware 0 10-18-2003 05:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration