Working on Chalice project. Running an env.sh shell script won't set env variables, but running `export DB_USERNAME=example` sets it. Why? - python-poetry

I am working on a chalice project. I need to set environment variables locally in order to connect to the DB using peewee.
I wrote a simple shell script called env.sh:
#!/bin/sh
export APP_NAME=example
export DB_HOST=example
export DB_PORT=example
export DB_NAME=example
export DB_USERNAME=example
export DB_PASSWORD=example
I expected that running ./env.sh will set all the env variables. But when I execute the chalice application, I get errors indicating that these variables are not defined.
If I go into the terminal and run these commands individually like export APP_NAME=example, the variables are set and the application successfully reads them.
I am using a virtual env with poetry (poetry shell) with python3.9, so it might have to do with that, but I am not sure how to wrap my head around this. I also tried poetry run env.sh and removed the shebang from the top of the script, but none of these worked.
Can you help me figure out why this is happening and how to make that script work?

Related

Setting up system shell environment from a Node.js script

I want to use a Node.js script to setup my shell environment instead of using Bash.
For example:
.zsh
# I want to do something like this:
node setup-shell.js
# Instead of this:
export foo="foo"
alias blah="blah"
source /path/to/blah.sh
Mostly it's a lot of export and alias. But then there are some functions declared too.
Ideas
coproc - https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html
https://unix.stackexchange.com/questions/86270/how-do-you-use-the-command-coproc-in-various-shells
Related
Is it possible to set system environment variables from a node script?

Setting FLASK_APP in an (semi)automated way

Before I run my Flask application using
$ flask run
I need to set few environment variables (e.g. FLASK_APP pointing to the application file). I've been trying to automate the process with a .sh script:
export FLASK_APP=application.py
export FLASK_DEBUG=1
echo $FLASK_APP
echo $FLASK_DEBUG
It turns out that even if the last two lines print out the input from the first two lines, Flask doesn't seem to care. If I want to set the variables so that Flask changes its behaviour, I must do it manually by typing the first two lines from the script in the terminal:
$ export FLASK_APP=application.py
$ export FLASK_DEBUG=1
What am I doing wrong when it comes to the script?
You are probably running your shell script as:
$ path_to_your_script.sh
This loads another shell and executes a script.
If you want to set environment variables for you current shell, use:
$ source path_to_your_script.sh
or (dot at the beginning)
$ . path_to_your_script.sh

bash script not picking up environment variables

I have a strange situation where I'm using zsh full-time, and any bash scripts I run are not picking environment variables properly. Obviously I don't expect bash to pick up env vars that are defined in zsh's environment, so I am using ~/.bashrc and ~/.bash_profile, but that doesn't work either.
For example, here's a test script:
#!/bin/bash
echo $MYTEST
I've added this line to both ~/.bashrc and ~/.bash_profile to cover my bases:
export MYTEST="hello"
I just get a blank line when running the script.
PS: I know running . ./testscript will work, but that's not an option since it's a system-wide script that's failing to pull env vars.
Oops. Maybe I should try having export VAR=val in my ~/.oh-my-zsh/custom/vars.zsh instead of just VAR=val!

Export environment variables using script

I need to set some environment variables in Ubuntu. I do the following and it works:
export PATH="/home/vagrant/ns-allinone-2.35/bin:/home/vagrant/ns-allinone-2.35/tcl8.5.10/unix:/home/vagrant/ns-allinone-2.35/tk8.5.10/unix:$PATH"
export LD_LIBRARY_PATH="/home/vagrant/ns-allinone-2.35/otcl-1.14:/home/vagrant/ns-allinone-2.35/lib"
export TCL_LIBRARY="/home/vagrant/ns-allinone-2.35/tcl8.5.10/library"
But I move the same thing in a script envexport.sh and execute it, the environment variables are not getting set.
Where am I going wrong? How to accomplish this?
Thanks.
If you just run the script, the environment variables get destroyed when the script finishes.
Use . envexport.sh. That way the commands get executed in the current shell (environment).
When you run a command in the shell, the shell creates a subprocess (child process). All the environment variables which were defined or changed down in the subprocess will be lost to the parent process.
However if you source a script, you force the script to run in the current process. That means environment variables in the script you ran will not be lost.
One thing that may help is if you will want those variables set for all of your sessions you can place the same commands in your .bashrc file by running the following command and pasting the lines in the file.
vim ~/.bashrc
and the run
source ~/.bashrc
in any terminals you currently are running. If you start any new terminals they will automatically have your directories added to your path.

Shell script problem to set my env

We have few executable which need some environment setting.
We manually running those scripts before running the executable
Like
$ . setenv.ksh
We have to encompass call these in one script to avoid the manual work.
We written a sh script like
#!/bin/sh
. setenv.ksh
./abc &
Still the environments are not setting in that session. I think the “. setenv.ksh” runs with fork and it’s not setting the environment.
Please me to solve this problem. Which command we use to run the setenv.ksh so, this will work fine.
Thanks
I notice the environment script is called setenv.ksh but you try to run it from /bin/sh. Maybe your system has a shell other than ksh as /bin/sh and it misparses something it setenv.ksh. Try changing the shebang line to #!/bin/ksh (or whatever the path to ksh is on your system).
In setenv.ksh, you need to export all environment variables you set so that any sub-shell will inherit the values:
export MYENV=myValue

Resources