Using Linux environmental variables as a path in Perl script - linux

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!

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.

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?

Setting environment variable with leading digit in bash

I need to set an environment variable called "64bit" (i.e. with a leading digit) in bash. However, bash variable names disallow a variable with a leading digit. I know a way to set it when invoking bash:
env 64bit=1 /usr/bin/bash
However, I'm looking for a way to change it in the currently running shell i.e. not by starting a new shell. I also know that csh allows variables to start with a digit, but I need to use bash.
Is there any way to achieve this?
You can also bypass the bash interpreter and define the variable directly with the bash internal functions:
$ gdb --batch-silent -ex "attach $$" \
-ex 'set bind_variable("64bit", "1", 0)' \
-ex 'set *(int*)(find_variable("64bit")+sizeof(char*)*5) = 1' \
-ex 'set array_needs_making = 1'
$ env | grep 64
64bit=1
As people point out, Bash does not allow variables starting with digits. It does however pass on unrecognized environment string to external programs, which is why the variable shows up in env but not in set.
As a workaround, you can work with a valid name like _64bit and then automatically inject your invalid variable name into commands you run:
#!/bin/bash
# Setup for injection hack
original=$PATH
PATH="/"
command_not_found_handle() {
PATH="$original" env "64bit=$_64bit" "$#"
}
# Your script and logic
_64bit="some dynamic value"
# This verifies that '64bit' is automatically set
env | grep ^64bit
Note that this particular method only works if you invoke through $PATH, not if you use relative or absolute path names.
If you do invoke by pathname, consider modifying PATH and invoking by name instead.

Read / Run Environment Variables in a text file

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

Resources