DejaGNU - want to use environment variable from Linux throughout tests - linux

I have some existing DejaGNU tests I need to modify, I want to replace some hard coded /dev entries with an environment variable, e.g. instead of /dev/ttyS0 I want /dev/$PORT where PORT is defined in the parent Linux shell.
How do I get a variable from the parent shell into DejaGNU?

$env(PORT)
If you want to test whether the environment variable is set, you can use
[info exists env(PORT)]

Related

How to list environment variables of ALL the processes in a systematic and hierarchical manner

I would like to check on how environment variables are populated and inherited by the Linux processes. It might be particularly useful on those processes not having explicit shell as a parent (Docker container with exec launch type etc)
It would be perfect to get that list in an organized and expressive form like for example pstree draws the list of processes with optional extra information.
Unfortunately I have not found a way to have pstree list the env vars for all the processes in the tree. There is an -a option but looks like it may help only if env vars are passed in a form of command line arguments (which is not always the case)
Of course it is possible to got individual process variables by e.g. obtaining PIDs with the pstree -p and then extracting the envs data with cat /proc/<PID>/environ however it is not exactly what I want.
This can't be done in a portable fashion. Heck, I don't even know of a way to do it non-portably. Env vars are stored in the address space of the process. On Linux the /proc/$pid/environ only shows the vars as they existed when the process started. It does not show any subsequence modifications to the env vars the process may have made with putenv() or setenv() or whatever mechanism the language the process was written in uses. Put this in a file named env_test:
#!/bin/sh
export WTF=hello
export PATH=XXXXXXXXXXXXXXX
./env_test2 &
/usr/bin/sleep 999
Put this in a file named env_test2:
#!/bin/sh
export WTF=goodbye
export PATH=ZZZZZZZZZZZZZZZZZZZZ
/usr/bin/sleep 999
Now type ./env_test & then ps waux | grep env_test to get the pids of the two scripts. Examine the respective /proc/$pid/environ psuedo-files and notice that the changes made by env_test are inherited by env_test2 and are reflected in its magic /proc/$pid/environ file but the exports done by each script are not reflected in that content.

Passing and updating an environment variable constantly

I need a way to constantly update a environment variable from a script. I need to use that environment variable in another program almost real time.
What I have is this code:
# !bin/bash
while :
do
AMA="$(cut -c 1-13 text.txt)"
source directory/this_script
done
I am getting the right information from my file when running cut this way. I just need to get this variable permanently updating if possible. Also a way to even get it to the environment.
Slackware Linux

FISH: How to set a specific $fish_function_path while launching fish?

I'm trying to use the sshrc to execute fish with my own defined functions at the remote server.
I know there is a $fish_function_path variable that I can modify to prepend my custom path with my functions, but this variable seems global and I don't want to affect other fish users.
Is there any way to change this variable only for my single fish session or any other ways to use my fish functions only for a single session?
I have also tried setting and environment variable from the outer shell: fish_functions_path="$MY_FUNCTIONS_PATH" fish but it seems environment variables don't affect those inner variables of fish.
Used this docs.
Thanks!
Global variables, including $fish_function_path, are per-session. You might be thinking of universal variables, which are shared across sessions for a given user.
It should be fine to modify $fish_function_path for a given instance of fish - it won't affect any others.

Puppet missing Facter environment variables

Any environment variable prefixed with "FACTER_" is automatically added to the facter collection. I've successfully added a "FACTER_" environment variable it is indeed showing up in the facter -p list, so it should be able to be used by puppet...
The problem, though, is in my .pp file the variable name that should be set to the FACTER_ value is empty (or non existant)
Is there something else I need to do to get FACTER_ variables into puppet variables?
Cheers
You are most likely setting up the system so that the FACTER_ variables are available in interactive shells. This is not sensible if you want your background agent to respect them.
I can see two direct approaches:
Modify your initscript or its configuration to set the appropriate environment variables.
Forego the approach entirely and use /etc/facter/facts.d instead.
I would advise towards the latter.

Child shell does not get empty environment variables and arrays of parent shell

After installation of Ruby Version Manager (RVM) as root on an Ubuntu 14.04. I am confronted with a strange behaviour of bash. Let's have a look at the exported environment variables. I login as user ubunutu and run exportin my bash. Here are three of rvm's exported environment variables, others are available:
declare -ax chpwd_functions='([0]="__rvm_cd_functions_set" [1]="__rvm_after_cd")'
declare -x rvm_version="1.25.28 (stable)"
declare -x rvm_ruby_mode
Everything is pretty fine, but when I run bash -c export we get only:
declare -x rvm_version="1.25.28 (stable)"
Can someone explain me why all empty environment variables and all arrays are removed in the child bash? What must I do to ensure that really all environment variables of a parent shell are available within a child shell?
This problem is really a blocker for me. I am using vagrant and its shell provisioner. In one script I setup rvm and in a second one I must configure some gemsets. The problem is that in the second script the rvm commands do not run. The active shell only gets those environment variables of rvm with are non-arrays and non-empty. Manually sourcing of rvm.sh is no solution!
It is because as per last line of man bash:
Array variables may not (yet) be exported.
I read somewhere a note by BASH developer that it is because exporting an array is very complex and error prone.
Also this line:
declare -x rvm_ruby_mode
is only declaring a name of the variable with export attribute set (without value), if you assign it a value it will be available in the sub shell.
Here is post by BASH author on export of array in BASH.
A variable is created when a value is assigned to a name using the = operator, for example
foo=bar
creates a variable named foo with the value bar.
declare is used for two reasons: one, to allow dynamic creation of variables (which is beyond the scope of this question), and two, to set attributes on names (not necessarily variables). The command
declare -x rvm_ruby_mode
simply sets the export attribute of the name rvm_ruby_mode. To actually create a variable whose name has the export attribute set, you need to use the = operator, just as without the declare command.
declare -x rvm_ruby_mode=
Now rvm_ruby_mode is an empty variable whose named is marked for export.
I say "marked for export" because variables are not exported until a subshell is created. Until then, there is simply a list of names that, if the name has a value when a subshell/child process is created, are copied into the new environment. This list is separate from the list of actual variables (which again are names with associated values).
As to why arrays cannot be exported? The environment is technically not a set of variables, since a variable is a shell construct and the environment is something used by all processes in POSIX, whether or not run by a shell. The environment is simply a list of strings of the form <name>=<value>. There is no standard for how to pack the elements of an array into a single string which any process can parse and reconstruct into an appropriate data structure. While it's possible that bash could make an exception if it new the child process was another bash shell and come up with some way of embedding an array in the environment (like it does with function definitions), apparently this has not been done.

Resources