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

Re: The .xsession-errors problem



On Tue 27 Oct 2020 at 07:53:01 (-0400), Greg Wooledge wrote:
> On Tue, Oct 27, 2020 at 11:28:12AM +1100, David wrote:
> > On Tue, 27 Oct 2020 at 10:56, David Wright <deblis@lionunicorn.co.uk> wrote:
> > >       fuser -v "$j"
> > >       [ $? -ne 0 ] && gzip "$j" && mv -i "$j.gz" "$HOME/.monitors/xsession/"
> > 
> > > (Script improvements always appreciated.)
> 
> > https://www.shellcheck.net says:
> > """
> > Line 2:
> > [ $? -ne 0 ] && echo hi
> >   ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not
> > indirectly with $?.
> > """
> 
> In other words, instead of writing:
> 
> fuser -v thing
> [ $? != 0 ] && other thing && third thing
> 
> You can write it this way:
> 
> if ! fuser -v thing; then
>   other thing &&
>   third thing
> fi
> 
> The $? form isn't technically *wrong*, but it's awkward, and doesn't
> let you have an "else" action.

Thanks to you both for reminding me of shellcheck. I think I've got a
few of these to correct in .bashrc too. Running shellcheck on .bashrc
is always a horror show.

Ironically, though, there is one slight advantage to the explicit test
of $? in my .xsession (because it's run with   set -x   in force):
the result of the test is reflected in the log. With the improved
construction, the result has to be deduced by checking which subsequent
commands run (or which branch is taken when there's an else clause).

Cheers,
David.


Reply to: