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

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!

Related

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.

Unable to run an interactive shell script using conda run

I have this script "/home/prod/test.sh"
#!/bin/bash
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname
I was expecting that when I run this command
conda run -n myenv /home/prod/test.sh
I will see a prompt waiting for me to enter the value for varname , instead I got
(base) prod#dev-box:~$ conda run -n reportz /home/prod/test.sh
Hello, who am I talking to?
It's nice to meet you
(base) prod#dev-box:~$
Would very much appreciate a response detailing, how I can use conda to run an inetractive shell script ?
[Edit]
Replaced read varname with read -r varname < /dev/tty
It did wait for my input, but without displaying the output resulting from the echo statement echo Hello, who am I talking to?
Solution I'm looking for - the script should run with conda run command exactly as it would in a typical shell env.
The conda run command defaults to buffering I/O. To have interaction, one needs to include the --no-capture-output flag.
conda run -n myenv --no-capture-output /home/prod/test.sh
This feature was introduced in Conda v4.9.0 (Change Log).

Not able to access environment variables in bash scripts

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: ".

printing from bash if virtualenv is activated

I want to print in the console if virtualenv is activated.
Given that activated environment is "myvirtualenv", code should be like this:
echo some_function
and result will be
myvirtualenv
virtualenv's activation script sets "VIRTUAL_ENV" environment variable. Just print it:
echo "$VIRTUAL_ENV"

Fish - Get Current Shell Command

I'm trying to get the current shell command (not $history[1] !), specifically i'd like to be able to replace the fish_title - (pwd), with specific running programs i.e. python if I'm in interactive shell, vim etc.
From the documentation:
The first argument to fish_title will contain the most recently executed foreground command as a string, starting with fish 2.2.
So a simple
function fish_title
echo $argv (set -q SSH_CONNECTION; and hostname)":" $PWD
end
should work.
From the documentation, Special Variables
_, the name of the currently running command.
The default fish_title function does this already, does it not?
function fish_title
echo $_ " "
set -q SSH_CONNECTION; and echo (hostname)":"
pwd
end

Resources