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.
If you don't know up front where the files are then you need to include the search for them in your script, for example with find. You could ask (echo) in your script to provide full path and file when executing, or ask for just a file name and run find from within your script. The first thing you'll need to check in order to avoid loosing time is if the file exists (with or without the mentioning of a path). If it doesn't, give an output to the user that the file doesn't exist. Find in its simplest form is used like this:
Code:
find / <filename>
(you can substitute / for any search path if you want to limit your search to a certain directory but since you mentioned the whole disk I left it as such)
Of course you'd also need to make sure that you're getting the correct file if duplicates are found.
In case your question is addressed to me, I'm providing a two way solution. If the 'enduser' knows the full path to both files then it would work without a search and for sure it would significantly reduce execution time. If the enduser doesn't know then a find can search the entire harddisk.
I still don't like the idea of the script finding the files. I would even consider it a bad practice (maybe except fore some special cases where it may be useful). I, personally, would be afraid to use a script that would search files anywhere else than in the path I specify. I would think that it is up to the enduser to find the files to compare. As this is your homework, your teacher will certainly use valid paths/filenames with your script (except to test error handling), so you are not required to make your script search for them unless the assignment explicitly states otherwise.
In post #30 the OP stated that he needs the script to search on the whole disk. Why would it be bad practice in a classroom environment to use a script to do that? Provided the permissions are set up correctly there is little harm that can be done, assuming the script will not run as root. As you stated yourself, it's homework so everything should be explore-able at any level in order to learn / teach something. After all it's not a production environment and up to the teacher to show the students the possible dangers. Where it to be used in a production environment, you'd be very right.
In post #30 the OP stated that he needs the script to search on the whole disk. Why would it be bad practice in a classroom environment to use a script to do that? Provided the permissions are set up correctly there is little harm that can be done, assuming the script will not run as root. As you stated yourself, it's homework so everything should be explore-able at any level in order to learn / teach something. After all it's not a production environment and up to the teacher to show the students the possible dangers. Where it to be used in a production environment, you'd be very right.
Kind regards,
Eric
Well in a classroom environment it does not hurt. The OP said he needs to search the whole hdd "because I have to show this script not on my computer", not "because the teacher said so". It made an impression to me that the OP is worried about the files being in different place on the teacher's computer and teacher not knowing how to use the script properly or not supplying valid paths to the files. So I only wanted to say that he doesn't have to worry about that and make the script search the hdd just in case the teacher does not know where the files are, unless the searching part is the point of the homework and specified in the assignment
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.