BATsh
view release on metacpan or search on metacpan
- eg/06_sh_comprehensive.batsh: I/O-redirection section simplified to a
plain "while read" loop now that the loop terminates correctly.
- Tests: t/9070-examples.t now executes each eg/*.batsh in a child
process and guards against runaway output and "syntax error"
breakage (E4). t/9060-readme.t verifies the README advertises every
eg/ example by name (R5). README gains an EXAMPLES section.
- SH background execution: an unquoted trailing "&" starts an external
command asynchronously and returns immediately. On Win32 the job is
spawned via system(1, ...) (P_NOWAIT, PID returned); on Unix it is
started through /bin/sh without a Perl fork, capturing the job PID
via the shell's $! into a sysopen O_CREAT|O_EXCL temp file (Pure
Perl, 5.005_03). The new $! parameter expands to the most recent
background PID (empty before any job); $? is 0 on a successful
launch (the job's own exit status is not awaited). Built-ins,
functions, assignments and control words ignore the trailing "&"
and run in the foreground; "&&", ">&"/"2>&1", quoted and escaped
"\&" are not treated as background. No job control; CMD-mode "&"
remains a sequential separator (see BUGS AND LIMITATIONS).
lib/BATsh/SH.pm view on Meta::CPAN
# Shebang: treat as comment
return 0 if $line =~ /\A#!/;
# ----------------------------------------------------------------
# Background execution: an unquoted trailing & (v1).
# Detected here, BEFORE _split_sh_compound, so that the bare & is
# never mistaken for && and so that an internal & (e.g. in 2>&1 or
# >&2) is left untouched. Only the single & at the very end of the
# line is consumed. Builtins / functions / control words / variable
# assignments run in the FOREGROUND (the & is ignored); only external
# commands are launched asynchronously.
# ----------------------------------------------------------------
my ($_is_bg, $_bg_line) = _split_trailing_bg($line);
if ($_is_bg) {
$line = $_bg_line;
my $probe = $line;
$probe =~ s/\A\s+//;
my $w0 = '';
($w0) = ($probe =~ /\A(\S+)/);
$w0 = '' unless defined $w0;
if (_sh_word_is_foreground($w0)) {
lib/BATsh/SH.pm view on Meta::CPAN
close(_BG_TMP);
push @_BG_TMPFILES, $path;
return $path;
}
# EEXIST or transient error: retry with next sequence number
}
warn "sh: cannot create background pidfile in $dir: $!\n";
return undef;
}
# _bg_launch: start $cmdline asynchronously.
# Win32 : system(1, STRING) spawns via the command shell (P_NOWAIT)
# and returns the PID directly.
# Unix-like : delegate to /bin/sh so the job is backgrounded without a
# Perl fork; the shell's $! (the job PID) is written to a
# temp file and read back into BATsh's own $!.
# On a successful launch $? (LAST_STATUS) is 0; the exit code of the
# background job itself is not awaited (sh semantics).
sub _bg_launch {
my ($class, $cmdline) = @_;
$cmdline = '' unless defined $cmdline;
lib/BATsh/SH.pm view on Meta::CPAN
A here-document body line that looks like a BATsh subroutine marker
(a line of the form C<:LABEL> later followed by C<RET>/C<RETURN>) may be
consumed by subroutine extraction, which runs before mode dispatch.
Avoid such lines inside here-document bodies.
=back
=head2 Background Execution
An unquoted C<&> at the very end of an SH command line starts the command
asynchronously and returns control immediately, in the style of POSIX
shells:
longjob &
echo "next prompt"
Only the single C<&> at the end of the line is consumed. An C<&> that is
part of C<&&>, of an fd-duplication such as C<2E<gt>&1> or C<1E<gt>&2>,
inside single or double quotes, or backslash-escaped (C<\&>) is B<not>
treated as a background operator and is left in place.
( run in 1.312 second using v1.01-cache-2.11-cpan-9581c071862 )