Re: bash conditional expressions

2021-11-17 Thread Michael J. Baars
On Fri, 2021-11-12 at 19:48 +0100, Andreas Schwab wrote:
>   FILE1 -nt FILE2  True if file1 is newer than file2 (according to
>modification date).
> 
> Andreas.
> 

So now we have a relation for 'older than' and for 'newer than', but how about 
'oldest' (executable), and 'newest' (executable)?

I could only come up with this:

unset y; for x in $(find bin -mindepth 1 -name "*"); do if [[ ${x} -nt ${y} ]]; 
then y=${x}; fi; done; echo newest: ${y};
y="bin"; for x in $(find bin -mindepth 1 -name "*"); do if [[ ${x} -ot ${y} ]]; 
then y=${x}; fi; done; echo oldest: ${y};

As you can see, the way the commands are initialized is not identical, because:

'-nt' returns a true when 'if file1 exists and file2 does not'  (y in 
initialized by the first condition evaluated)
'-ot' returns a true when 'if file2 exists and file1 does not'  (y is not 
initialized by the first condition evaluated)

When you try to selectively link new executables, I think it is important that 
you do not only have relations for 'older than' and 'newer than', but also 
consistent (identically initializated)
relations for 'oldest' and 'newest'.

Mischa.





Re: bash conditional expressions

2021-11-17 Thread Michael J. Baars
On Wed, 2021-11-17 at 14:06 +0200, Ilkka Virta wrote:
> On Wed, Nov 17, 2021 at 1:33 PM Andreas Schwab  wrote:
> > On Nov 17 2021, Michael J. Baars wrote:
> > 
> > 
> > 
> > > When -N stands for NEW, and touch (-am) gives you a new file
> > 
> > 
> > 
> > It doesn't.  The file hasn't been modified after it was last read.
> 
> touch creates the given file if it doesn't previously exist. Immediately 
> afterwards,it could be called "new" in the usual English meaning, and would 
> be new in thesense that nothing was done to it
> after it was created. But:
> $ echo $BASH_VERSION5.1.8(3)-maint
> $ rm foo.txt
> $ ls -l foo.txt
> ls: cannot access 'foo.txt': No such file or directory
> $ touch -am foo.txt
> $ if test -N foo.txt; then echo is new; else echo is NOT new; fi
> is NOT new
> 
> Of course "new" is not an exact concept, it could be defined e.g. to compare 
> the
> file timestamps with the current time.
> 
> 
> Anyway, the documentation doesn't seem to say 'test -N' tests if the file is 
> "new".

It seemed logical to assume that '-N' stands for 'new' in some way. The rest of 
the line does indeed not imply '-N' to be equivalent to 'new'.


Re: bash conditional expressions

2021-11-17 Thread Michael J. Baars
On Fri, 2021-11-12 at 19:48 +0100, Andreas Schwab wrote:
>   FILE1 -nt FILE2  True if file1 is newer than file2 (according to
>modification date).
> 
> Andreas.
> 

This would indeed also solve the problem at hand :)




Re: bash conditional expressions

2021-11-17 Thread Michael J. Baars
On Mon, 2021-11-15 at 09:23 -0500, Chet Ramey wrote:
> On 11/12/21 4:36 AM, Mischa Baars wrote:
> 
> > Could you please restore the Fedora 32 behaviour? Someone must have read
> > the bash manual a little too precise, because now the statement only
> > returns true when a 'touch -a test' is given and not when a 'touch -am
> > test' is given.
> > 
> > As I understand it, -N stands for NEW and therefore should return a true
> > when either a 'touch -a test' or a 'touch -am test' is given.
> 
> Why do you think `touch -am', which sets the atime and mtime to the same
> value, should make -N true?

When -N stands for NEW, and touch (-am) gives you a new file, then -N should 
return true on a newly created file and the documentation is incomplete.

> 
> If test -N is a strict test that mtime > atime, it is working correctly
> and you have managed to defeat it by setting atime == mtimne.
> 




Re: bash conditional expressions

2021-11-12 Thread Michael J. Baars
Yeeh, that's funny indeed :)

Now this: 

time ( test2Y=$(stat -c %Y test2); for (( i=0; i<1024; i++ )); do if (( $(stat 
-c %Y test1) < ${test2Y} )); then echo >> /dev/null; else echo >> /dev/null; fi;
done; );

real0m4.503s
user0m1.048s
sys 0m3.240s

time ( for (( i=0; i<1024; i++ )); do if [[ -N test1 ]]; then echo >> 
/dev/null; else echo >> /dev/null; fi; done; );

real0m0.034s
user0m0.023s
sys 0m0.009s

On Fri, 2021-11-12 at 12:16 -0500, Lawrence Velázquez wrote:
> On Fri, Nov 12, 2021, at 4:36 AM, Mischa Baars wrote:
> > Using Fedora 32 (bash 5.0.17) this returns a true, while on Fedora 35 (bash
> > 5.1.8) this returns a false:
> > touch test; if [[ -N test ]]; then echo true; else echo false; fi;
> > 
> > [...]
> > 
> > As I understand it, -N stands for NEW and therefore should return a true
> > when either a 'touch -a test' or a 'touch -am test' is given.
> 
> FWIW, there's some disagreement on this.
> 
> % cat foo_test
> test -N foo
> echo "$?"
> % touch foo
> % /bin/bash -c 'echo "$BASH_VERSION"; . ./foo_test'
> 3.2.57(1)-release
> 0
> % /opt/local/bin/bash -c 'echo "$BASH_VERSION"; . ./foo_test'
> 5.1.8(1)-release
> 1
> % ksh -c 'echo "${.sh.version}"; . ./foo_test'
> Version AJM 93u+ 2012-08-01
> 1
> % yash -c 'echo "$YASH_VERSION"; . ./foo_test'
> 2.51
> 1
> % zsh -c 'echo "$ZSH_VERSION"; . ./foo_test'
> 5.8
> 0
>