Setting envs or globals in bash script [duplicate] - mysql

How can I keep the environment variables, set from a shell script, after the script finishes running?

This is not possible by running the script. The script spawns it's own sub-shell which is lost when the script completes.
In order to preserve exports that you may have in your script, call it
either as
. myScript.sh
or
source myScript.sh
Notice the space between the . and myScript.sh; also note that "source is a synonym for . in Bash, but not in POSIX sh, so for maximum compatibility use the period."

run the script as follows:
source <script>
-OR-
. <script>
This will run the script in the current shell, and define variables in the current shell. The variables will then be retained in the current shell after the script is done.

Environment variables exist the the environment that is private to each process. The shell passes a COPY of exported variables to it's children as their environment, but there is no way for them to pass their environment back to the parent or any process other than their children.
You can print those variables and load them into the parent.
Or as has been already mentioned, you can run your script in the current shell with either "source script" or ". script" (and you might need ./script if . isn't in your PATH).
Some tools print their vars and the shell can load them using backticks like ssh-agent. That way whatever ssh-agent prints will be run as a command. If it prints something like "VAR1=VAL;VAR2=VAL2" that may do what you want.

Related

When you write a Powershell function how long does it last in memory?

When you write a Powershell function how long does it last in memory. So I have developed a custom function and ran it to create the function, does this function remain on my machine permanently so all I need to do is call the function? If i turned my machine on and off and re-booted can I call the function etc. Does another user need to run the function first before they can use it?
The function exists only in the session where it's code is executed. As soon as you close the Powershell process it's gone. But you can just load up the function definition again and it's back. If you want to have a function in every powershell session available create a module and add it to you userprofile (here is how).
function foo {write-host "I am alife"}
foo
Powershell functions last as the powershell session itself, so when you define a function in the console, you can still call it as this console is running, once you close it, you will have to define the function again.
However, Powershell has specific scripts in the system that runs whenever Powershell session is started, these scripts are called Powershell profiles.
So in your case, if you put the function in Powershell profile, it will be defined directly when you open your Powershell session and it will be permanently defined.
Look here to see about powershell profile
This code will put your function in the profile:
New-Item -Type Directory -Path "$home/Documents/WindowsPowerShell"
"<Your Function Here>" | Out-File -Force -FilePath $profile.CurrentUserAllHosts
Now whenever you open a new Powershell console (or any powershell process runs), your function will be loaded (even after you reboot your system).
If you have a powershell file (.ps1) that contains many functions, you can simply copy its content to $Profile.CurrentUserAllHosts, or put this code in $Profile.CurrentUserAllHosts to import your file as Powershell starts.
Import-Module "<Your File (.ps1) Path Here>"
Update
I forgot to tell you that you have to change your Powershell privacy policy to unrestricted, otherwise you will get error telling you that running script is disabled on this system.

Powershell save functions

I already know how to make a function in powershell. The problem is: How do I save that function so I can use it in the future?
When I write myFunction{3+3} in Powershell I can use that function in that session.
Altough, if I quit powershell and open it again, that function is gone. How do I "save" the function so I can use it even after I restart Powershell?
There are several ways:
You can compose it to a module and load it with import-module functions.psm1 into a script or via use the ps profile.
You can also dot sourc any saved functions into another ps1 ( . .\functions.ps1).
Put it in your Powershell profile:
Rather than explain it here, try this article:
http://www.howtogeek.com/50236/customizing-your-powershell-profile/

Loading tcl extension from tclsh

I have a C extension to Tcl where command mytest is defined. The extension is compiled correctly (I am on Linux, extension is *.so). For example, I can start tclsh and use it like this:
$ tclsh
% load /path/extension.so
% mytest abc
...
But, if I create a file myscript.tcl with the following content:
load /path/extension.so
mytest abc
then I get error:
$ tclsh myscript.tcl
invalid command name "mytest"
while executing
"mytest abc"
(file "myscript.tcl" line 2)
I am using bash on Ubuntu 14.04. Tcl 8.6.
EDIT 1: My question/problem is that I want to use tclsh with a script as an argument - this script should properly load extensions in such a way that mytest and other implemented functions are working without error.
EDIT 2: Uhh, If I use command "source myscript.tcl" inside tcl shell the result is the same. If I use absolute path for myscript.tcl the error is still the same --- "load" executes without warning but I am not sure about it because I get invalid command name "mytest". Maybe the problem is with scope, but it is working correctly when tclsh is used interactively.
If you are using the full path of the extension library in both cases, that part should work identically. It probably is doing though; if it couldn't load it, it would generate an error (which might or might not be helpful, as some of the ways that things fail give very little information; Tcl reports what it has got, but that's sometimes not enough, as it is dependent on the OS to tell it some things). Instead, the problem is probably elsewhere.
The main difference between interactive use and scripted use is that in interactive use, the unknown command will expand unknown command names to Tcl commands that the thing you typed is an unambiguous prefix of. This is convenient, but when converting to a script, you should always use the full command name. OK, not the full full command name — you mostly don't want or need the :: namespace on the front — but without abbreviation, so don't use lappe for lappend. (In interactive use, Tcl will also exec things as external programs without requiring you to type the exec explicitly; again, that's turned off in scripts as it is rather fragile.)
Could it be that this is what is going on? You can check by setting the global variable tcl_interactive to 0 before typing in your code (I recommend using cut-n-paste for that typing, so that you know exactly what is going in). If that fails, it's the interactive-mode helpfulness that is tripping you up. Check what commands you might have as an expansion for a prefix with info commands (after the load, of course):
info commands mytest*
If that just reports mytest, my theory is wrong. (Well, if it does that and the length of that string is 6; there could theoretically be extra invisible characters have been put on the command name, which would be legal Tcl but very nasty and DON'T DO THAT!)

function defined in .zshrc not found when called from a script

I have defined some variables, aliases and functions in my .zshrc file:
export MY_VAR="example"
alias my_alias="echo an example"
function say_hello
{
echo "say hello"
}
I have verified that all three are defined and whatnot when called from the terminal. However, when I try to call the function say_hello from another script (which itself is called from the same terminal), the function does not appear to be defined. I see a 'command not found' error. The alias and variable MY_VAR appear to be defined just fine when referenced from this other script.
Any ideas on what might be going on? Thanks.
When zsh is called from a terminal it is invoked in interactive mode that causes zsh to source additional configuration files, including $ZDOTDIR/.zshrc ($HOME/.zshrc). By default (in non-interactive mode in non-login shell) it sources only /etc/zsh/zshenv* and $ZDOTDIR/.zshenv ($ZDOTDIR is most of time $HOME) files, see last but two section of man zsh named STARTUP/SHUTDOWN FILES. I have no idea why alias is defined (how exactly did you check?), but MY_VAR is defined in script because you exported it.
* /etc/zshenv according to doc, much likely they are Gentoo maintainers of zsh package who changed it to /etc/zsh/zshenv.
Note: it is bad idea to put such functions into .zshenv file as it creates implicit dependency. You should use script libraries instead: put it into
~/.zsh/say_hello.zsh
and do
source ${ZDOTDIR-$HOME}/.zsh/say_hello.zsh
in both your script and .zshrc.

How to get a response from a script back to Hudson and to fail/success emails?

I'm starting a Python script from a Hudson job. The script is started though 'Execute Windows batch command' in build section as 'python my_script.py'
Now I'd need to get some data created by the script back to Hudson and add it to the fail/success emails. My current approach is that the Python script writes data to stderr which is read to a temp file by the batch and then taken into an environment variable. I can see the environment variable correctly right after the script execution (using set command), but in the post-build actions it's not visible any more. The email sending is probably done in different process, so the variables are not visible anymore. I'm accessing the env vars in the email as ${ENV, varname} (or actually in debug mode as $ENV to print them all)
Is there a way to make the environment variable global inside Hudson?
Or can someone provide a better solution for getting data back from Python script to Hudson.
All the related parts (Hudson, batch and Python script) are under my control and can be modified as needed.
Thanks.
Every build step get's is own shell. This implies, that your environment variables are only valid within the build step.
You can just write the data in a nice format to the std output (use a header that is easy to identify) and if the job fails, the data output gets attached in the email.
If you insist on only putting in the data, you can use the following token for the Editable Email Notification post build action (Email-ext plugin).
${BUILD_LOG_REGEX, regex, linesBefore, linesAfter, maxMatches, showTruncatedLines, substText}