Originally Posted by Dru-Jitsu
God I suck at scripting, please help!
Create a shell script called newest.bash that takes two filenames as input arguments ($1 and $2) and prints out the name of the newest file (i.e. the file with the newest last modified date).
I know this seems easy to all you guys that script a lot on linux... but I don't. I don't even know where to start I have watched all my teachers videos and been reading and I am confused how to use the mtime or whatever you have to use for this. I am assuming I should use an if statement since it's between 2 filenames. Again I am not expecting straight code for the whole thing since I am doing homework but I severely need help I have to write 8 of these before work.
27 posts and about 25 seem like homework! And by the 27th you are still getting stuck at the 'I don't know how to start' stage. OK, let's see if an alternative approach helps.
There are a few guides to scripting online: 'The Advanced bash Scripting Guide' comes to mind, and I'm sure that there is other stuff on TLDP. Or search 'tutorial' and 'bash' or 'shell scripting'(good script samples are available at 'bash cures cancer' and 'bash hackers').
Now you've got some reference materials, you problem is to compare dates on two files. On Linux/Unix there is or can be more than one date attached to a file (mtime, ctime,...). You will have to:
- decide which of the above that you want for your requirements
- make it a command-line option and the script decides which to use (-m for mtime, -c for ctime, for instance)
you might want to think of the second of those as the advanced option...
Your choice now is
- can I get something that just gives me the information that I want (see, for example an earlier post, and use the man command, or even man -k)
- alternatively is there something that gives me way too much information and filter it down with eg awk (or something else) to just get the information that you want
Having done that, you've got two dates (actually, you might object that you've got one date; if so, do it again so that you've got two). Now you have got to compare those two to find the newest. I haven't actually done this, but you should be able to give it a few tries and get something that gives you the correct result. you may, initially, want to separate this part of the project from the rest (ie, write a script fragment that you think will be useful and feed in data manually to test it out). Err, and what happens if the times are the same??? Does your script crash, or do something sensible?
I am assuming I should use an if statement...
Well, that certainly seems to be a workable way forward...Something that works like:
if time_1 < time_2 then
would be useful, but you could probably find other ways to do it to (select/case for example) but for an example like this it isn't clear that anything else offers any obvious advantages over a simple if, although they might do in a more elaborate comparison.
Now, let's see what you've got, so far...