Embedding data in a ksh script?

Embedding data in a ksh script?

am 10.09.2007 17:50:41 von andrew.fabbro

What is the best way to embed data into a ksh (88) script?

I have a script that needs to use a set of static data. I
could put it in an external text file and process it, of
course, but I thought I would just include it in the script
since it never changes.

By "include it" I mean "section of the script that can
be treated as if it was input". i.e., something like
perl's __DATA__ section.

The actual data is a set of colon-separated values.

SOMETHING:0:123:456:789:10
ELSE:5:567:213:405:88
ANOTHER:4:616:310:110:45

etc. I could simply set up variables, but they'd be
in the form of

name[0]="SOMETHING"
field[0]=0
field2[1]=123
field3[2]=456
field4[3]=789
field5[4]=10

This is somewhat less convenient to enter/edit.
Considering there are several hundred lines of data,
it also balloons the script's length enormously (this
doesn't really affect its speed or function, just
esthetics). This is not a horrible option, I'd just
like to see if there are alternatives.

I thought of using a heredoc:

# ############ DATA ##############

(cat < #
# FORMAT is name:field1:field2:etc.
#
SOMETHING:0:123:456:789:10
ELSE:5:567:213:405:88
ANOTHER:4:616:310:110:45
#
# end of data
#
EOF
)| while read line ; do
# process line, which ignores comments
done

That works fine, though it mingles definition
(data) and processing...I'd prefer to have the
data in one area and the processing in a function,
since in some cases it may not be needed.

Anything like perl's __DATA__ in ksh88? Similar?

Thanks.

Re: Embedding data in a ksh script?

am 10.09.2007 18:56:48 von Janis Papanagnou

Andrew Fabbro wrote:
> What is the best way to embed data into a ksh (88) script?
>
> I have a script that needs to use a set of static data. I
> could put it in an external text file and process it, of
> course, but I thought I would just include it in the script
> since it never changes.
>
> By "include it" I mean "section of the script that can
> be treated as if it was input". i.e., something like
> perl's __DATA__ section.
>
> The actual data is a set of colon-separated values.
>
> SOMETHING:0:123:456:789:10
> ELSE:5:567:213:405:88
> ANOTHER:4:616:310:110:45
>
> etc. I could simply set up variables, but they'd be
> in the form of
>
> name[0]="SOMETHING"
> field[0]=0
> field2[1]=123
> field3[2]=456
> field4[3]=789
> field5[4]=10

(How would the ELSE and ANOTHER fields coded; continued
as field6, field7, ...?)

>
> This is somewhat less convenient to enter/edit.
> Considering there are several hundred lines of data,
> it also balloons the script's length enormously (this
> doesn't really affect its speed or function, just
> esthetics). This is not a horrible option, I'd just
> like to see if there are alternatives.
>
> I thought of using a heredoc:
>
> # ############ DATA ##############
>
> (cat < > #
> # FORMAT is name:field1:field2:etc.
> #
> SOMETHING:0:123:456:789:10
> ELSE:5:567:213:405:88
> ANOTHER:4:616:310:110:45
> #
> # end of data
> #
> EOF
> )| while read line ; do
> # process line, which ignores comments
> done
>
> That works fine, though it mingles definition
> (data) and processing...

I think here-docs are the most legible way to do it.
And since your data is fixed...

Would you prefer using variables to carry _each line_?
data[0]="SOMETHING:0:123:456:789:10"
data[1]="ELSE:5:567:213:405:88"
....
and split them on demand setting IFS=":"?

Janis

> I'd prefer to have the
> data in one area and the processing in a function,
> since in some cases it may not be needed.
>
> Anything like perl's __DATA__ in ksh88? Similar?
>
> Thanks.
>

Re: Embedding data in a ksh script?

am 10.09.2007 20:24:35 von andrew.fabbro

On Sep 10, 9:56 am, Janis Papanagnou
wrote:
> > SOMETHING:0:123:456:789:10
> > ELSE:5:567:213:405:88
> > ANOTHER:4:616:310:110:45
>
> > etc. I could simply set up variables, but they'd be
> > in the form of
>
> > name[0]="SOMETHING"
> > field[0]=0
> > field2[1]=123
> > field3[2]=456
> > field4[3]=789
> > field5[4]=10
>
> (How would the ELSE and ANOTHER fields coded; continued
> as field6, field7, ...?)

Sorry, poor example on my part.

It would be more like...

name[0]="SOMETHING"
field[0]=0
field2[0]=123
field3[0]=456
field4[0]=789
field5[0]=10

....so index 0 is consistent.

Re: Embedding data in a ksh script?

am 10.09.2007 20:27:40 von Ed Morton

Andrew Fabbro wrote:
> What is the best way to embed data into a ksh (88) script?
>
> I have a script that needs to use a set of static data. I
> could put it in an external text file and process it, of
> course, but I thought I would just include it in the script
> since it never changes.
>
> By "include it" I mean "section of the script that can
> be treated as if it was input". i.e., something like
> perl's __DATA__ section.
>
> The actual data is a set of colon-separated values.
>
> SOMETHING:0:123:456:789:10
> ELSE:5:567:213:405:88
> ANOTHER:4:616:310:110:45
>
> etc. I could simply set up variables, but they'd be
> in the form of
>
> name[0]="SOMETHING"
> field[0]=0
> field2[1]=123
> field3[2]=456
> field4[3]=789
> field5[4]=10
>
> This is somewhat less convenient to enter/edit.
> Considering there are several hundred lines of data,
> it also balloons the script's length enormously (this
> doesn't really affect its speed or function, just
> esthetics). This is not a horrible option, I'd just
> like to see if there are alternatives.
>
> I thought of using a heredoc:
>
> # ############ DATA ##############
>
> (cat < > #
> # FORMAT is name:field1:field2:etc.
> #
> SOMETHING:0:123:456:789:10
> ELSE:5:567:213:405:88
> ANOTHER:4:616:310:110:45
> #
> # end of data
> #
> EOF
> )| while read line ; do
> # process line, which ignores comments
> done
>
> That works fine, though it mingles definition
> (data) and processing...I'd prefer to have the
> data in one area and the processing in a function,
> since in some cases it may not be needed.
>
> Anything like perl's __DATA__ in ksh88? Similar?

No, but you could do:

----------
data="\
#
# FORMAT is name:field1:field2:etc.
#
SOMETHING:0:123:456:789:10
ELSE:5:567:213:405:88
ANOTHER:4:616:310:110:45
#
# end of data
#"
----------

and then:

----------
echo "$data" | sed '/^#/d' |
while read d
do
printf "d=\"%s\"\n" "$d"
done
----------

or:

----------
while read d
do
# filter out #-started lines, then
printf "d=\"%s\"\n" "$d"
done < $data
!
----------

or:

----------
awk -v d="$data" 'BEGIN{gsub(/#[^\n]*(\n|$)/,"",d); printf "%s",d}' |
while read d
do
printf "d=\"%s\"\n" "$d"
done
----------

or similair.

Ed.

Re: Embedding data in a ksh script?

am 11.09.2007 18:15:27 von andrew.fabbro

On Sep 10, 11:27 am, Ed Morton wrote:
> No, but you could do:
>
> ----------
> data="\

Ah, brilliantly simple. Thanks much.