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

Re: IF statements on the command line in bash



"Rodolfo J. Paiz" wrote:

>  From the "I must have missed something" department:
>
> I often find myself needing to do simple, short if's or for's from the
> command line. However, I can't seem to get the hang of it. As an example,
> these two don't work:

>
> $ for i in 21 22 23 24 25 26 27 28 29 30 ; echo "Sep $i" ; done
> $ if [ -x /etc/hosts ] ; echo "File exists." ; done

Assuming you are running a bourne compatible shell.....

The first line has incorrect syntax.
Should be.

for i in 21 22 23 24 25 26 27 28 29 30 ; do echo "Sep $i" ; done

The second line you probably want to use the -f flag since /etc/hosts is not
executable.
Also the syntax seems incorrect.  Try the next...

if [ -f /etc/hosts ] ; then echo "File exists."; fi

or simply use..

test -f /etc/hosts && echo "File exists."

Please keep in mind that '[' is really the 'test' program in disguise... so
if [ -f /etc/hosts ] ; then echo "File exists."; fi

is really

if test -f  /etc/hosts ; then echo "File exists." ; fi

Check the following....

man sh  ... or  man bash
man test




>
>
> Can't find what's wrong from the man page; is it that you can't do this
> from the CLI, or am I doing something wrong?
>
> --
> Rodolfo J. Paiz
> rpaiz indahaus com
>
> _______________________________________________
> Redhat-list mailing list
> Redhat-list redhat com
> https://listman.redhat.com/mailman/listinfo/redhat-list

--
___________________________________________________________________________
Paul R. Brandariz           E-mail Internet: brandari lore kla-tencor com
KLA-Tencor  Corporation
One Technology Dr.
Milpitas, CA 95035







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