Read / Run Environment Variables in a text file - linux

I have an app which runs and reads a text configuration file.
This points to several locations of configurations / outputs etc.
Is it possible to use environmental variables inside the text configuration file, rather than hardcoded paths?
LogFilePath=$LOG_FILE_PATH
vs
LogFilePath=/home/user/logs
When running, the application fails as it cannot expand the Environment Variable.
It will be sourced inside the shell before the application is run.
Thanks!

Recently I used this in a (bash) script:
#!/bin/bash
# ...
source config.file
# ...
Where config.file had lines like this:
export ORIG_PATH="${PATH:-.}:/bla/bla"
export SOMESETTING="${SOMEVAR:-"somedefault"},somedata"
...
So the ${parameter:-word} thing worked well for me: use default values, if parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. (From man bash.)
HTH

Related

Setting up environment variable in linux

While installing UIMA I got this steps in readme file
* Set JAVA_HOME to the directory of your JRE installation you would like to use for UIMA.
* Set UIMA_HOME to the apache-uima directory of your unpacked Apache UIMA distribution
* Append UIMA_HOME/bin to your PATH
* Please run the script UIMA_HOME/bin/adjustExamplePaths.bat (or .sh), to update
paths in the examples based on the actual UIMA_HOME directory path.
This script runs a Java program;
you must either have java in your PATH or set the environment variable JAVA_HOME to a
suitable JRE.
I opened /etc/environment and perfomed this changes:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/UIMA_HOME/bin"
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386"
UIMA_HOME="/root/Desktop/karim/software/UIMA/UIMA_SDK_1.4.5"
after that executed:
UIMA/UIMA_SDK_1.4.5/bin# ./documentAnalyzer.sh
which gave this error:
./documentAnalyzer.sh: 2: .: Can't open /bin/setUimaClassPath.sh
documentAnalyzer.sh code :
#!/bin/sh
. "$UIMA_HOME/bin/setUimaClassPath.sh"
if [ "$JAVA_HOME" = "" ];
then
JAVA_HOME=$UIMA_HOME/java/jre
fi
"$JAVA_HOME/bin/java" -cp "$UIMA_CLASSPATH" -Xms128M -Xmx900M "-Duima.home=$UIMA_HOME" "-Duima.datapath=$UIMA_DATAPATH" -DVNS_HOST=$VNS_HOST -DVNS_PORT=$VNS_PORT "-Djava.util.logging.config.file=$UIMA_HOME/Logger.properties" com.ibm.uima.reference_impl.application.docanalyzer.DocumentAnalyzer
What is the mistake here? I guess I set environment variable correctly
I think the answers given about adding the $ to the variable UIMA_HOME in your PATH variable are correct, but, I think you are also lacking the EXPORT command for your variables.
Look, after you set their values, you should also writhe this in /etc/environment:
export UIMA_HOME
export JAVA_HOME
export PATH
That way, you would be able to use them later (always remember to fix the PATH variable with the $UIMA_HOME as well).
If this does not work, try rebooting your computer after setting the variables as I said.
In the case that does not work either, try repeating the process and in a console (after doing everythin all over again) try using the following command:
source /etc/environment
Fianlly, if that does not work, try setting the variables in the file called /etc/profile (do the same process: setting the varialbes and exporting them), and this should work.
The order of variable assignments in your /etc/environment is wrong; in order to use $UIMA_HOME in the PATH=..., you have to define UIMA_HOME afore, e. g.
UIMA_HOME="/root/Desktop/karim/software/UIMA/UIMA_SDK_1.4.5"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$UIMA_HOME/bin"
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386"

how to set the LUA_PATH and LUA_CPATH for the zerobrane studio in linux

I want to add a module path for all of my project in zerobrane. I add following code into the user.lua.
LUA_PATH=LUA_PATH .. ';mypath' or
package.path=package.path .. ';mypath'
It can't work. how can I do it ?
PS
I don't want to set the package.path at the begin of all the project.
When Lua starts, it initialises package.path and package.cpath with values of LUA_PATH and LUA_CPATH environment variables. Setting up these environment variables will be one clean way to set paths. Appending LUA_PATH's value with a double semi-colon will make Lua append the default path to the specified path.
Using bash on Linux, you can set the paths by adding these lines to the end of ~/.bashrc file. For example:
## final ;; ensure that default path will be appended by Lua
export LUA_PATH="<path-to-add>;;"
export LUA_CPATH="./?.so;/usr/local/lib/lua/5.3/?.so;
/usr/local/share/lua/5.3/?.so;<path-to-add>"
Hope it helps.
You can set LUA_PATH and LUA_CPATH before starting ZeroBrane Studio and it should pass those values to all the projects you run or debug from the IDE.
I add following method into the /opt/zbsstudio/lualibs/mobdebug/mobdebug.lua file.
package.path = package.path .. ';my_path/?/init.lua'
package.cpath = package.cpath .. ';my_path/?.so'
But I'm not sure that's the best way.

