Skip to content

Commit

Permalink
read from stdin if the file name is -
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyx committed May 29, 2022
1 parent b0615e7 commit 01a8d57
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 19 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ install: all
cat LICENSE; \
) >>$(mandir)/man1/shsub.1
@echo generate "`echo $(bindir)/shsub`"
@>$(bindir)/shsub
@>>$(bindir)/shsub echo '#!/bin/sh'
@>>$(bindir)/shsub echo exec `echo $(libdir)/shsub/cli` '"$$@"'
@( \
echo '#!/bin/sh'; \
echo exec `echo $(libdir)/shsub/cli` '"$$@"'; \
) >$(bindir)/shsub
@chmod 755 $(bindir)/shsub

test: all
Expand Down
8 changes: 6 additions & 2 deletions cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ trap '[ -f "$script" ] && rm "$script"' EXIT
trap 'exit 1' TERM INT HUP
if [ $# -gt 0 ]; then
printf '%s\n' progname="`shesc "$1"`" >>"$script"
<"$1" preproc | "$tc" >>"$script"
if [ "$1" = - ]; then
preproc | "$tc" >>"$script"
else
<"$1" preproc | "$tc" >>"$script"
fi
shift
else
preproc | "$tc" >>"$script"
fi
$sh "$script" "$@"
"$sh" "$script" "$@"
25 changes: 12 additions & 13 deletions shsub.1
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ and execute it

.SH SYNOPSIS

\fBshsub\fR [\fIoptions\fR] [\fIfile\fR\]
\fBshsub\fR [\fIoptions\fR] [\fIfile\fR\] [\fIargument\fR ...]

.PP
\fBshsub\fR reads the shell template from \fIfile\fR.
If \fIfile\fR is omitted, \fBshsub\fR reads from stdin.
If \fIfile\fR is \fB-\fR or omitted, \fBshsub\fR reads from stdin.

.SH DESCRIPTION

Expand Down Expand Up @@ -49,17 +49,16 @@ This is for the convenience of making executable shell templates.

.SH READING INPUT

When the template is stdin,
no argument will be passed to the template
and \fB$0\fR could be any value.
If the template contains commands reading stdin,
e.g. \fBread\fR(1), the behavior is unspecific.

When the template is a named file,
all arguments after the file name are passed to the template,
and \fB$progname\fR is set to the template name.
The template can safely read stdin.
However, \fB$0\fR could still be any value.
Arguments are passed to the compiled script,
but \fB$0\fR could be any value.

The shell variable \fB$progname\fR is
set to \fB-\fR if the template is read from stdin.
Otherwise it's set to the template name.

If the template is read from stdin
and the the template itself reads stdin,
the behavior is unspecific.

.SH OPTIONS

Expand Down
Empty file added test/19/ansout
Empty file.
6 changes: 6 additions & 0 deletions test/19/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env shsub
We've learnt from <%=$progname%>:

<% for i in "$@"; do -%>
- <%= $i %>
<% done -%>
5 changes: 5 additions & 0 deletions test/19/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
We've learnt from -:

- let the machine do the work
- let other people do the work
- do the job in stages
8 changes: 8 additions & 0 deletions test/19/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

<in shsub -\
"let the machine do the work" \
"let other people do the work" \
"do the job in stages" \
>progout
diff out progout
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.0-dev
1.4.0

0 comments on commit 01a8d57

Please sign in to comment.