Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

edit CLASSPATH in .bashrc from schell script

Posted on 2006-11-02
5
Medium Priority
?
1,058 Views
Last Modified: 2013-12-26
I'm writing a shell script in which I want to append variable to my classpath in my .bashrc file.  Is there an easy way to do this?
0
Comment
Question by:ICPooreman
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 2000 total points
ID: 17859913
If you mean you want the classpath ammended for the lifetime of the script itself, simple:

#!/bin/bash
export CLASSPATH=$CLASSPATH:my.jar
echo classpath is changed to $CLASSPATH

However, if you want to run a script, and after running the script is complete to have the classpath appended, this is less simple...
with the same script example above (saved as eg.sh)

if you ran
./eg.sh
echo classpath is $CLASSPATH

afterwards the CLASSPATH env var. is the same as before (since the environment is only set in current shell, and script runs as its own shell).

instead, "source" the script, rather than running it:

. ./eg.sh

This will execute the "export" in the same shell as it is sourced, hence changing it in the current env.

Hope this helps.
C.
0
 

Author Comment

by:ICPooreman
ID: 17860090
The first part changes the variable in my script but my commands in the script later are still using the original set in the .bashrc file.  If I do an echo on CLASSPATH the value will have changed however, it doesn't appear to be effecting the later commands in the script.

I'm unfamiliar with what the second option is supposed to be doing but it didn't work either.  It closed down my shell and nothing happened when I ran it.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17860126
Do you have the "export" in place? without the "export" settings to the variable are only for the shell itself, not children - the export makes the change for the current shell's children processes (i.e. the "java" command).

The second approach would exit the command shell if there's an exit...

you can also specify classpath variables for Java, e.g.

#!/bin/bash

export MY_CLASSPATH=$CLASSPATH:my.jar

java -cp $MY_CLASSPATH my.org.HelloWorld

Cheers,
C.

0
 

Author Comment

by:ICPooreman
ID: 17860534
You're right it was a typo on my side, Sorry thanks for the help
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17860564
Glad to be of help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question