Using Linux environmental variables as a path in Perl script

I am trying to use an environmental variable to point to a file, run it through a subroutine and associate it with a variable. I managed it with Windows but I cannot get the syntax working for Linux..
This is what I have:
my $config = read_config("$ENV{APP_HOME}/config/APP-linux.cfg");
my script dies when reaching this line with the error:
Use of uninitialized value $ENV{"APP_HOME"} in concatenation (.) or string at ./XXXXX.pl
APP_HOME is defined as an environment variable (confirmed using set). What am I doing wrong?
In bash, = simply creates a shell variable. These are not automatically exported to the environment. You need to do that explicitly.
Set a shell variable:
$ AA=hello
Set and export another one (in a single statement):
$ export BB=there
Start a new process:
$ bash
Voila! Only the exported variable is inherited by the new process:
$ echo "[$AA] [$BB]"
[] [there]
Note that set does not set a variable. set AA=hello does not do what the Windows shell does.
Okay, the solution was of my own stupidity. I set the variable in .bashrc using:
APP_HOME=$HOME/APP/DATA/STORAGE; export FINE_DIR
The RAPID_DIR had no right to be there. Was a remainder of a copy/paste and poor oversight... Changes FINE_DIR to APP_HOME and all is good.
Thankyou for all the guidance!

Use shell to load in variables to replace placeholders

I have a problem where my config files contents are placed within my deployment script because they get their settings from my setting.sh file. This causes my deployment script to be very large a bloated.
I was wondering if it would be possible in bash to do something like this
setting.sh
USER="Tom"
log.conf
log=/$PLACEHOLDER_USER/full.log
deployment.sh
#!/bin/bash
# Pull in settings file
. ./settings.sh
# Link config to right location
ln -s /home/log.conf /home/logging/log.conf
# Write variables on top of placeholder variables in the file
for $PLACEHOLDER_* in /home/logging/log.conf
do
(Replace $PLACEHOLDER_<VARAIBLE> with $VARIABLE)
done
I want this to work for any variable found in the config file which starts with $placeholder_
This process would allow me to move a generic config file from my repository and then add the proper variables from my setting file on top of the placeholder variables in the config.
I'm stuck on how I can get this to actually work using my deployment.sh.
This small script will read all variable lines from settings.sh and replace the PLACEHOLDER_xxx in file for each. Does this help you?
while IFS== read variable value
do
sed -i "s/\$PLACEHOLDER_$variable/$value/g" file
done < settings.sh
#!/usr/local/env bash
set -x
ln -s /home/log.conf /home/logging/log.conf
while read user
do
usertmp=$(echo "${user}" | sed s'#USER=\"##' \
sed s'#"$##')
user="${usertemp}"
log="${user}"/full.log
done < setting.sh
I don't really understand the rest of what you're trying to do, I will confess, but this will hopefully give you the idea. Use read.

Set Enviroment Variable that contains two paths interelated paths

I am trying to create a custom environment variable that uses python to execute a py file.
Here is an example of what I have
export VAR=${VAR}:"/usr/bin/python2.7 /home/user/file"
When I use the variable I get the output:
bash: :/usr/bin/python2.7: No such file or directory
If I echo the variable I get the output:
/usr/bin/python2.7 /home/user/file
EDIT:
Trying "$VAR" gives me the output
bash: :/usr/bin/python2.7 /home/user/file: No such file or directory
If I run just this /usr/bin/python2.7 /home/user/file it works
I think an alias is more appropriate for all kinds like this (you may consider a more suitable name for the alias)
alias var="/usr/bin/python2.7 /home/user/file"
If you want to stick with your version you have to tell your shell to evaluate the content of VAR.
For this you just have to invoke
eval ${VAR}
By the way, why do you append the string "/usr/bin/python2.7 /home/user/file" to VAR instead of overwriting the content of VAR?

Resources