bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "return" should not continue script execution, even if used inapprop


From: Robert Elz
Subject: Re: "return" should not continue script execution, even if used inappropriately
Date: Tue, 08 Jan 2019 08:45:37 +0700

    Date:        Mon, 7 Jan 2019 08:55:58 -0500
    From:        Greg Wooledge <address@hidden>
    Message-ID:  <address@hidden>

  | https://mywiki.wooledge.org/BashFAQ/109

Which only works when the shell is bash...

address@hidden said:
  | there's a good reason for the "craziness": it enables individual testing of
  | the script's functions.

For that kind of use there's a trivial solution (as there often
is for cases when people are sure that the current definition
is inadequate).

If you have a script "dotscr" and you want to test it, then
you do ...

        cat <<-EOF >run-dotscr
        . ./dotscr
        EOF

and then

        sh run-dotscr           # or bash, or mksh, or ...

You can probably abbreviate that as

        sh -c '. ./dotscr'

What's more, if dotscr is as most scripts designed to be used
via the . command, and has no actual executable code (in the
sense that it consumes no input and produces no output, so
aside from checking for syntax errors, the above does nothing
useful) you can add extra commands into the run-dotscr script;
or as extra commands after a ';' in the -c case,  to actually call
the functions dotscr defines, or the variables it creates, or
whatever it does which needs testing.

Or alternatively, interactively ...

        sh      # start a new shell (any appropriate shell)
        . ./dotscr
        # do whatever testing you lile
        exit

Also, of course, it is also possible to write a script that can be
executed either via the '.' command, or as a standalone script,
if that is the desire - in fact many (perhaps most) scripts not
expressly designed to only work as "dot scripts" are like that.

The one piece of advice from that python related BashFAQ that
Greg referred to which is worth following is ...

        Bash can do this, but it's not a natural style,
        and you shouldn't be doing it.

kre





reply via email to

[Prev in Thread] Current Thread [Next in Thread]