Re: (shellcheck) SC2103

Liste des GroupesRevenir à cu shell 
Sujet : Re: (shellcheck) SC2103
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shell
Date : 05. Mar 2025, 23:09:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83ikonf7tb.fsf@helmutwaitzmann.news.arcor.de>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 Kaz Kylheku <643-408-1753@kylheku.com>:
On 2025-03-05, Kenny McCormack <gazelle@shell.xmission.com> wrote:
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
    )
>
That obviously won't work if do_something has to set a variable
that is then visible to the rest of the script.
>
Forking a process just to preserve a current working directory
is wasteful; we wouldn't do that in a C program, where we might
open the current directory to be saved, and then fchdir back to it.
>
 Unfortunately, there is no way to fchdir() by means of a shell  built‐in command, say “fchdir“.
    cd somewhere;...;cd -
>
cd - will break if any of the steps in between happen to to cd;
it is hostile toward maintenance of the script.
>
 Yes.  I really would appreciate using the imagined “fchdir” bash  built‐in command to be able to do    set_exit_status()
   {
     return ${1-}
   }
   unset -v es &&
   exec {saved_wd}< . &&
   {
     cd -- somedir &&
     {
       do_something
       es="$?"
       fchdir -- "$saved_wd"
     }
     exec {saved_wd}<&-
     set_exit_status ${es-}
   }

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