LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2010, 06:49 AM   #1
jsullivan44
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
Question Check if the user is in the correct directory before executing bash script.


Hello everyone. I have been searching for 90 minutes for something that I "think" should be fairly easy. I'm pretty new to Bash Scripting so I could be completely wrong. Then again it may be a weird request to even need something like this. But here it is.

I have a script written to convert data from one of our software version to another. The only thing I need to add to it is a "check to make sure the user running the script is in the /tmp directory".

I tried
if [ `pwd` = "/tmp" ]; then
echo "Blah Blah TRUE"
else
echo "Blah blah FALSE"
exit

Maybe this is too basic? Like I said I'm pretty new.
Thanks For Your Help!!!!
 
Old 12-10-2010, 07:04 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well, have you tried that?? Seems strange to have found a solutino and then ask online if it works without apparently trying it yourself...?

Either way, it should be fine. You should use $(pwd) syntax this millennium though, not `pwd`.
 
Old 12-10-2010, 07:24 AM   #3
jsullivan44
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Smile Resolved

Of course I tested it and it did not work. Sorry for any misunderstanding. However I did find the fix I am looking for. Also seems that I had the echo statements backwards.
v
if [ `pwd` != "/tmp" ]; then
echo "Blah Blah FALSE"
else
echo "Blah Blah TRUE"
fi


I'm still open to any input, or alternative methods. But this did fix what I was trying to do.
 
Old 12-10-2010, 07:28 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Well, the test command looks fine here... ahh but you have an "exit" instead of a "fi". In terms of style, assumign that your script is going to quit totally if the wrong directory, then I wouldn't use an else condition. Just exit with an error if in the wrong directory, otherwise keep going. Makes scoping and things easier if you don't have to remember however many lines further down the page you were once in the if statement.

Last edited by acid_kewpie; 12-10-2010 at 07:31 AM.
 
Old 12-10-2010, 07:28 AM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
oops

Last edited by wje_lq; 12-10-2010 at 07:30 AM.
 
Old 12-10-2010, 07:45 AM   #6
jsullivan44
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for help guys.
 
Old 12-10-2010, 07:52 PM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
if [ "`pwd`" = "/tmp" ]; then
echo "Blah Blah TRUE"
else
echo "Blah blah FALSE"
exit

or

if [ X`pwd`X = X/tmpX ]; then
echo "Blah Blah TRUE"
else
echo "Blah blah FALSE"
exit

I think in your test condition, it would always be false. In the first case, you wanted the true and so it failed. In the second case, not /tmp is false and so it ran the else portion as you intended.

Please try it from some other directory.
 
Old 12-10-2010, 08:27 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You might prefer to use $( pwd -P ) if you want to know exactly which directory is current and you almost certainly want to use [[ <test expression> ]] rather than [ <test expression> ] for reasons explained on Greg's WIKI.

EDIT: the change from [ to [[ avoids having to use either of the techniques shown by AnanthaP.

Last edited by catkin; 12-10-2010 at 08:29 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 03:06 AM
bash script to check how many files in directory guest Programming 8 01-31-2009 11:55 PM
super user privileges check for a normal user in bash script freeindy Programming 2 08-01-2008 06:08 AM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 04:03 PM
executing a script - check.sh wfstanle Linux - Newbie 4 12-16-2005 08:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration