On Fri, 4 Aug 2000, Pierre Fortin wrote:
> Daniel Woods wrote:
> > > > 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 :)
> 
> Use braces:
> 
> $ x=mysignature
> $ if [ -f ${x} ]
> > then echo ${x}
> > else echo NO
> > fi
> mysignature                                                                          
>                 

Does work (surprised), but did you also try:
    $ x=mysignature
    $ if [ -f ${x} ]; then echo ${x} ; else echo "NO" ;fi
NO

which does *not* work.  I had tried ${x} before your suggestion
and saw it failed, so I'm surprised the multi-line one works.
This also does not work...
    $ x=mysignature
    $ if [ -f ${x} ]; then { echo ${x} }; else { echo "NO" } ; fi
NO

This is definitely one of those scratch-your-head-it-should-work
things which annoys us because it becomes a challenge to fix.

Oh well, on to more important things for now :)

Thanks... Dan.

Reply via email to