LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-18-2010, 11:55 AM   #1
Dru-Jitsu
Member
 
Registered: Jun 2010
Posts: 38

Rep: Reputation: 15
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.
 
Old 07-18-2010, 12:08 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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=
 
Old 07-18-2010, 12:12 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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!
 
Old 07-18-2010, 01:06 PM   #4
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,053

Rep: Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881
Quote:
Originally Posted by Dru-Jitsu View Post
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...
 
Old 07-18-2010, 02:50 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 07-18-2010, 06:05 PM   #6
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
#!/bin/bash
set -e

[[ $1 -ot $2 ]] &&
echo "$1 is older than $2" ||
echo "$2 is older than $1"
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Are there any Bash scripting guides that don't suck? Alex_Dc Linux - Newbie 8 11-11-2009 08:43 AM
LOL I suck {DoG}Corpse Linux - Newbie 30 09-13-2005 06:09 PM
I suck at sed dc2447 Programming 9 05-17-2005 09:49 AM
why does yum suck so much? borrrden Linux - Software 4 07-04-2004 11:38 PM
Does KDE 3.0 Not SUCK? lackluster Linux - General 24 05-17-2002 10:32 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration