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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
09-10-2019, 04:13 AM
#1
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Rep:
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
09-10-2019, 04:22 AM
#2
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725
Spaces around the equal?
Code:
teamname = "${loweritam}"
09-10-2019, 04:54 AM
#3
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Original Poster
Rep:
Quote:
Originally Posted by
scasey
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""")
09-10-2019, 05:06 AM
#4
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Original Poster
Rep:
Quote:
Originally Posted by
flukeyLinux
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 .
09-10-2019, 05:21 AM
#5
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
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.
09-10-2019, 05:49 AM
#6
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Original Poster
Rep:
Quote:
Originally Posted by
NevemTeve
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.
09-10-2019, 06:16 AM
#7
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
Quote:
Originally Posted by
flukeyLinux
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:
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.
09-10-2019, 06:28 AM
#8
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Original Poster
Rep:
Quote:
Originally Posted by
Firerat
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
09-10-2019, 06:34 AM
#9
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
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 .
09-10-2019, 06:41 AM
#10
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
09-10-2019, 06:49 AM
#11
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
@OP: Could you please quote a complete minimal example that shows the problem? (If it is not solved yet.)
09-10-2019, 07:52 AM
#12
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Original Poster
Rep:
Quote:
Originally Posted by
NevemTeve
@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 .
09-10-2019, 08:06 AM
#13
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
09-10-2019, 08:07 AM
#14
Member
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48
Original Poster
Rep:
Code:
echo ''''+teamname+'-REL''' > rel
would pass
Code:
echo 'valueofteamname-REL' > rel
[/QUOTE]
Tried this but no joy either. The echo comes back blank.
09-10-2019, 08:17 AM
#15
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
Quote:
Originally Posted by
flukeyLinux
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
All times are GMT -5. The time now is 08:47 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News