For example, I would like to use $USER and $HOSTNAME which is defined when I login.
however
set g_user [exec echo \$USER]
shows me only an error that \$USER is not recognizable.
The env array is what you need:
# if you use env in a procedure, you need to specify it is a global variable
global env
set g_user $env(USER)
Documented at http://tcl.tk/man/tcl8.5/TclCmd/tclvars.htm
Related
In a Dockerfile I have
ENV JAVA_OPTS="-DmyKeyStore=${blah1} -DmyApi=${blah2} -Dsalt=${blah3}"
The ${blah} variables are populated during our CI/CD run but I want the Docker build to fail if one of the parameters fails to get populated.
I can use the below code to check if ENV JAVA_OPTS as a whole isn't populated.
RUN if [ -z "$JAVA_OPTS" ]; then echo 'Environment variable JAVA_OPTS must be specified. Exiting.'; exit 1; fi
but I want to do a deeper check within that variable and fail if -DmyKeyStore=blank/null for example.
Are you sure docker uses bash, and not something internal? If it uses bash, then you could always use the ${param:?message} parameter expansion to generate an error.
Something like this:
JAVA_OPTS="-DmyKeyStore=${blah1:?} -DmyApi=${blah2:?} -Dsalt=${blah3:?}"
You can test it in an interactive shell like this:
$ : ${t:?}
bash: t: parameter null or not set
$ t=1
$ : ${t:?}
Following is a screenshot of how I am setting an environment variable in Jenkins.
I am trying to access it in a shell script, but it is not echoing its value.
echo "Starting ..."
echo ${BUILD_NUMBER}
echo ${DATABASE}
${BUILD_NUMBER} is a global env variable.
The correct use of variables in Jenkins is the following:
echo "The current build number is: ${BUILD_NUMBER}"
The important part is to use ". If you would use ' Jenkins would not notice that you want to access the variable and simply print your variable as string.
Source: Jenkins String Interpolation
Theres a similar question to this, but cant manage it to work:
I want to simply set an env variable, then use it:
execute "start zookeeper" do
cwd "/opt/zookeeper-3.4.5/bin"
command "./zkServer.sh start"
environment "JVMFLAGS" => "-Xmx#{heap_jvm} -Xms#{heap_jvm}"
user "root"
action :run
end
I've also tried using bash to "export JVMFLAGS='-blabla'" but still it runs the sh with none set to the variable. Is there some issue preventing my sh script from checking the variable?
I could use the sh like a template and replace the ocurrence of JVMFLAGS... But i want to check if theres a better solution..
Have you tried setting environment variable through Ruby just before the execute block? Chef actually recommends using ENV (See the note on that page).
ENV['JVMFLAGS'] = "-Xmx#{heap_jvm} -Xms#{heap_jvm}"
Another possibility is to add JVMFLAGS to the command itself.
execute "start zookeeper" do
[...]
command "JVMFLAGS=-Xmx#{heap_jvm} -Xms#{heap_jvm} ./zkServer.sh start"
[...]
end
I have a script that requires the env variable USER to be set. As the script is used by several users, I can't just do export USER=xxx at the beginning of the script. I could define in the crontab, but I was just wondering whether there is a good way of pulling it in.
I tried sourcing .bashrc and .profile, but neither define USER, plus on Ubuntu .bashrc simply returns on non-interactive shells.
You could work around it by writing at the top of the script (Bashism):
USER=$(whoami)
or old-style:
USER=`whoami`
... assuming you have whoami in the PATH, which can also be set in the crontab just like several (most?) other variables. I.e. you can also set the variable in crontab itself (at least in Vixies cron) - see here for example.
Use the env command. Your crontab entry could look like:
* * * * * env USER=foouser /path/to/script.sh
You can specify environment variable before the command. This way, it won't affect anything else in the crontab.
user.sh:
#!/bin/sh
echo $USER
cli:
USER=foo ./user.sh ## outputs "foo"
I'm trying to write a simple script that will set proxy settings. Actually I just need to export http_proxy ftp_proxy https_proxy ... variables with export command.
But it's not working when I run it manually from the shell because export affect only current shell and subshells, but no others. Also I don't want to call it from .bashrc because it's not my default proxy settings.
So how should I export http_proxy variable to make effect globally?
Back in the day I was also sick of setting and then unsetting the proxy settings after my work was done. I always wished if there was a command simple command to do the set and unset function for me.
Then I figured that if I create a new function in my .bashrc I can call it from the command line by using the bash-tab-completion. Saves even more time.
This is what I did:
$ vi ~/.bashrc
function setproxy() {
export {http,https,ftp}_proxy='http://proxy-serv:8080'
}
function unsetproxy() {
unset {http,https,ftp}_proxy
}
$ . ~/.bashrc
Now I just do:
$ setproxy
or
$ setp<TAB> and <ENTER>
and it sets the proxy for me. Hope this helps.
Instead of doing this in a script, make this a function. You can declare this function in your .bashrc:
function set_custom_proxy() {
export http_proxy='http://myproxy:3128'
}
Then run this in the current shell:
echo $http_proxy
set_custom_proxy
echo $http_proxy
It works as a variable modification in a function is not local to the function.
EDIT:
FYI: to use a local variable in a function, you need to use the local keyword:
atest="Hello World"
btest="Hello World2"
function my_func() {
local atest;
atest="Hello World3"
btest="Hello World4"
echo $atest
echo $btest
}
echo $atest
echo $btest
my_func
echo $atest
echo $btest
Since you can't access .bashrc, you can use source command which will run in the current shell's context and all the variables you set will be available.
source ./script
If you don't want to modify the .bashrc file run your script with .
. script.sh