Access environment variable in a korn shell script - scripting

my dev/qa/uat/prod servers each have an environmental variable "DATADIR".
For example in a dev linux server:
echo $DATADIR
devl
Can I access that environment variable inside
a korn shell script?

I figured it out after reading more about variables.
System variables are available inside the script.
echo $SOMEPATH
/usr/bin/
path=$SOMEPATH
echo The path you want is $path
The path you want is /usr/bin

Related

Why can't Poetry load my environment variables?

I'm trying to get Poetry to load env vars from a file when the .venv is activated. I can't use the plugin to load a .env because I'm not allowed to use Poetry 1.2.
I've got my env vars in a file called creds.sh.
I've edited both my .venv\Scripts\activate and .venv\Scripts\activate.bat to source from that file:
set -a
. creds.sh
set +a
and,
for /f "delims== tokens=1,2" %%G in (%~dp0..\..\creds.sh) do set %%G=%%H
respectively.
When I open up a new bash, and \activate, it works. I can env | grep <my var> and find my vars.
When I open up a new cmd, and \activate.bat, it works. I can again env | grep <my var> and find my vars.
When I poetry shell or poetry run my-script, it can't find my vars! What the heck is going on.
One thought is somehow poetry's opening up some other kind of shell, so I've tried to look for the $SHELL var inside my .venv but it doesn't seem to exist.
Any ideas? How else might I inject env vars into the Poetry env when the env is activated?

Bind path in singularity using container environment variable

I have a singularity container built with a scientific filesystem app.
I run the container with singularity run --app myapp image.sif and, accordingly, I have the environment variable SCIF_APPDATA=/scif/data/myapp set inside the container.
I'd like to bind a path using this environment variable. Something like:
singularity run -B /my/path/on/host/:$SCIF_APPDATA/input/
Unfortunately this does not work. I didn't manage to make singularity use as a mount path the environment variable with its "internal" value.
I have to explicitly pass the value of the environment variable:
singularity run -B /my/path/on/host/:/scif/data/myapp/input
Does anybody know how to use container environment variables in bind paths?
I don't think it is possible to directly use environment variables from inside the container in the bind statement. However, you can do it in two steps, first a call to singularity exec to get the value of the variable, then use it with singularity run:
SCIF_APPDATA=$(singularity exec ascisoftApp.sif bash -c "echo \$SCIF_APPDATA")
singularity run -B /my/path/on/host/:$SCIF_APPDATA/input/ ...
Don't forget the backslash to escape the $ in the echo command.
Note: Not sure why but it doesn't work for me when I directly use echo, hence the wrapping with bash -c.

Where to set environment variables for non interactive bash use?

If I add an export declaration in ~/.bashrc the environment variable is available only if I use bash interactively, not when I invoke a command as argument to bash, e.g. bash -c export.
Where do I set environment variables so that they are available both in interactive and non-interactive mode?

How can i set a local variable in ssh?

I would like to set a local variable in a ssh command-chain that is only used in this environment:
#!/bin/sh
my_var='/tmp/wrong_file'
ssh user#server "my_var='/tmp/a_file'; cat $my_var;my_var=123;echo $my_var"
echo $my_var
This example the "outer" $my_var is used. How to fix this and use variables "in" the current ssh connection as locally defined? There is no need to change or access the external value '/tmp/wrong_file' in $my_var, as asked in Assign directory listing to variable in bash script over ssh.
You're using the wrong quotes. Parameter expansion is performed inside double quotes, but not inside single quotes.
#!/bin/sh
my_var=/tmp/wrong_file
ssh user#server 'my_var=/tmp/a_file; cat $my_var;my_var=123;echo $my_var'
First of all: The SSH shell and your local shell are completely different and do not exchange any environment variables. This is a good thing - consider environment variables such as LD_LIBRARY_PATH when using SSH between machines of different OS architecture.
IMHO the best solution for your problem is to encapsulate your commands into a shell script on the remote side, then maybe start it with parameters. E.g.:
Remote:
myscript.sh contains:
#!/bin/sh
MY_FILE="$1";
echo "Contents of §MY_FILE:"
cat $MY_FILE
Local:
RUn something like
export REMOTE_FILE='/path/to/it'
ssh user#server "/path/to/myscript.sh '$REMOTE_FILE'"

Can I use GNU make's SHELL variable to connect to a remote shell?

One of the projects I'm working on uses gnu make for testing. I would like to test on a platform that doesn't have a make implementation, but does have a POSIX shell.
Is it possible to create a script (preferably in python) that can "stitch" a remote shell to make, and put it in make's SHELL environment variable?
If not, is there another way anyone can think of to do it?
It is possible.
Create a script that forwards commands to a remote host. For example:
#!/bin/bash
shift # remove -c argument
exec ssh remote_host "$#"
And make it executable (chmod +x).
And then in Makefile:
SHELL := './my_shell.sh'
all :
#echo `hostname` is making $#
Outputs:
$ make
remote_host.peer1.net is making all