Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

script & data in same file

29 views
Skip to first unread message

paris2venice

unread,
May 24, 2017, 5:46:36 AM5/24/17
to
I have a text file where I found it useful to include the script at the top of the data. However, what seems to work from the shell, does not seem to work in the script.

In the script, I use "^exit" to mark the end of the script and the beginning of grep searches and I do that with:

more +/^exit $0 |grep "pattern"

However, this is what is now not working in the script even though it works flawlessly from the shell. I want to keep my script and data together ... is there a better way to achieve the same thing?

Thanks!

Janis Papanagnou

unread,
May 24, 2017, 5:59:45 AM5/24/17
to
On 24.05.2017 11:46, paris2venice wrote:
> I have a text file where I found it useful to include the script at the top
> of the data. However, what seems to work from the shell, does not seem to
> work in the script.
>
> In the script, I use "^exit" to mark the end of the script and the
> beginning of grep searches and I do that with:
>
> more +/^exit $0 |grep "pattern"

It would probably be much clearer if you'd just tell us what you actually
want to achieve and if you provide complete and comprehensible samples
and showing how you call it and what shell versions you use.

>
> However, this is what is now not working in the script even though it works
> flawlessly from the shell. I want to keep my script and data together ...
> is there a better way to achieve the same thing?

Usually you use a mechanism that is called here-document; redirecting a
block of data into some program (or into some other shell construct).
For example...

awk '
...awk program code here...
' <<EOT
data line 1
...
data line N
EOT

For terse (one-line) data there's a simpler form using the <<< operator,
as in

bc -l <<< 12/5*100

Hope that helps, otherwise please clarify your problem.

Janis

>
> Thanks!
>

Dan Espen

unread,
May 24, 2017, 11:18:54 AM5/24/17
to
For your search terms use:

unix bin self extracting

these files usually end in ".bin" and contain a script followed by data.

You could also search for "makeself".

--
Dan Espen

paris2venice

unread,
May 26, 2017, 3:28:21 AM5/26/17
to
Hi Janis,

Thanks for your assistance.

$ bash --version
GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.

This is the script:

#!/bin/bash
usage()
{
N=$(basename $0)
printf "%s\n" "$N [ month ]"
printf "%s\n" "$N [ hour ]"
printf "%s\n" "$N [ ^Gardiner] # search for a specific name"
exit 1
}
[ $# -eq 0 ] && show_usage
re="guardians.*named|protected.by"
case $1 in
mon* ) more +/^exit $0 |grep "MK.*month" ;;
hour ) more +/^exit $0 |grep Hour: |egrep "$re" ;;
?* ) more +/^exit $0 |grep -v "^#" |grep "$1" ;;
esac
exit 0
... over 3600 lines of data (essentially a dictionary)

Janis Papanagnou

unread,
May 26, 2017, 6:14:14 AM5/26/17
to
You probably want something like this

case $1 in
(mon*) grep "MK.*month" ;;
(hour) grep "Hour:" | egrep "$re" ;;
(*) grep -v "^#" | grep "$1" ;;
esac <<EOT | more
... over 3600 lines of data (essentially a dictionary)
EOT

but I'm not sure what you intend with the more +/^exit pager call;
a pager is usually used at the end of a command pipeline. (As said, it
would probably be much clearer if you'd just tell us what you actually
want to achieve here.) If all you want is skipping the program then you
don't need a pager at all (for that purpose at least) and you can omit
it completely:

case $1 in
(mon*) grep "MK.*month" ;;
(hour) grep "Hour:" | egrep "$re" ;;
(*) grep -v "^#" | grep "$1" ;;
esac <<EOT
... over 3600 lines of data (essentially a dictionary)
EOT

As said in my previous post, the essential part for embedded data is the
here-document redirection ('<<'), in this case applied to the whole case
shell construct (affecting standard input of the embedded commands).

Janis

paris2venice

unread,
May 26, 2017, 1:39:24 PM5/26/17
to
Many thanks, Janis! That works perfectly and is far more elegant.

Jens Schweikhardt

unread,
May 27, 2017, 3:38:35 AM5/27/17
to
paris2venice <paris2...@gmail.com> wrote
in <85723855-b1ad-4941...@googlegroups.com>:

# usage()
# {
# N=$(basename $0)
# printf "%s\n" "$N [ month ]"
# printf "%s\n" "$N [ hour ]"
# printf "%s\n" "$N [ ^Gardiner] # search for a specific name"
# exit 1
# }

Save a process and some typing today:

N=${0##*/}
printf "%s\n" "$N [ month ]" \
"$N [ hour ]" \
"$N [ ^Gardiner] # search for a specific name"

since printf repeatedly uses the format until all args are consumed.

Regards,

Jens
--
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)

Janis Papanagnou

unread,
May 27, 2017, 8:36:24 AM5/27/17
to
On 27.05.2017 09:38, Jens Schweikhardt wrote:
> paris2venice <paris2...@gmail.com> wrote
> in <85723855-b1ad-4941...@googlegroups.com>:
>
> # usage()
> # {
> # N=$(basename $0)
> # printf "%s\n" "$N [ month ]"
> # printf "%s\n" "$N [ hour ]"
> # printf "%s\n" "$N [ ^Gardiner] # search for a specific name"
> # exit 1
> # }
>
> Save a process and some typing today:

(The OP is using bash, but note, just BTW, that in Kornshell printf
would be a builtin and not require a process.)

>
> N=${0##*/}
> printf "%s\n" "$N [ month ]" \
> "$N [ hour ]" \
> "$N [ ^Gardiner] # search for a specific name"
>
> since printf repeatedly uses the format until all args are consumed.

Note also that you can use (here as well) a here-document to achieve
that without many processes (which might also be clearer without all
that arguments and quoting that printf requires and the escapes)...

N=${0##*/}
cat <<EOT
$N [ month ]
$N [ hour ]
$N [ ^Gardiner] # search for a specific name
EOT

...so the OP's program would effectively be just two here-documents
and he would yet save not much but even some more typing. :-)

Janis

>
> Regards,
>
> Jens
>

Dario Niedermann

unread,
May 29, 2017, 12:54:21 PM5/29/17
to
paris2venice <paris2...@gmail.com> wrote:

> more +/^exit $0 |grep "pattern"

Don't use `more': it's an interactive program, and it's overkill.
Use `sed' instead:

sed -n -e '/^exit/,$p' "$0" |grep "pattern"

--
Dario Niedermann. Also on the Internet at:

gopher://darioniedermann.it/ <> https://www.darioniedermann.it/
0 new messages