[go-nuts] Re: Pass variables to bash

2017-02-02 Thread Rich
Thanks everyone for your input... I tried HowardCShaw's method before 
posting, but I went back and experimented more with it and found that the 
output was putting all the variables on one line. I separated them all into 
separate lines and it works! 
What I was doing:
exportVar=`
export var1="Foo Bar"
export var2="Bar Foo"
export var3="Far Boo"
...
export var34="ooB raF"
`
fmt.Println(exportVar)

In bash:
myVars=`goSetVar`
eval myVars

myVars would equal all of the lines above on one line.  What I did was just 
put every line with fmt.Println and that seems to work.


On Thursday, February 2, 2017 at 11:23:49 AM UTC-5, Rich wrote:
>
> Hi All,
>
> My dev team did a very bad thing and issued lots of scripts that they 
> wrote source a file that is in clear text which contains usernames / 
> passwords etc.  Without having to re-write a ton of existing bash scripts, 
> I wanted to use Go and have that set the usernames / passwords and 
> introduce some user validation etc. Is there a way for Go to set 
> environment variables that persist to the script that ran the go program? 
> For example:
>
> Go Program SetPass:
>
> package main
>
> import (
> "fmt"
> "os"
>
> )
>
> func main() {
>
>  os.Setenv("MyPass","Abc.1234")
>
> }
>
>
>
> Bash Script:
>
> #!/bin/bash
>
> /usr/local/bin/SetPass
>
> echo $MyPass
>
>
> This is a simplistic example, in the real script I'd do things like look 
> at the user / groups that are running the program to ensure that they've 
> got the rights to run.
>
> Thanks,
>
> Rich
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Pass variables to bash

2017-02-02 Thread howardcshaw
I don't think you can do what you want to do, precisely.

But, I do think you can solve your problem!

Instead of actually writing to your environment in the Go code (which can 
only be seen by *child* processes *of the Go program*!), just print to 
stdout.

That is, instead of os.Setenv("MyPass","Abc.1234"), you would just do 
fmt.Printf("export %s=%s\n", envVarName, envVarValue),

so the output of the program is lines like 

export MyPass=Abc.1234
export somethingelse=thisotherthing

Then take that output and eval it in the shell script.

Example:

osm@Beast:~/test$ echo "#!/bin/sh
> echo export TEST=TRUE
> echo export TEST2=FALSE" > test.sh

osm@Beast:~/test$ cat test.sh
#!/bin/sh
echo export TEST=TRUE
echo export TEST2=FALSE

osm@Beast:~/test$ env | grep TEST
osm@Beast:~/test$ ./test.sh
export TEST=TRUE
export TEST2=FALSE
osm@Beast:~/test$ env | grep TEST
osm@Beast:~/test$ eval "$(./test.sh)"
osm@Beast:~/test$ env | grep TEST
TEST=TRUE
TEST2=FALSE

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Pass variables to bash

2017-02-02 Thread mhhcbon
this : 
http://craigwickesser.com/2015/02/golang-cmd-with-custom-environment/ ?


On Thursday, February 2, 2017 at 5:23:49 PM UTC+1, Rich wrote:
>
> Hi All,
>
> My dev team did a very bad thing and issued lots of scripts that they 
> wrote source a file that is in clear text which contains usernames / 
> passwords etc.  Without having to re-write a ton of existing bash scripts, 
> I wanted to use Go and have that set the usernames / passwords and 
> introduce some user validation etc. Is there a way for Go to set 
> environment variables that persist to the script that ran the go program? 
> For example:
>
> Go Program SetPass:
>
> package main
>
> import (
> "fmt"
> "os"
>
> )
>
> func main() {
>
>  os.Setenv("MyPass","Abc.1234")
>
> }
>
>
>
> Bash Script:
>
> #!/bin/bash
>
> /usr/local/bin/SetPass
>
> echo $MyPass
>
>
> This is a simplistic example, in the real script I'd do things like look 
> at the user / groups that are running the program to ensure that they've 
> got the rights to run.
>
> Thanks,
>
> Rich
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.