Solved

edit CLASSPATH in .bashrc from schell script

Posted on 2006-11-02
5
1,030 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're right it was a typo on my side, Sorry thanks for the help
0
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
Glad to be of help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now