Not able to access environment variables in bash scripts - bash

I have previously set some environment variables of my own like export EDITOR="vim" in my /etc/bash.bashrc. Also I have showmessage script:
#!/usr/bin/env bash
notify-send -t 50000 -u normal "editor is: $EDITOR"
When I run the script in the terminal by "~/scripts/showmessage" everything is fine and I get the desired output, i.e. "editor is: vim".
But when I call/run the script through keybindings like Mod+t it can't get the $EDITOR value and gives me "editor is: ".

Related

How to set zsh $RPROMPT from parent bash

I have a file that customize the rprompt like this :
#!/bin/bash
# Environment ....
export PATH="/some/path/:$PATH"
export CMAKE_PREFIX_PATH="/a/really/nice/path"
# Tell the user we are in the environment
export RPROMPT="BUILD ENV - $RPROMPT"
echo "********* Entering build environnment *******"
$SHELL
echo "********* Exiting build environment *********"
But it doesn't work. $SHELL is zsh. How can I set the $RPROMPT variable from a parent bash script without having to modify the user configuration files? (.zsh, .bashrc...)
I can't use zsh in the parent script in case of the user doesn't have zsh. But if he has zsh, it should add this optional feature to modify the prompt.

"Set echo" doesn't seem to show code in tcsh script

Please nothing in the realms of "Why are you using TCSH?". I have my reasons.
I'm trying to debug a tcsh script, but using the options "set echo" and "set verbose" don't actually seem to show the code that I'm trying to debug.
Per this question, I tried "set echo" and "set verbose" in tcsh. I then ran this script 'test.tcsh':
echo "Hello world"
foo=1
bar=2
foobar=$(expr $foo + $bar)
echo $foobar
It returns the following output:
test.tcsh
test.tcsh
Hello world
3
history -S
history -M
So it shows clearly the output of the code. However, what I want to see is the code itself - the echo, the call to expr and so on. In bash, set -xv would do what I want, but it's seemingly not working here.
Anything I'm missing?
To be sure your script is run by the tcsh shell and to get it showing the code, simply add the following line as the first line of your script :
#!/bin/tcsh -v
This will make your script run by tcsh shell and set the tcsh shell to echo each script commands.
For reference, your actual script in the question doesn't seem to be a tcsh script, see comment under your question.
EDIT: To debug without altering the script, you can also simply launch the tcsh shell with the -v parameter followed by the script filename :
$ /bin/tcsh -v test.tcsh

Setting Environmental Variables Through a BASH Script in Jupyter

I am attempting to execute a BASH script which sets needed environmental variables from within a Jupyter notebook. I understand that the magic command %env can accomplish this, but the BASH script is needed in this instance. Neither the use of !source or %system accomplishes the goal of making the environmental variables persist within the Jupyter notebook. Can this be done?
You could use python to update os variables:
Cell
! echo "export test1=\"This is the test 1\"" > test.sh
! echo "export test2=\"This is the test 2\"" >> test.sh
! cat test.sh
Result
export test1="This is the test 1"
export test2="This is the test 2"
Cell (taken from set environment variable in python script)
import os
with open('test.sh') as f:
os.environ.update(
line.replace('export ', '', 1).strip().split('=', 1) for line in f
if 'export' in line
)
! echo $test1
! echo $test2
Result
"This is the test 1"
"This is the test 2"
To permanently set a variable (eg. a key) you can set a Bash environment variable for your Jupyter notebooks by creating or editing a startup config file in the IPython startup directory.
cd ~/.ipython/profile_default/startup/
vim my_startup_file.py
The file will be run on Jupyter startup (see the README in the same directory). Here is what the startup .py file should contain:
1 import os
2 os.environ['AWS_ACCESS_KEY_ID']='insert_your_key_here'
3 os.environ['AWS_SECRET_ACCESS_KEY']='another_key'
Now inside a Jupyter notebook you can call these environment variables, eg.
#Inside a Jupyter Notebook cell
import os
session = boto3.session.Session(
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
region_name='us-east-1'
)
You will need to restart your kernel for the changes to be created.

How can I pass command line arguments when activating a conda environment in a shell script

I am on Linux, Python 3.6.
I have a shell script that looks like this:
#!/bin/bash
instance=$1
export instance
echo "Instance is" $instance
. /home/xyz/setenvvars.sh
source activate myenv
echo arg1 $1
echo env $instance
python myprg.py $1
python myprg.py $instance
It seems after I activate the conda environment, the command line argument this shell script received is not available in the activated environment. How can I pass the command line argument this script originally received to newly activated environment. The 2 echos after the activate show blanks.
TIA!

Problem with bash script

I'm using this bash script:
for a in `sort -u $HADOOP_HOME/conf/slaves`; do
rsync -e ssh -a "${HADOOP_HOME}/conf" ${a}:"${HADOOP_HOME}"
done
for a in `sort -u $HBASE_HOME/conf/regionservers`; do
rsync -e ssh -a "${HBASE_HOME}/conf" ${a}:"${HBASE_HOME}"
done
When I call this script directly from shell, there are no problems and it works fine. But when I call this script from another script, although the script does its job, I get this message at the end:
sort: open failed: /conf/slaves: No such file or directory
sort: open failed: /conf/regionservers: No such file or directory
I have set $HADOOP_HOME and $HBASE_HOME in /etc/profile and the script does the job right. But I don't understand why it gives this message in the end.
Are you sure it's doing it right? When you call this script from the shell it is acting as an interactive shell which reads and sources /etc/profile and ~/.bash_profile if it exists. When you call it from another script it is running as non-interactive and wont source those files. If you want a non-interactive shell to source a file you can do this by setting the BASH_ENV environment variable.
#!/bin/bash
export BASH_ENV=/etc/profile
./call/to/your/HADOOP/script.sh
Everything points to those variables not being defined when your script runs.
You should ensure that they are set for your script. Before the first loop, place the line:
echo "[${HADOOP_HOME}] [${HBASE_HOME}]"
and make sure that doesn't output "[] []" (or even one "[]").
Additionally, put a set +x line at the top of the script - this will output lines before executing them and you can see what's being done.
Keep in mind that some shells don't pass on environment variables to subshells unless you explicitly export them (setting them is not enough).

Resources