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

Re: grep: show matching line from pattern file



* David Christensen <dpchrist@holgerdanske.com> [22-06/02=Thu 15:50 -0700]:
> On 6/2/22 15:13, Will Mengarini wrote:
>> * Greg Wooledge <greg@wooledge.org> [22-05/28=Sa 17:11 -0400]:
>>> [...]
>>> #!/usr/bin/perl
>>> use strict; use warnings;
>>> [...]
>>> open PATS, "<patterns.txt" || die "can't open patterns.txt";
>>> [...]
>>
>> You need "or die", not "|| die", because of precedence: what you coded
>> checks whether "<patterns.txt" is logically true (it is), whereas you
>> wanted to check whether the result of open() is logically true.
>
> +1  That is a good explanation of a Perl fine point/ gotcha.
>
>> In this transcript, the number before the prompt-ending '$' is $?:
>> --------------------------------
>> debian/pts/4 bash3 ~ 14:56 0$perl -e 'open "gweeblefleep" || die'
>> debian/pts/4 bash3 ~ 14:57 0$perl -e 'open "gweeblefleep" or die'
>> Died at -e line 1.
>> debian/pts/4 bash3 ~ 14:57 2$
>> --------------------------------
>
> What is your shell?  PS1?

The shell is Bash 5.1.4.  My PS1 is constructed by an elaborate script
that's old enough to have sex in Thailand, but you can get the effect
of what I posted by setting PS1 with the line
--------------------------------
PS1="\\h/${TTY#/dev/} \\s$SHLVL \\w \\A \$?\\\$"
--------------------------------
assuming you're running at least Bash 2.05a.  You may prefer
--------------------------------
PS1="\\h/${TTY#/dev/} \\s^$SHLVL \\w \\A \$?\\\$"
--------------------------------.

My original script was coded for Bash 1.4.7, and had to do
--------------------------------
PS1="\\h${TTY#/dev/} \\s$SHLVL \\w \`s=\$?;date +%H:%M;exit \$s\` \$?\\\$"
--------------------------------
because \A wasn't available, so 'date' had to be run in a subshell
that needed to take care to save and restore $?.  (The variable
it uses for that, s, goes away when the subshell does; and that
scary-looking exit just exits the subshell, resetting $?.)

-- 
                 Will Mengarini  <seldon@eskimo.com>
         Free software: the Source will be with you, always.
               sh -c 'echo -n MENGARINI|sum -s|colrm 4'


Reply to: