Check if the user is in the correct directory before executing bash script.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!!!!
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.