public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Adam Dinwoodie <adam@dinwoodie.org>
To: Fergus Daly <fergusd84@outlook.com>
Cc: "'cygwin@cygwin.com'" <cygwin@cygwin.com>
Subject: Re: bash shell script: recently running, now failing
Date: Thu, 6 Apr 2023 18:18:59 +0100	[thread overview]
Message-ID: <20230406171859.ud6jsdxgwtulu6cg@lucy.dinwoodie.org> (raw)
In-Reply-To: <DB6P18901MB005559D81FD4125B0D6DAA07A4919@DB6P18901MB0055.EURP189.PROD.OUTLOOK.COM>

On Thu, Apr 06, 2023 at 04:43:51AM +0000, Fergus Daly via Cygwin wrote:
> I have a "hash bang" bash shell script i.e. first line
> #! /bin/sh
> or equivalently
> #! /bin/bash
> For various reasons I want this file to be identified as binary so its second line
> is the single character null \x00 showing up in some editors e.g. nano as
>  ^@
> This does not prevent the script from running to a successful conclusion.
> Or not until recently. Now the script fails with
> /home/user/bin/file.old.sh: cannot execute binary file
> Q1 - was bash recently updated? Would this explain the changed behaviour?
> Q2 - if so, is this newly introduced "glitch" known and presumably intended? Or
> an unintended consequence that will be retracted in a later update? 
> I then altered the first line to
> #! /bin/dash
> whilst retaining the null character at line 2 and subsequent content also unaltered..
> The altered script file.new.sh runs as previously to a successful conclusion.
> Q3 - at 1/8 the size of bash and sh, I am not at all sure of the role and reach of dash.
> Should the edit (dash replacing bash/sh) be incorporated elsewhere or would this be a
> bad idea (and retained only locally in what is indeed an eccentric and one-off context)?

Dash is smaller and much less feature-rich than Bash.  Whether Dash is a
suitable replacement for Bash depends on how much (if at all) you're
relying on Bash-specific functions.  For very simple scripts, the only
difference is likely that Dash will be very slightly faster, but working
out whether your script is using any "Bashisms" isn't always a trivial
job.

(I have previously been involved work in migrating scripts between Ksh
and Bash, which is a similar-but-different problem, and there were *a
lot* of surprises in how the two differed.)

Depending on why you want the file to be identified as a binary, and how
that identification is being done, you could move your null byte later
in the file.  In particular, a pattern I've seen several times in Bash
is to have a normal Bash script, finishing with an explicit `exit`,
followed by an actual binary blob; this can be used to create things
like self-extracting bundles, where the binary blob is a tarball and the
script at the top of the file has the instructions for extracting the
tarball.

  parent reply	other threads:[~2023-04-06 17:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06  4:43 Fergus Daly
2023-04-06  8:03 ` Corinna Vinschen
2023-04-06 12:23   ` Scott Smith
2023-04-06 12:21 ` Andrey Repin
2023-04-07 19:34   ` Brian Inglis
2023-04-08  8:37     ` Andrey Repin
2023-04-06 17:18 ` Adam Dinwoodie [this message]
2023-04-06 19:26   ` Scott Smith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230406171859.ud6jsdxgwtulu6cg@lucy.dinwoodie.org \
    --to=adam@dinwoodie.org \
    --cc=cygwin@cygwin.com \
    --cc=fergusd84@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).