niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Change Environment var and export result only if value contains no "xxxx"?
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Kevin Rodgers
*nix forums beginner


Joined: 01 Mar 2005
Posts: 29

PostPosted: Fri Feb 18, 2005 4:03 pm    Post subject: Re: Change Environment var and export result only if value contains no "xxxx"? Reply with quote

Matthias Zach wrote:
Quote:
I could imagine a way similar to

if [ set | grep CLASSPATH | grep "/aaa/bbb/ccc.jar" = "" ] then
export CLASSPATH=/aaa/bbb/ccc.jar:$CLASSPATH
fi

This code above yields a "syntax error at line 234 : 'fi' unexpected.
Why?


Because the then keyword needs to be on a separater line, or preceded by
a semicolon.

And because the [ ... ] test command expects a single word on each side
of the = operator. You want to compare the output of the set | grep |
grep pipeline to the empty string, so it needs to be enclosed in the
command substitution delimiters.

So:

if [ `set | grep CLASSPATH | grep "/aaa/bbb/ccc.jar"` = "" ]; then
blah blah blah
fi

Quote:
However, even if it would work I feel that there is a shorter solution.
Is there ?

See John L's followup.

--
Kevin Rodgers
Back to top
John L
*nix forums Guru Wannabe


Joined: 17 Mar 2005
Posts: 191

PostPosted: Thu Feb 17, 2005 8:45 pm    Post subject: Re: Change Environment var and export result only if value contains no "xxxx"? Reply with quote

"Matthias Zach" <zach228@fortuencity.com> wrote in message news:cv32c1$jbp$05$1@news.t-online.com...
Quote:
I want to add a new path /aaa/bbb/ccc,jar to the environment variable CLASSPATH
if and only if it does not contain this jar file yet.
If I have to prepend it it should be exported.
How do I do this the easiest way?

I could imagine a way similar to

if [ set | grep CLASSPATH | grep "/aaa/bbb/ccc.jar" = "" ] then
export CLASSPATH=/aaa/bbb/ccc.jar:$CLASSPATH
fi

This code above yields a "syntax error at line 234 : 'fi' unexpected. Why?

However, even if it would work I feel that there is a shorter solution.
Is there ?


case "$CLASSPATH" in
*/aaa/bbb/ccc.jar*) ;;
*) export CLASSPATH="/aaa/bbb/ccc.jar:$CLASSPATH" ;;
esac

--
John.
Back to top
Matthias Zach
*nix forums beginner


Joined: 17 Mar 2005
Posts: 4

PostPosted: Thu Feb 17, 2005 8:29 pm    Post subject: Change Environment var and export result only if value contains no "xxxx"? Reply with quote

I want to add a new path /aaa/bbb/ccc,jar to the environment variable CLASSPATH
if and only if it does not contain this jar file yet.
If I have to prepend it it should be exported.
How do I do this the easiest way?

I could imagine a way similar to

if [ set | grep CLASSPATH | grep "/aaa/bbb/ccc.jar" = "" ] then
export CLASSPATH=/aaa/bbb/ccc.jar:$CLASSPATH
fi

This code above yields a "syntax error at line 234 : 'fi' unexpected. Why?

However, even if it would work I feel that there is a shorter solution.
Is there ?

Peter
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 12:03 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts User Environment - export PATH variable paalepu AIX 0 Tue Sep 12, 2006 8:12 pm
No new posts Creating relational view for an ODBC result set? antilog@gmail.com Server 0 Fri Jul 21, 2006 5:56 am
No new posts Removing db from environment mike.klaas@gmail.com Berkeley DB 0 Fri Jul 21, 2006 5:14 am
No new posts change default install directory when using bdist_rpm krithika.sridhar@gmail.co python 1 Fri Jul 21, 2006 4:09 am
No new posts syslog Output Change Maja AIX 0 Thu Jul 20, 2006 8:41 pm

Debt Consolidation | McDonalds | Debt Consolidation | Credit Cards | Problem Mortgage
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1204s ][ Queries: 20 (0.0451s) ][ GZIP on - Debug on ]