File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare OS command with String value in Shell Script ?

 
kri shan
Ranch Hand
Posts: 1453
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the first line should be (those are backwards single quotes, or ticks, around pwd):

cudir = `pwd`
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic