How to get path and check in if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get path and check in if statement
# 1  
Old 04-08-2008
How to get path and check in if statement

Hi,

I was wondering if it possible to get the path of a variable and compare that to something. Basically I want to write a script that checks if my $JAVA_HOME is correct and if not then it sets it. So far I have...

Code:
if [[$JAVA_HOME != '/pathhere']]
then
export JAVA_HOME='/pathhere'
echo JAVA_HOME='/pathhere'
fi

Problem is that the script can't seem to understand that I want to compare the path $JAVA_HOME and not what's in there. Anyone know how to just extract the path from that already set variable? Thanks!
# 2  
Old 04-08-2008
Your question doesn't make sense. Your code already compares the value of $JAVA_HOME to the string /pathhere; how this is or isn't "what's in there" is not quite clear.

Depending on your shell, you might need to add spaces inside the [[ ... ]] delimiters in order for the syntax to validate. Is that your problem?

Speculatively, if you want to check if a colon-delimited path contains /pathhere anywhere in it, and if not, add it, try this:

Code:
case :$JAVA_HOME: in *:/pathhere:*) ;; *) JAVA_HOME=/pathhere:"$JAVA_HOME";; esac

# 3  
Old 04-08-2008
well reason why im confused is that when i tried to run the script it errors in the if condition. it gives me this...

/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home: No such file or directory

where that path is the $JAVA_HOME. It shouldn't matter if that file or directory exist right, I just want to check that value which is the problem
# 4  
Old 04-08-2008
Try adding those spaces, and also quote the variable. (case is less picky about quoting and works even in good ole /bin/sh which doesn't have this fancy [[ ... ]] stuff.)

Code:
case $JAVA_HOME in /pathhere) ;; JAVA_HOME=/pathhere;; esac

# 5  
Old 04-08-2008
ok cool I tried the space thing and it worked! Thanks!

How exactly does case work? Is it like if the first element is a part of the second element then do then else this? Sorta like that?
# 6  
Old 04-08-2008
No, it's basically a generalization of if then else if then else if then else, but all branches examine the same variable. In many programming languages, this is a "switch" statement (think railroad switch). Read the documentation for your shell.
# 7  
Old 04-08-2008
Quote:
Originally Posted by eltinator
Hi,

I was wondering if it possible to get the path of a variable and compare that to something. Basically I want to write a script that checks if my $JAVA_HOME is correct and if not then it sets it. So far I have...

Code:
if [[$JAVA_HOME != '/pathhere']]
then
export JAVA_HOME='/pathhere'
echo JAVA_HOME='/pathhere'
fi

Problem is that the script can't seem to understand that I want to compare the path $JAVA_HOME and not what's in there. Anyone know how to just extract the path from that already set variable? Thanks!

Code:
test -e $JAVA_HOME

or use if statement.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to escape the @ character in an if statement check?

Hi, I am running a shell script that has to verify a password with an if statement. The password has an @ in it. I am having problems getting the if statement to test for the password. The @ causes problem. I tried the standard backslash escape but it did not work for me. How can I do it? I... (2 Replies)
Discussion started by: mojoman
2 Replies

2. UNIX for Beginners Questions & Answers

If statement to check file transfer

Hello Guys, I am trying scp few file within if statement, but getting error, can someone please help to understand, what mistake I am making ? if && ] ; then echo " Files transferred to Log servers successfully. " else echo " One or more file transfer failed over... (10 Replies)
Discussion started by: UnknownGuy
10 Replies

3. Shell Programming and Scripting

Parsing a PATH statement

I have a script that will be placing a trigger file for other applications. The user-inputted path is similar to: "/data/region/NorthAm/Project HAV 8H" The project path will not change throughout the script. However, pwd changes as the scanning continues in the script. I need to truncate... (3 Replies)
Discussion started by: leepet
3 Replies

4. Shell Programming and Scripting

IF statement to check file exists

Hi All, If i run below copy command, it works absolutely fine, /opt/csw/bin/scp axetlxyz01:/opt/data/test/QURIES* ./input I want to make the above line better, by adding an IF statement, want to check if there is any file exists with name QURIES*.* then i need to copy that. if ... (7 Replies)
Discussion started by: rkrgarlapati
7 Replies

5. Shell Programming and Scripting

using if statement to check file size

Hi, Am trying to execute certain commands if the condition satisfied, but i feel i am making some mistakes in the usage of if statement here is the code #!/bin/ksh SIZE=$(ls -ltr /aemu/ws/DN.txt | tr -s ' ' | cut -d ' ' -f 5) filename=`TZ=CST+24 date +%Y%m%d` ZERO=0 if then cp... (5 Replies)
Discussion started by: aemunathan
5 Replies

6. Shell Programming and Scripting

check file is there not in linux using if statement

How to check file is there not in linux using if statement ? Please do put one example if possible. (4 Replies)
Discussion started by: nskbalu
4 Replies

7. Shell Programming and Scripting

How to check if a file exists using the if statement

Hi, I'm trying to write a bit of code that will check if a file exists and then archives the file Im trying to use the following if statement without success.. if then mv filename archive/filename else echo "no filename exists" fi Should the file name be... (3 Replies)
Discussion started by: Jazmania
3 Replies

8. Shell Programming and Scripting

check if specified path is in $PATH

echo $PATH | grep "\/usr\/ucb" is not working using sh-posix The problem is very simle. I want to check '/usr/ucb' is in the PATH environment variable. If i simply grep '/usr/ucb' i might got wrong result eg '/usr/ucb/bin'. After the path an end of line or colon character should be. In... (4 Replies)
Discussion started by: fpeter75
4 Replies

9. UNIX for Advanced & Expert Users

how to check the actual path instead of link path

Hi I have a path link /test/df/link1/actual/file1 here link1 is actually a softlink link1= a/b i need to print the ACTUAL FULL path instead of a linked path is there any direct command to print the actual path of any linked path eg showPhyscialPath /test/df/link1/actual/file1 and it... (4 Replies)
Discussion started by: reldb
4 Replies

10. UNIX for Dummies Questions & Answers

check a statement -- yes/no

this statement "Be sure to have /usr/bin before /usr/local/bin in your $PATH" is because, if u have any files in bin directory, /usr/local/bin will be the first place to look at instead of /usr/bin. BUT, /usr/local/bin has one more descending directory to go , therefore /usr/bin has to come before... (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question