Re: (shellcheck) SC2103

Liste des Groupes 
Sujet : Re: (shellcheck) SC2103
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shell
Date : 05. Mar 2025, 18:33:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83v7snfkm5.fsf@helmutwaitzmann.news.arcor.de>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 gazelle@shell.xmission.com (Kenny McCormack):
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.
>
 Does    cd -- somedir &&
   {
     do_something
     cd ..
   }
  make shellcheck happy while at the same time localizing the   damage and avoiding a subshell?
    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,
>
   cd -- "$OLDPWD"
 Will help.  But note, that    cd -- "$OLDPWD"
 as well as    cd -
 will not restore the working directory established at the start  of the    cd somewhere; do_something; cd -
 commandline if that directory gets renamed or moved by a  (asynchronous) process while running.  Try the following command in an empty directory:  It creates a  subdirectory named “sandbox” and in it more subdirectories and  removes everything when it ends:    (
     mkdir -- sandbox &&
     {
       n=1 &&
       mkdir -p -- sandbox/"$n"/WD &&
       {
         while
           sleep 1 &&
           mv -- "$n"/ "$((n+1))"/ &&
           n="$((n+1))"
         do
           :
         done &
       } &&
       pid="$!" &&
       {
         (
           CDPATH= cd -- sandbox/"$n"/WD &&
           exec "$SHELL"
         )
         kill -s INT -- "$pid"
       }
       sleep 1
       rm -R -- sandbox
     }
   )
 The command creates the subdirectories “sandbox/1” and  “sandbox/1/WD” and launches a process that, running in the  background, will rename the directory “sandbox/1/” after 1 second  to “sandbox/2/”, then, after an additional second to “sandbox/3/”  and so on, continuing incrementing the number, until terminated.  At the same time the command launches in the foreground an  interactive (sub‐)shell (“"$SHELL"”), using the directory  “sandbox/1/WD” as its working directory, allowing the user to  type commands.  If they type “exit”, the “"$SHELL"” will exit.  The command then will signal the background process an INT signal  and removes the directory “sandbox/” including its subhierarchy.   Finally it exits.  Each second the launched “"$SHELL"” will have its working  directory renamed to a new path in the file system without being  notificated about that fact.  In the launched interactive “"$SHELL"” one can repeatedly type the  commands    pwd -P
   pwd -L
   cd .
 or the like, observing, whether they continue to report the  original (obsolete) path of the working directory or follow the  changed path.  => In this use case, the command    cd -- "$OLDPWD"
 might fail to restore the former working directory.
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