I tried so many ways to store a value in jenkins. But I could not assign a value into a variable in Shell script of Jenkins.
First I have set a ENV variable called branch which has the value is */release/hotfix
So I tried to spliit that word and save in new variable like this.
VALUE=$($branch | sed -e 's/\*\/.*\///g')
I tried so many experiment to save the value. It won't work.
In my research, I found a way to assign a value to variable but the plugin ( How to set environment variables in Jenkins? ) may not be safe to use.
why cant i save value to variable in Jenkins shell? How do I do that?
I found the way to achieve this problem.
first I create a file using touch command
touch enviroments.cfg
Then save value into variable while saving to that file
echo "export VALUE=$(echo $branch | sed -e 's/\*\/.*\///g')" >> enviroments.cfg
to access the that saved variable again, I reopened that file and get that value like this
. './enviroments.cfg' (you can not use **source** command to read, use **dot**(.) instead of **source** command)
echo $VALUE
You can read values anywhere like this.
Related
Anyone know how I can set the environment variable value based on unix terminal command in jenkins using inject variable or some other way. e.g CurrentBranch=ls /project/folder | grep my_name. This doesn't seem to work. I want this command,ls /project/folder | grep my_name, to be evaluated and the result should be set to CurrentBranch.
Some reason CurrentBranch variable value never gets evaluated but rather it always has a value as this: ls /project/folder | grep my_name
Anyone knows what I am doing wrong.
Thanks.
I don't think you can do this in a way so that it is available to any Jenkins task, which is written.
You can however write your Jenkins task so that all of them are executing a shell script, and then your question boils down to sourcing a suitable rc file (we use for instance zsh as a shell and have a special rc file just for Jenkins), and inside this file, you can set your environment in the usual way, i.e.
export $(ls /project/folder | grep my_name)
(Of course, grepping the output of ls in this way is not the best idea, but this is a different topic)
You can save CurrentBranch in a file called env and inject that file into your job using EnvFile Plugin
echo "CurrentBranch=`ls /project/folder | grep my_name`" > /tmp/env
In the jenkins job configuration, check the checkbox "Prepare an environment for the run" .
select below checkboxes
Keep Jenkins Environment Variables
Keep Jenkins Build Variables
in the "Script Content" section, run below command to create properties file
echo "CurrentBranch=`ls /project/folder | grep my_name`" > /tmp/jenkins_job.prop
then in "Properties File Path" section, give this properties file with full path name.
now CurrentBranch variable will be avialable through out the build and can be access using ${CurrentBranch} .
I can find lots of source about how to set environment variables but, i'm looking to display a variable rather than set it.
I've wrote a custom motd file and I want it to display the $LOCATION environment variable in the output of the motd script, how is it done please?
I'm not sure, if I'm misunderstanding, but...?
echo $LOCATION
/etc/motd is a static file. You can generate it with another script, but as far as I can tell login won't expand anything in that file.
i am trying to set the output of a command to a variable in a script. i have searched all day and it looks like i have the command right. but each time i run my script, it just runs the command instead of only assigning it to a variable and not running it.
here is the relevant part of the script:
ffname=$(rename 's/_1080p_12000//' *.*)
echo $ffname
i need to use ffname as a variable in another part of the script without running the command.. but it doesnt seem to wanna work. anyone have any suggestions?
You can add a -n(no-act) option to rename to get the output.
ffnames=($(rename -n 's/_1080p_12000//' *.* | awk '{print $1,$NF}'))
The result contains multiple items. So you need a array to hold them.
Then do real renaming without -n option.
If you're using Bash, you can use the shell parameter expansion substitution operator on a populated variable to replace the portion of the filename that you want to delete. For example:
$ ffname=foo_1080p_12000.bar
$ echo "${ffname/_1080p_12000}"
foo.bar
However, it will remain up to you to perform the actual mv or rename commands, or to re-assign the modified value back to ffname if you like.
I am using the sh 3.2 in Mac Os X. I have a file test.conf
config1="Configuration 1"
config2="a lot of text"
config3=...
So I only need to get the config1= and config2= parameter. How can I set a variable, that I can do this:
> echo $variable
Configuration 1
So simple, but I am not doing it work.
the sommand you are looking for is source
source test.conf
echo $config1 #echoes Configuration 1
if you need to have config1 in variable, add
varible=$config1
At a rough guess...
export `grep 'config1=' /your/config/file`
export `grep 'config2=' /your/config/file`
But remember if you put this in a shell script file, then you'll need to eval the file rather than execute it to set the variables in the current shell instance.
You could do this:
variable=`sed -n 's/^config1=//p'`
Or if you are attempting to evaluate certain parts of your file, try something like
eval `grep ^config1= test.conf`
to have config1=Configuration 1 evaluated by the current shell. (With the example you provided, this will cause a syntax error, because the value cannot contain unquoted whitespace.)
I generally recommend beginners to stay away from backticks, but this is a situation where they are a good answer.
Why does the following work from the prompt but fail when stuck inside a bash script? The bash script produces one empty line leading me to believe the variable isn't being set:
echo "red sox" | read my_var
echo $my_var
UPDATE: Since I guess my example isn't working, what I'm really trying to do is take I/O and pipe it into a variable to so I can do things with it. How do I do this? I thought this should be taken care of by the read command, but maybe there's another way?
If you are asking this because you simplified from a more general problem such as:
someprog args | read my_var
you should be using command substitution:
my_var=$(someprog args)
The reason read doesn't work in a pipe the way you have it is that it creates a subshell. Variables set in a subshell don't persist to their parents. See BashFAQ/024.
You can store the output of a command in a variable using either backticks or $() syntax:
my_var=`cat /some/file.txt`
Now the content of /some/file.txt is stored in $my_var. Same thing, with different syntax:
my_var=$(cat /some/file.txt)
It doesn't work at the prompt. My guess is you already have my_var set in your CLI, and are just retrieving that at the prompt.
Try this:
$ my_var="nothing"; echo "red sox" | read my_var; echo $my_var
If you want the my_var variable to have a constant value, as in your question, why not just do:
my_var="red sox"
What are you trying to do? Please explain what you wan't to do first.
If you're trying to set a variable this is what you need:
my_var="red sox"
echo $my_var
If you need it globally you should set it in env:
export my_var