Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
And can you please be a bit more specific? I don't
understand what you're trying to achieve, if you know
its location and its name, what is there to search for?
[edit]
Btw, next time choose a more sensible thread title,
please, one that gives people a clue as to what your
problem might be.
[/edit]
I want to accept a directory name from the user.Then i want to search for the directory starting from the root and check if the directory exits or not. Then i want to ask the user to enter a file name and then search for the same in the specified directory only. How can i do this using shell programming.
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282
Rep:
You have all the information you need to do that in those man pages, except for how to read input from a keyboard in shell scripts. Now you have that, it's just a question of putting the commands together to make a shell script.
If you run into any specific problems while doing this, that you can't solve using Google or by re-reading the man pages, then post them here by all means. However, I've never liked the policy of giving people scripts and saying "this should work". Give a man a fish... It's much better to learn by doing things yourself - or even better, trying and failing, then having someone help you over a specific hurdle.
the first [ "$FOUND_DIR" ] tests if there actually was a matching directory and only tries to search for files if there was. In it's most simple form your script would look something like:
The above script will take the name of the directory to be searched as it's first parameter and the name of the file as the Read could be used to make the script to ask for these instead. Also note that that first find statement might in some cases return several matches which you will have to deal with using a loop. Which would make it something like:
#!/bin/bash
echo -n "Directory to search? "
read SEARCHDIR
echo -n "File to search within $SEARCHDIR? "
read SEARCHFILE
FOUND_DIR="`find / -name $SEARCHDIR -type d`"
if [ "$FOUND_DIR" ]; then
IFS="\n"
for EACH in "$FOUND_DIR"; do
find $EACH -name $SEARCHFILE
done
unset IFS
fi
Didn't test them, so there might be some glitches, but that's the basic idea.
However putting them togehter is the tricky bit...
Exactly, and in cases such as this where the user is hesitant to give out information as to why they want to do it and the question is very clear cut, again like this one, chances are it could be school work or the like. Now you see why DTsazza, IBall and myself have given out pretty vague answers. Never fear, hopefully if the OP passes they'll come back and let you know
I still see no problem in sharing information be it a school project or not. In my opinion it doesn't make a lot of sense to refrain from answering or get very vague for the fear that the information might used in a way that somebody might dissaprove of. That way we could answer no questions at all, as we can never be quite sure what's the situation behind the question. Furthermore, I don't even think it's any of our business.
The way I see it, we're not here to guard people and their uses of information, but to help them out when a need arises. Information wants to be free
Manpages don't tell you how to connect UNIX commands together, that's their weakness. (S)he can learn it from the script example above, and if (s)he doesn't care, then what difference does it make anyway? I don't think people can be forced to learn.
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282
Rep:
Oh no, I never meant to guard information - I just think too many people are ready to say, in essence, "here's what I want to do - now do it for me" - and I've seen in certain forum threads (just from Googling, not here) the person who supplies the answer making two or three revisions, based on feedback from the OP. That's just free software consulting, not help. And while it's fine if the responder's willing to do that, I don't think it's that good for the OP - even if they try to do it themselves and fail, they can at least grasp the concepts involved.
To put it more concisely, I'm against spoon-feeding and I won't do it, on the basis that it makes societies as a whole weaker.
Originally posted by alienDog I still see no problem in sharing information be it a school project or not. In my opinion it doesn't make a lot of sense to refrain from answering or get very vague for the fear that the information might used in a way that somebody might dissaprove of. That way we could answer no questions at all, as we can never be quite sure what's the situation behind the question. Furthermore, I don't even think it's any of our business.
The way I see it, we're not here to guard people and their uses of information, but to help them out when a need arises. Information wants to be free ;)
Manpages don't tell you how to connect UNIX commands together, that's their weakness. (S)he can learn it from the script example above, and if (s)he doesn't care, then what difference does it make anyway? I don't think people can be forced to learn.
Give the man a fish, and he'll come back the next day for more.
Teach him to fish, and he'll be out there drinking beer all day. :D
And yes, I'm totally with Dtsazza on this one, help needs to be
help towards self-improvement, to widen ones scope, to learn how to
do things for oneself.
Pointing someone at the appropriate information is the BEST thing
one can do for them.
There are significantly differing opinions on the subject of build strong societies. Maybe we won't get into that right now. It might be slightly off topic ;D I do see your point however.
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282
Rep:
Heh, yes, I do tend to over-justify things and make them all grandiose. I think we'd best not get into that now - and I harbour no ill will to people with different opinions, so we can just agree to disagree.
If the user is entering a directory name you can use the shell's test (or [ ) command to determine its existence (man sh or bash for details). If the directory exists, cd into it and use the test for existence of the file the same way (different option, of course, but the man page will tell you all you need to know (and much more).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.