It's not a secret anymore!*
The moose likes Linux / UNIX and the fly likes compare OS command with String value in Shell Script ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "compare OS command with String value in Shell Script ?" Watch "compare OS command with String value in Shell Script ?" New topic
Author

compare OS command with String value in Shell Script ?

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
How to compare OS command pwd with String value in Shell Script ?

cudir=pwd

if [ "$cudir" == "etc" ]; then
echo "You are in etc"
else
echo "some other"
fi
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

I think the first line should be (those are backwards single quotes, or ticks, around pwd):

cudir = `pwd`

JBoss In Action
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

And remove the spaces around that first equal sign:

cudir=`pwd`

A more readable solution (and one that allows greater flexibility) is to use the alternate construct:

cudir=$(pwd)

Remember that this will give the path from the root directory. Therefore your if statement will also need to refer to the full directory:

if [ "$cudir" == "/etc" ];then

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare OS command with String value in Shell Script ?
 
Similar Threads
split function problem in AWK
Echo value capture in .sh script
how to set classpath
Maximum length of Command Line Argument
system.exit