When scripting, which is better?

Lists: pgsql-hackers
From: Justin Clift <justin(at)postgresql(dot)org>
To: PostgreSQL Hackers Mailing List <pgsql-hackers(at)postgresql(dot)org>
Subject: When scripting, which is better?
Date: 2001-10-01 13:37:37
Message-ID: 3BB871A1.948C5AA1@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi all,

Reading through the script files again, there seems to be several
different methods of doing the same thing :

i.e. if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ];
then

or

if [[ -x "$self_path/postmaster" && -x "$self_path/psql" ]]; then

if [ x"$foo" = x"" ]; then

or

if [ "$op" = "" ]; then

or

if [ "$foo" ]; then

--
"My grandfather once told me that there are two kinds of people: those
who work and those who take the credit. He told me to try to be in the
first group; there was less competition there."
- Indira Gandhi


From: "Ken Hirsch" <kenhirsch(at)myself(dot)com>
To: "Justin Clift" <justin(at)postgresql(dot)org>, "PostgreSQL Hackers Mailing List" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: When scripting, which is better?
Date: 2001-10-01 14:59:10
Message-ID: OE53oE1pbzE3zLEuPB00000779b@hotmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Justin Clift wrote:

> if [ x"$foo" = x"" ]; then

This is the safest way. It prevents problems when $foo begins with with a
"-"

I don't know about your first question, though.


From: Adrian Phillips <adrianp(at)powertech(dot)no>
To: Justin Clift <justin(at)postgresql(dot)org>
Cc: PostgreSQL Hackers Mailing List <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: When scripting, which is better?
Date: 2001-10-01 15:15:14
Message-ID: 87d747tmrh.fsf@grannyogg.localnet
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

>>>>> "Justin" == Justin Clift <justin(at)postgresql(dot)org> writes:

Justin> if [ x"$foo" = x"" ]; then

Justin> or

Justin> if [ "$op" = "" ]; then

Justin> or

Justin> if [ "$foo" ]; then

I'm not the slightest bit a shell expert, but why not :-

if [ -z "$foo" ]; then

Is this POSIX/SUS2/whatever ?

Sincerely,

Adrian Phillips

--
Your mouse has moved.
Windows NT must be restarted for the change to take effect.
Reboot now? [OK]


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Justin Clift <justin(at)postgresql(dot)org>
Cc: PostgreSQL Hackers Mailing List <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: When scripting, which is better?
Date: 2001-10-01 16:46:32
Message-ID: 200110011646.f91GkWP05617@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> Hi all,
>
> Reading through the script files again, there seems to be several
> different methods of doing the same thing :
>
> i.e. if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ];
> then

The above semicolon is useless. Actually, I have never see this. The
normal way is:

if [ -x "$self_path/postmaster" -a -x "$self_path/psql" ]

>
> or
>
> if [[ -x "$self_path/postmaster" && -x "$self_path/psql" ]]; then

I usually do:

if [ ... ]
then

Pretty simple.

>
>
>
>
> if [ x"$foo" = x"" ]; then
>
> or
>
> if [ "$op" = "" ]; then

This is done if you think $op may have a leading dash.

>
> or
>
> if [ "$foo" ]; then
>

This tests whether "$foo" is not equal to "".

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026


From: Justin Clift <justin(at)postgresql(dot)org>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: PostgreSQL Hackers Mailing List <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: When scripting, which is better?
Date: 2001-10-02 08:56:10
Message-ID: 3BB9812A.8BE56363@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Sorry guys,

I didn't realise I actually sent this, it was part of an email I was
putting together to achieve consistency in the scripts, but I thought I
cancelled it when it got late in the morning.

My apologies.

Regards and best wishes,

Justin Clift

Bruce Momjian wrote:
>
> > Hi all,
> >
> > Reading through the script files again, there seems to be several
> > different methods of doing the same thing :
> >
> > i.e. if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ];
> > then
>
> The above semicolon is useless. Actually, I have never see this. The
> normal way is:
>
> if [ -x "$self_path/postmaster" -a -x "$self_path/psql" ]
>
> >
> > or
> >
> > if [[ -x "$self_path/postmaster" && -x "$self_path/psql" ]]; then
>
> I usually do:
>
> if [ ... ]
> then
>
> Pretty simple.
>
> >
> >
> >
> >
> > if [ x"$foo" = x"" ]; then
> >
> > or
> >
> > if [ "$op" = "" ]; then
>
> This is done if you think $op may have a leading dash.
>
> >
> > or
> >
> > if [ "$foo" ]; then
> >
>
> This tests whether "$foo" is not equal to "".
>
> --
> Bruce Momjian | http://candle.pha.pa.us
> pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
> + If your life is a hard drive, | 830 Blythe Avenue
> + Christ can be your backup. | Drexel Hill, Pennsylvania 19026

--
"My grandfather once told me that there are two kinds of people: those
who work and those who take the credit. He told me to try to be in the
first group; there was less competition there."
- Indira Gandhi


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Justin Clift <justin(at)postgresql(dot)org>
Cc: PostgreSQL Hackers Mailing List <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: When scripting, which is better?
Date: 2001-10-02 18:53:48
Message-ID: Pine.LNX.4.30.0110022035210.796-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Justin Clift writes:

> i.e. if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ];
> then
>
> or
>
> if [[ -x "$self_path/postmaster" && -x "$self_path/psql" ]]; then

I don't think the second one is a valid expression. ;-)

Maybe you were wondering about [[ ]] vs [] -- In Autoconf [] are the quote
characters so you have to double-quote, sort of. It's better to use
'test' in that case because m4 quoting can be tricky. I prefer test over
[] in general because it is more consistent and slightly clearer.

> if [ x"$foo" = x"" ]; then

Maximum safety for the case where $foo starts with a dash. Yes, that
means all comparisons should really be done that way. No, I don't think
we should do it in all cases if we know what $foo can contain, because
that makes code *really* unreadable.

> or
>
> if [ "$op" = "" ]; then
>
> or
>
> if [ "$foo" ]; then

These two are equivalent but the second one is arguably less clear.

--
Peter Eisentraut peter_e(at)gmx(dot)net http://funkturm.homeip.net/~peter