This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

compare OS command with String value in Shell Script ?  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • 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: 5856
7
Android Eclipse IDE Ubuntu
  • 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
Posts: 12143
255
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!