I used to use Debian's alternatives system to set 'global env' like java, javac, javap but I've read about the disadvantages.
So I added
export JAVA_HOME=/opt/jdk/java
export PATH=$JAVA_HOME/bin:$PATH
to my ~/.bashrc and when I open my terminal I can use the commands as expected but my most of my shell scripts doesn't work anymore.
As you can see in the picture below they check if $JAVA_HOME exists and executes the following command which does nothing. When I enter $JAVA_HOME/bin/java -version it works correctly. If I start the script in a terminal it works, too.
So it seems that #!/bin/sh doesn't source .bashrc? Changing it to #!/bin/bash doesn't solve the problem.
I tried to add the export commands to /etc/profile but this doesn't seem to get sourced at startup/login.
Does anyone have an idea or keywords? I think the solution is quite simple but at the moment I'm stuck.
Thank you in advance!
UPDATE:
Starting the script in the bash terminal with ./something.sh works fine. Right click and execute or 'Open with bash' (XFCE4 context menu) does nothing.
Bash loads .bashrc only when the shell is interactive and non-login. In your case the shell is not interactive, so .bashrc is not loaded.
.bashrc contains a check that prevents it from executing if the shell is not interactive. Usually the first thing .bashrc does is:
case $- in
*i*) ;;
*) return;;
esac
This will prevent you from calling source .bashrc from your script.
The script should inherit from your parent shell, so you should be getting all variable that have been exported before you ran the script.
Also, the preferred shebang is #!/usr/bin/env bash which is more portable.
So in your case:
Open a terminal window. That will load .bashrc, but just to make sure, run . .bashrc and then echo $JAVA_HOME to verify that the variable was set correctly.
Then your script will simply be:
#!/usr/bin/env bash
java -jar <whatever>
If you have some other script related variable that you want to set, you can do that by sourcing a "settings" script:
#!/usr/bin/env bash
source ~/settings.sh
echo $SOME_VAR_SET_IN_SETTINGS_SH
Adding additional folders to your PATH variable in XFCE4 with LightDM will not work as expected! Shell scripts that use these additional commands and are started from the graphical environment will fail because the can't find the command. Logging the PATH variable while starting one of these scripts shows that the PATH variable is overwritten with the very default one. Why? Because LightDM hardcodes the PATH variable and overwrites it for the graphical environment. Screw you! Look here!
Source: https://ljwo.wordpress.com/2014/02/02/global-path-in-debian-wheezy-xfce/
Disable it or use another DM.
Related
I have made a calculator CLI calculator app in Java that I would like to use. Since I am actually going to use it quite often, I do not like the idea of typing the long path all the time.
I have read an article on StackoverFlow that you can place a .sh command into the /usr/bin folder, but after the El Capitan update, all System folders are locked even for the root user. So, again, I did some more research, and I found that /usr/local/bin folder is specifically made for "home-made" commands for the terminal. I have made a calculator.sh file with the following code:
#!/bin/sh
-jar /Users/mac/Desktop/Данила/my_apps/calculator.jar "$*"
The article said that I should place it in the /usr/bin folder, but because it is locked, I placed it in the /usr/local/bin, thinking that it is practically the same thing, and that it should work. Of course after I placed it in there and tried to run "calculator" command in the terminal, it did not work at all. I figured that it might need a "chmod" command to make it work. So I used chmod +x /path, but afterwards it still would not work. Right now I am stumped, so any help would be welcome.
First, is /usr/local/bin on your path? echo $PATH to find out.
Second, are you sure you pasted the above script properly?
It seems to be missing java on the command line.
I would expect it to look something like:
#!/bin/sh
java -jar /Users/mac/Desktop/Данила/my_apps/calculator.jar "$*"
Also, since you named it calculator.sh, you have to run it using that name.
The .sh extension isn't needed, so you could just call it calculator.
To this I recommend you create an alias.
alias test="java -jar /full/path/to/jar.jar"
And "test" is a command now, if you like get this command allways add it into .bash_profile.
This command works great from terminal $java -jar $picard
Whenever I put that in bash script, It gives error as: line 2: -jar: command not found
#!/bin/sh
$java -jar $picard`
Is there any fix, thanks?
The $ at the beginning of the line is not part of the command; it is part of the shell prompt.
When you put the command in a batch file, you should not include the shell prompt. So change it to:
#!/bin/sh
java -jar $picard
EDIT
OP mentioned that "$java" points to the actual Java binary.
If you are following naming conventions for shell scripts then $java and $picard are local variables in your shell, not environment variables, so they don't get passed onto any commands that you invoke.
To turn them into environment variables, you need to export them from your shell. Whereever you set values in them is the best place to put:
export java
export picard
However, this turns them into environment variables, and in that case you should make the names "all capitals" -> JAVA, PICARD.
You have to export the java variable which you are using to point to java path as below
JAVA_CLASSPATH=/bin/jre1.8.0_101/bin/java
export PICARD_CLASSPATH=/bin/picard-tools-2.5.0/picard.jar
$JAVA_CLASSPATH -classpath $PICARD_CLASSPATH {Nmae of the Class from where the execution begins}
Instead of using small case you can use capital letter so it will be more readable.
Environment: mac
Precondition: I already configured 'adb' full path to my bash_profile. and when I tried type 'adb' in my terminal, it is working.
But, I tried to exec 'adb' command from java, 'adb' is not working, instead I need to pass the full adb path to make it work.
I guess this is probably something to do with the bash_profile setting, anyone know the exact reason for this issue?
Runtime.getRuntime().exec() runs /bin/sh -c <command>. If this is or points to a bash shell on your system: A non-interactive bash does not read .bash_profile unless explicitly (--login) told to do so.
From the documentation:
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
It's a little convoluted, but non-interactive, non-login bash instances don't read the profile files.
Your path settings does not get picked up by the subshell (which is actually /bin/sh which might not even be bash at all).
If you want, you can add the path to adb system wide by adding an appropriate entry to to /etc/paths.d.
I'm doing a year in computer science at my university and it turns out there are very few computers that have the jdk installed on them. I can run eclipse portable from my memory stick, so compiling java should be possible.
What I was wanting was a batch file that set up the environment variables on the system so that I am able to run the portable version of the jdk from my memory stick. I don't appear to be able to alter the system variables but can change the user ones.
So far I have the below code as a batch file, though it still leaves me with a javac not recognize message when run. As a final note I should point out I don't have a great deal of experience with batch files.
echo %path% > stored_path.txt
setx PATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%PATH%"
setx JAVA_HOME "%cd%PortableApps\CommonFiles\OpenJDK"
setx CLASSPATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%CLASSPATH%"
Thanks for any help you can give me.
Update 04/10/13 - 16:54:
Just to clarify Eclipse itself works fine and is fully usable. However I have a jdk saved on the memory stick that works as if I call javac from a console that is open to that directory it works fine. What I want to know is if there is a way for windows to recognise this command from any directory on the system by just changing the user environment variables.
A thought that just occurred to me (I can't test it right now), is that would making a shortcut to the javac exe file in another folder work?
No need to actually set the environment variables permanently!
Just set them for the current session of the program. This is the batch script I made to launch my eclipse setup.
#echo off
setlocal
pushd "%~dp0"
if not exist eclipse.exe exit /b 1
if not exist jdk1.6.0_45\ exit /b 2
if not exist apache-maven-2.2.1\ exit /b 3
set "JAVA_HOME=%~dp0\jdk1.6.0_45"
set "MAVEN=%~dp0\apache-maven-2.2.1"
set "M2_HOME=%MAVEN%"
set "MAVEN_OPTS=-Xms512m -Xmx1024m"
set "CLASSPATH=.;%JAVA_HOME%\jre\lib\ext;%CLASSPATH%"
set "PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%MAVEN%\bin;%PATH%"
rem M2_REPO
mklink /d jre "jdk1.6.0_45\jre"
start eclipse.exe -vm "%JAVA_HOME%\bin\javaw.exe" -data ".\workspace"
rem cmd /k
popd
endlocal
exit /b 0
This sets all that I need and starts eclipse with these settings without having to modify the variables.
My folder structure:
eclipse\
jdk1.6.0_45\
apache-maven-2.2.1\
workspace\
eclipse.exe
all other eclipse folders and files.
Our school-computers didn't have git installed, so i put the portable version of msysgit on my USB-Stick. That gives you (aside from git) a portable MSYS, which is a bourne shell with most of the standard linux tools (like ls and df).
So, i used the shell, instead of the windows Command Line. Then, for the shell, I created a small bash-script to set up the environment-path's. From the gist:
To make the script ("env_setup.sh") execute every time the git-shell
is started, you'll want to put the script into the "etc/"-directory in
your msysgit-folder.
After that, you'll need to edit the "profile"-file, which is executed
by the shell every time it's launched.
Add the following line at
the end of the file:
# Setup the environment-variables
. /etc/env_setup.sh
This is the script, which sets up the environment variables:
#!/bin/bash
# Resources:
# http://stackoverflow.com/questions/623518
# http://stackoverflow.com/questions/59895
# http://markashleybell.com/articles/portable-git-windows-setting-home-environment-variable
# Get the directory this script is in:
DIR=$(cd $(dirname "$0"); pwd)
# Get only the Flash-Drive letter (e.g. "F")
FLASH=${DIR:1:1}
echo "We determined your USB-drive to be on $FLASH:\\"
# Set the home-path to the Flash-drive to get portable SSH-keys:
# --- You'll want to change this to your prefered Home-directory!
export HOME=/$FLASH/PortableApps/git/home/luke
echo "I set your Home-directory to '$HOME'";
### --- EXAMPLES ---
# Set the Java-Home variable to a JDK on USB-Stick:
export JAVA_HOME=/$FLASH/JDK1.6
# Add the Java-Tools to the JDK-folder:
export PATH=$PATH:/$FLASH/JDK1.6/bin
# Add a Maven from your USB-drive to the PATH:
export PATH=$PATH:/$FLASH/PortableApps/apache-maven/bin
# Add Node.js from the local pc to your PATH:
export PATH=$PATH:/c/Programms/nodejs
Here, you can see a few examples, including a JDK I had on the same USB-Stick. It is added to the PATH, so you can write javac everywhere you like and always use the JDK specified.
Note that this is only for one session and that it only works in the MSYS shell!
I am using c-shell and I am used to using setenv. I need to execute an equivalent command from within a python script. I tried using os.environ['JAVA_HOME'] = "/usr/local/java" which works from the python interpreter, but when my script is executed from the command line, the shell it ran in does reflect the newly set environment variable. Can anybody help, I am new to scripting, I hope I made my question clear.
If you're using subprocess.Popen, it should be enough to pass the env parameter to the constructor to whatever you need as a dictionary (you can copy the contents of os.environ and add your own environment variables if you wish).
As explained in How to use export with Python on Linux, setting an environment variable within any process (such as your Python script) cannot affect any parent processes (such as the csh process from which you execute the Python script).
What you can do is have your Python script print a setenv command, and then evaluate the output in your shell as a command.
For example:
csh% cat foo.py
#!/usr/bin/python
import os;
os.environ["JAVA_HOME"] = "/usr/local/java"
print "setenv JAVA_HOME", os.environ["JAVA_HOME"]
csh% ./foo.py
setenv JAVA_HOME /usr/local/java
csh% echo $JAVA_HOME
JAVA_HOME: Undefined variable.
csh% eval `./foo.py`
csh% echo $JAVA_HOME
/usr/local/java
csh%
And you can set up an alias in your ~/.cshrc to do the eval `...`, or just invoke it directly from your .cshrc or .login (depending on just what you're trying to accomplish).
I know it is been a while sins this issue, but I solved it a little different.
Maybe this ain't even the most beautiful solution, but it works.
I created a shell script called env.sh
#!/bin/tcsh
eval $*
Then in my python script called subprocess.
output = subprocess.Popen(["env.sh", "setenv", "DISPLAY", "remhost:0"], stdout = subprocess.PIPE).communicate()[0].split()
This works for me, don't forget to make the env.sh executable by executing "chmod +x env.sh"