aspose file tools*
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 Spring in Action this week in the Spring 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: 1378
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: 5836
    
    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: 11477
    
  94

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
 
jQuery in Action, 2nd edition
 
subject: compare OS command with String value in Shell Script ?