LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-22-2010, 08:59 AM   #1
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
problem with line buffering using here document in bash.


I keep time sheet entries at work in an sqlite database called 'timesheet'.

I have a shell script called 'today' which queries for all timesheet entries which are less than 24 hours old; it looks like this:

Code:
#!/bin/bash
echo "select datetime(start_time, 'unixepoch', 'localtime'), datetime(end_time, 'unixepoch', 'localtime'), project, site, duration from timesheet where start_time > strftime('%s', 'now', '-1 day', 'localtime');" | sqlite3 ~/.timesheet/timesheet.db
In an effort to make it more readable, I've put the query inside a here document:

Code:
#!/bin/bash
cat | sqlite3 ~/.timesheet/timesheet.db << query
select
        datetime(start_time, 'unixepoch', 'localtime')
        , datetime(end_time, 'unixepoch', 'localtime')
        , project
        , site
        , duration
from
        timesheet
where
        start_time > strftime('%s', 'now', '-1 day', 'localtime');
query
The problem is that the second shell script doesn't return to the bash prompt after it runs... it requires me to press 'enter' before I get my prompt back. I can't figure out why.

Ok... I just solved my problem by re-directing the here document directly to sqlite3, rather than using 'cat |':

Code:
#!/bin/bash
sqlite3 ~/.timesheet/timesheet.db << query
select
        datetime(start_time, 'unixepoch', 'localtime')
        , datetime(end_time, 'unixepoch', 'localtime')
        , project
        , site
        , duration
from
        timesheet
where
        start_time > strftime('%s', 'now', '-1 day', 'localtime');
query
but it's still not clear to me why the former wasn't working.
 
Old 01-22-2010, 09:12 AM   #2
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
I believe it's because the << is being applied to the sqllite command and not the cat command, but when the script starts, cat has to do something to pass the control to the sqllite cmd, which is the extra enter that you have to press. If you try ths: cat << query | sqllite.... the script won't wait for an enter. The better solution as you already found out was to remove cat completely. Another solution that I always use is just use the echo command as in your original script, just break it into multiple lines; the shell will handle it.

Code:
echo "
select stmt ...
....
....
" | sqllite....
 
1 members found this post helpful.
Old 01-22-2010, 04:48 PM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Thanks, rn. That makes perfect sense...

as such, I think that I could also have done this:

Code:
#!/bin/bash
(cat | sqlite3 ~/.timesheet/timesheet.db) << query
select
        datetime(start_time, 'unixepoch', 'localtime')
        , datetime(end_time, 'unixepoch', 'localtime')
        , project
        , site
        , duration
from
        timesheet
where
        start_time > strftime('%s', 'now', '-1 day', 'localtime');
query
... although there's no reason to.
 
  


Reply



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
[SOLVED] Problem executing a bash line read from another file. Mountain Man Programming 4 12-02-2009 04:16 PM
[SOLVED] [BASH] here document creates problem with if-fi statemens. RaptorX Programming 17 08-27-2009 11:53 AM
document editor for command-line snama Linux - Software 3 06-24-2008 02:43 PM
Streams and buffering problem MadCactus Programming 2 02-17-2004 01:32 PM
frame buffering problem. GT-GEO Linux - General 3 01-31-2002 04:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 AM.

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
Open Source Consulting | Domain Registration