> > This will work...
> > cd {directory}
> > ls -t | head -1
> > or
> > ls -t $HOME | head -1
> >
> > Using ksh (on Unix), this next line works to only accept files
> > x=`ls -t | head -1`; if [[ -f $x ]]; then echo $x ;fi
> > except that I could not get this to work with bash as
> > x=`ls -t | head -1`; if [ -f $x ]; then echo $x ;fi
> > since the '-f $x' seems to fail the test. Anyone know why ?
>
> As far as each shell goes those lines are direct equivalents. Is the directory
> that you're working in changing? Only regular files will show up with either
> command.
>
> Tony
I tried this on Linux (bash)
x=`ls -t | head -1`; if [ -f $x ]; then echo $x ; else echo "NO" ;fi
and "NO" kept being printed. I tried just the first part to set x
and then "echo $x" and got a file name 'xxx', so the 'if...' should work
(but it does not). When I tried "if [ -f xxx ]; ..." then it worked.
So for some reason in bash, the '-f $x' is not being interpreted properly.
I even tried on the command line...
# x=xxx
# echo $x
xxx
# if [ -f $x ]
> then echo $x
> else echo NO
> fi
NO
# echo $x
xxx
#
So it still won't work but $x *is* correct.
I gave up and wrote the Perl script instead :)
Thanks... Dan.