Deutsch   English   Français   Italiano  
<83v7snfkm5.fsf@helmutwaitzmann.news.arcor.de>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Helmut Waitzmann <nn.throttle@xoxy.net>
Newsgroups: comp.unix.shell
Subject: Re: (shellcheck) SC2103
Date: Wed, 05 Mar 2025 18:33:22 +0100
Organization: A noiseless patient Spider
Lines: 161
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <83v7snfkm5.fsf@helmutwaitzmann.news.arcor.de>
References: <vq9nto$18ps$1@news.xmission.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Date: Wed, 05 Mar 2025 18:33:45 +0100 (CET)
Injection-Info: dont-email.me; posting-host="5f6bce4b910b72fd5efc7bed1fe5ae8f";
	logging-data="2652528"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+RMsGqxNdUsJo7TiwMarQJt+fjvI06Kwc="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:261PiPLzTHA69m/bKoz44nJS+Gw=
	sha1:pHcCypJQ3wbTzprsLGd8YhqecM4=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
Bytes: 4849

 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 re=
al
> rationale is that if the cd fails, putting it into a subshell "localizes"
> the damage.
>

 Does=20


   cd -- somedir &&
   {
     do_something
     cd ..
   }


  make shellcheck happy while at the same time localizing the=20
  damage and avoiding a subshell?=20


>     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.=20


 But note, that=20


   cd -- "$OLDPWD"


 as well as=20


   cd -


 will not restore the working directory established at the start=20
 of the=20


   cd somewhere; do_something; cd -


 commandline if that directory gets renamed or moved by a=20
 (asynchronous) process while running.=20


 Try the following command in an empty directory:=C2=A0 It creates a=20
 subdirectory named =E2=80=9Csandbox=E2=80=9D and in it more subdirectories=
 and=20
 removes everything when it ends:=20


   (
     mkdir -- sandbox &&
     {
       n=3D1 &&
       mkdir -p -- sandbox/"$n"/WD &&
       {
         while
           sleep 1 &&
           mv -- "$n"/ "$((n+1))"/ &&
           n=3D"$((n+1))"
         do
           :
         done &
       } &&
       pid=3D"$!" &&
       {
         (
           CDPATH=3D cd -- sandbox/"$n"/WD &&
           exec "$SHELL"
         )
         kill -s INT -- "$pid"
       }
       sleep 1
       rm -R -- sandbox
     }
   )


 The command creates the subdirectories =E2=80=9Csandbox/1=E2=80=9D and=20
 =E2=80=9Csandbox/1/WD=E2=80=9D and launches a process that, running in the=
=20
 background, will rename the directory =E2=80=9Csandbox/1/=E2=80=9D after 1=
 second=20
 to =E2=80=9Csandbox/2/=E2=80=9D, then, after an additional second to =E2=
=80=9Csandbox/3/=E2=80=9D=20
 and so on, continuing incrementing the number, until terminated.=20


 At the same time the command launches in the foreground an=20
 interactive (sub=E2=80=90)shell (=E2=80=9C"$SHELL"=E2=80=9D), using the di=
rectory=20
 =E2=80=9Csandbox/1/WD=E2=80=9D as its working directory, allowing the user=
 to=20
 type commands.=C2=A0 If they type =E2=80=9Cexit=E2=80=9D, the =E2=80=9C"$S=
HELL"=E2=80=9D will exit.=20


 The command then will signal the background process an INT signal=20
 and removes the directory =E2=80=9Csandbox/=E2=80=9D including its subhier=
archy.=C2=A0=20
 Finally it exits.=20


 Each second the launched =E2=80=9C"$SHELL"=E2=80=9D will have its working=
=20
 directory renamed to a new path in the file system without being=20
 notificated about that fact.=20


 In the launched interactive =E2=80=9C"$SHELL"=E2=80=9D one can repeatedly =
type the=20
 commands=20


   pwd -P

   pwd -L

   cd .


 or the like, observing, whether they continue to report the=20
 original (obsolete) path of the working directory or follow the=20
 changed path.=20


 =3D> In this use case, the command=20


   cd -- "$OLDPWD"


 might fail to restore the former working directory.=20