LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-19-2007, 02:51 PM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Bash script only appears to process one line of file


I have a list of dates and times that I would like to convert to seconds since the epoch and these are stored in a file. I've written a shell script to do this and also generated some test data (because the actual file I want to run this on is quite large). My test data looks like this:

Thu Mar 30 2006 11:53:03
Fri Mar 31 2006 12:54:01

My shell script is the following:

Code:
#!/bin/bash

exec<dates

while read line
do
  seconds=`date -d "$line" +%s`
  echo $seconds >> seconds
done
When I view the "seconds" file, there's only one line in it, so I'm assuming that the loop only executes once for some reason. I can't figure out what I'm doing wrong though.

Any ideas?

Thanks!
 
Old 08-19-2007, 03:58 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Your script works fine for me.

Have you tried '#!/bin/bash -x' as the shebang line?

Dave
 
Old 08-19-2007, 04:06 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
That doesn't help, but it doesn't matter too much. Thanks for the suggestion though . I tried the script on another machine and it works fine there. Odd, must be a problem with my version of Bash .
 
Old 08-19-2007, 04:34 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Nylex View Post
Code:
#!/bin/bash

exec<dates

while read line
do
  seconds=`date -d "$line" +%s`
  echo $seconds >> seconds
done
Why not:
Code:
while read line
do
  date -d "$line" +%s >> seconds
done < dates
or:
Code:
cat dates | while read line
do
  date -d "$line" +%s >> seconds
done
ta0kira
 
Old 08-19-2007, 06:00 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
My
Code:
date +%s -f dates
 
Old 08-19-2007, 11:35 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
Why not:
Code:
while read line
do
  date -d "$line" +%s >> seconds
done < dates
or:
Code:
cat dates | while read line
do
  date -d "$line" +%s >> seconds
done
ta0kira
Because I only thought of one way to do it .

Quote:
Originally Posted by colucix View Post
My
Code:
date +%s -f dates
LOL, I didn't realise date had such an option! Thanks ta0kira and colucix for the replies as well.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
time a process within bash script aubrey-calm2 Linux - General 2 06-15-2007 02:39 PM
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 06:14 AM
bash script for displaying a file one line at a time. panchosansa Programming 6 10-12-2006 11:54 AM
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 08:32 AM


All times are GMT -5. The time now is 04: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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration