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-30-2010, 01:52 AM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
pseudo-files in shell programming?


Perl has the concept of pseudo virtual files where data can be appended to the end of the script, & Perl will treat it as a legitimate source of data:
Code:
$ cat pseudo_file.pl
#!/usr/bin/env perl

use strict;

while (<DATA>) {
    print;
}

__DATA__
1
2
3
$ perl pseudo_file.pl
1
2
3
$ _
I would really like to be able to do something similar in a Bourne shell script where I don't have to manage a collateral text file which is prone to getting lost. Does anyone know if there is a similar construct in shell programming? I would have thought this feature was a remnant that Perl inherited from its shell programming roots, but searching on the Web has revealed nothing of consequence.

Anyone have insight into similar functionality?

Thanks.
 
Old 01-30-2010, 03:44 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
In shell they are called 'here documents':

http://tldp.org/LDP/abs/html/here-docs.html
 
Old 01-30-2010, 03:47 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
AFAIK no such facility exists. While it could be programmed there would be a fundamental problem in that it is not possible for bash scripts to reliably find the file they are run from as explained here.
 
Old 01-30-2010, 03:51 AM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Quote:
Originally Posted by catkin View Post
AFAIK no such facility exists. While it could be programmed there would be a fundamental problem in that it is not possible for bash scripts to reliably find the file they are run from as explained here.
Are you replying to the correct post?
 
Old 01-30-2010, 03:52 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
You can also write things like this:
Code:
while read line; do
  echo "$line"
done <<<"first line
second line
third line
last line"
 
Old 01-30-2010, 03:56 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Or even:
Code:
DATA="1
2
3
$(/get/output/from/another/script.sh)
1
2
3"

while read line; do
  echo "$line"
done <<<"$DATA"
On numerous alternatives, actually, using command substitution…

Yves.
 
Old 01-30-2010, 02:30 PM   #7
ocicat
Member
 
Registered: May 2007
Posts: 208

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by theYinYeti View Post
You can also write things like this...
Thanks, but the <<< redirection operator is Bash-specific.
 
Old 01-30-2010, 02:37 PM   #8
ocicat
Member
 
Registered: May 2007
Posts: 208

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by macemoneta View Post
In shell they are called 'here documents'...
Thanks. I am aware of "here" documents. I was hoping that someone would know the history or lineage where __DATA__ came from (thinking it came from shell programming...), but just as searching didn't reveal anything, it must have started with Perl.

Larry Wall is just one righteous dude.

Thanks everyone for your answers.
 
Old 01-30-2010, 04:44 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by macemoneta View Post
Are you replying to the correct post?
The OP requirement was "data can be appended to the end of the script"
 
Old 01-30-2010, 05:08 PM   #10
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Quote:
Originally Posted by ocicat View Post
Thanks. I am aware of "here" documents. I was hoping that someone would know the history or lineage where __DATA__ came from (thinking it came from shell programming...), but just as searching didn't reveal anything, it must have started with Perl.

Larry Wall is just one righteous dude.

Thanks everyone for your answers.
As far as the origin, it goes back much before perl. Mainframe systems, using punched card readers use a process control language (early scripting) called JCL (job control language). Card decks were 'stacked' in the reader: JCL, source, more JCL, data. For example:


Code:
//MYJOB   JOB  CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),REGION=256K
//S1      EXEC PGM=IEBGENER
//SYSUT1   DD  DATA,DLM=$$
/*******************************************************/
/*                                                     */
/*                      Hello World                    */
/*                                                     */
/*******************************************************/
$$
//SYSUT2   DD  SYSOUT=A
//SYSPRINT DD  SYSOUT=A
//SYSIN    DD  DUMMY
//
The DLM defines the delimiter for the inline data. This has been around since the mid-1960's.
 
Old 01-31-2010, 01:10 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by macemoneta View Post
As far as the origin, it goes back much before perl. Mainframe systems, using punched card readers use a process control language (early scripting) called JCL (job control language).
Oooo! It made me come over all nostalgic I remember when ...

JCL is where the dd command came from, both in name and its unconventional (for UNIX) syntax. IIRC DD meant "data definition" ... ?
 
Old 01-31-2010, 06:12 PM   #12
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by catkin View Post
IIRC DD meant "data definition"
That's correct, it did.

In FORTRAN shops, for example, you could save compute time (and therefore money in your research budget) by compiling your program and sending the object deck to the card punch. Then you could run your program over and over with different sets of data (in those days, a file was known as a "data set"). You'd put the punched object deck near the beginning of your batch job preceded by this:
Code:
//LINK.SYSIN DD *
and followed by the all-purpose end of file indicator:
Code:
/*
That end of file indicator could have anything you wanted in columns 3 through 80, so it was customary (at least at UC Irvine) to have lots of cards available which, for the sake of convenience, looked like this:
Code:
/*                                                                            */
 
  


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
Pseudo terminal programming Alien_Hominid Programming 5 01-20-2008 04:19 AM
Shell programming: Question about source files and read data from the files ccwq Programming 3 08-05-2007 12:13 AM
Shell programming with CGI/Letting users write to files. sunksullen Programming 8 07-02-2007 01:30 AM
shell programming - removing files with spaces ANU Programming 4 08-08-2006 04:42 AM
Regarding Pseudo tty, Pseudo terminals ? mqureshi Programming 0 07-30-2005 10:51 AM

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

All times are GMT -5. The time now is 11:00 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