(shellcheck) SC2103

Liste des GroupesRevenir à cu shell 
Sujet : (shellcheck) SC2103
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.unix.shell
Date : 05. Mar 2025, 15:43:04
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <vq9nto$18ps$1@news.xmission.com>
User-Agent : trn 4.0-test77 (Sep 1, 2010)
All testing done with shellcheck version 0.10.0 and bash under Linux.

Shellcheck says that you should replace code like:

    cd somedir
    do_something
    cd .. # (Or, cd -, which is almost, but not exactly the same thing)

with

    (
    cd somedir
    do_something
    )

The ostensible rationale is that it is shorter/easier to code, but the real
rationale is that if the cd fails, putting it into a subshell "localizes"
the damage.

I find this analysis problematic for (at least) the following reasons:

    1) First of all, I almost always run with either -e or (more likely)
"trap handler ERR", where handler is a shell function that prints
an error message and exits.  So, if any "cd" fails, the script
aborts.  Shellcheck fails to realize this and flags every "cd" in
the script with "Don't you mean: cd ... || exit".  So, if it
recognized the trap better, both this and the SC2103 thing would
evaporate.

    2) Subshells (still, as far as I know) require a fork() and run as
another process.  Given that bash is a large program (it says so
right in the man page), this fork() is expensive.  And note that it
isn't the cheap sort of fork() where it is immediately followed by
an exec().  It's the expensive kind which requires the COW
mechanism to kick in.  So, it seems unwise for shellcheck to be
recommending using a subshell.

I certainly avoid it if possible, for this reason.

A further note:  The most common case is to use (as shown above):

    cd somewhere;...;cd ..

but the more general pattern would be:

    cd somewhere;...;cd -

But note that "cd -" - even in a script - prints the name of the directory
it is cd'ing back to, which is annoying.  I could not find any option to
turn this off, but: cd - > /dev/null
works.  I'm guessing that people avoid using "cd -" in scripts for this
reason.

--
Modern Christian: Someone  who can take time out  from using Leviticus
to defend  homophobia and  Exodus to plaster  the Ten  Commandments on
every school and courthouse to claim  that the Old Testament is merely
"ancient laws" that "only applies to Jews".

Date Sujet#  Auteur
5 Mar 25 * (shellcheck) SC21038Kenny McCormack
5 Mar 25 +* Re: (shellcheck) SC21032Helmut Waitzmann
6 Mar 25 i`- Re: (shellcheck) SC21031Kenny McCormack
5 Mar 25 `* Re: (shellcheck) SC21035Kaz Kylheku
5 Mar 25  +- Re: (shellcheck) SC21031Janis Papanagnou
5 Mar 25  +- Re: (shellcheck) SC21031Helmut Waitzmann
6 Mar 25  `* Re: (shellcheck) SC21032Kenny McCormack
6 Mar 25   `- Re: (shellcheck) SC21031Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal