• Uncategorized

About linux : cut-choice-parameter-value-in-executable-shell-script-of-jenkins

Question Detail

I have a choice parameter in Jenkins FreeStyle Job Type.

Choices Are for Variable ${IP}:

192.168.1.33-prod
192.168.1.34-qa
192.168.1.35-stage

In The Executable Shell Script,I want to remove the the value after “-” in the selected choice parameter, before the value is assigned to the command.

The Command Executed:

rsync --owner=ec2-user --group=ec2-user -O --no-p  -arzh --exclude ".git/" --perms --chmod=a+rwx /tmp/some-value/ ec2-user@${IP}:/some-folder/

The Linux Command is:

echo ${IP} | cut -f1 -d"-"

The Result Should be
Result:

192.168.1.33

The Final Command before execution should look like:

However, the value is coming empty when i try the below way:

rsync --owner=ec2-user --group=ec2-user -O --no-p  -arzh --exclude ".git/" --perms --chmod=a+rwx $WORKSPACE/ [email protected]:/some-folder/

Question Answer

The problem is probably related to the step where you re-assign the value of the IP variable — that step is missing in your question.

However, in your case it could be more elegant to use shell parameter expansion instead of cut. With %%, you can remove the longest matching pattern at substitution time, so this should do the trick:

rsync [...] ec2-user@${IP%%-*}:/some-folder/

For details, see the bourne shell manual page.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.