LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script only appears to process one line of file (https://www.linuxquestions.org/questions/programming-9/bash-script-only-appears-to-process-one-line-of-file-578231/)

Nylex 08-19-2007 01:51 PM

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!

ilikejam 08-19-2007 02:58 PM

Your script works fine for me.

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

Dave

Nylex 08-19-2007 03:06 PM

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

ta0kira 08-19-2007 03:34 PM

Quote:

Originally Posted by Nylex (Post 2863913)
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

colucix 08-19-2007 05:00 PM

My :twocents:
Code:

date +%s -f dates

Nylex 08-19-2007 10:35 PM

Quote:

Originally Posted by ta0kira (Post 2863989)
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 (Post 2864048)
My :twocents:
Code:

date +%s -f dates

LOL, I didn't realise date had such an option! Thanks ta0kira and colucix for the replies as well.


All times are GMT -5. The time now is 07:53 PM.