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. |
Well one suggestion can be to have a look at options to the ls command.
Do a man ls and check out the following: -l -t --time= |
That's right, we cannot do homework for you. The only way is to read, try, re-try and be patient. You have to write a script in bash, so that you can use the advanced features of some commands. Basically you have to know how to manage positional parameters (a.k.a. arguments passed to the script through the command line), how to test for the newest file between two and the syntax of the if/then statement.
Please, start reading the following: http://www.tldp.org/LDP/Bash-Beginne...#sect_03_02_05 (below the table there is a simple and good example about usage of positional parameters). Then go through: http://www.tldp.org/LDP/Bash-Beginne...ect_07_01.html and carefully look at table 7.1. The same information is available in man test. Finally, please show us your attempt if still in doubt and/or your final solution. I'm sure you can easily find it out! :) |
Quote:
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:
Your choice now is
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? Quote:
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... |
In bash it's even more simple than comparing two dates, but obviously we don't know what is the topic of the related lessons and we cannot tell what is the best solution for this exercise. Sorry, Dru-Jitsu, but it's up to you now.
|
Code:
#!/bin/bash |
All times are GMT -5. The time now is 08:30 PM. |