LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   God I suck at scripting, please help! (https://www.linuxquestions.org/questions/linux-newbie-8/god-i-suck-at-scripting-please-help-820547/)

Dru-Jitsu 07-18-2010 10:55 AM

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.

grail 07-18-2010 11:08 AM

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=

colucix 07-18-2010 11:12 AM

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! :)

salasi 07-18-2010 12:06 PM

Quote:

Originally Posted by Dru-Jitsu (Post 4037217)
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?

Quote:

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...

colucix 07-18-2010 01:50 PM

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.

Andrew Benton 07-18-2010 05:05 PM

Code:

#!/bin/bash
set -e

[[ $1 -ot $2 ]] &&
echo "$1 is older than $2" ||
echo "$2 is older than $1"



All times are GMT -5. The time now is 08:30 PM.