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

Re: nother bash question



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Jun 13, 2016 at 10:19:46AM +0200, Thomas Schmitt wrote:
> Hi,
> 
> Gene Heskett wrote:
> > if test ${InMail} = "gene"
> > bin/mailwatcher: line 66: test: =: unary operator expected
> 
> The syntax problem is most probably about missing "-quotes around
> the variable ecaluation ${InMail} which would have to be empty to
> cause the message:

Agreed.

If I may add something -- one of the most difficult things for shell
novices to wrap their head around is the interaction of command line
expansion and the commands themselves. This is one of the things
which make the shell very powerful [1], but you gotta get used to it.

Let's walk through your problem case (note that I'm restating
what Thomas said. It's just I know it's difficult and bears some
repeating :)

You wrote:

  test ${InMail} = "gene"

Now the shell gets a first chance at it. Suppose ${InMail} is empty.
After variable expansion, you got:

  test = "gene"

(${InMail} was empty, so it gets replaced by nothing). Now that's
what the "if" gets fed with. It responds with "Eek! You told me
to compare *two* things and there's just one!

One could argue "unary operator expected" is a strange way to
restate this. I didn't check, but I guess we're seeing too
deep into the bowels of if's parser.

The solution is, as Thomas said, to put ${InMail} in double quotes.
It almost always is. The better solution is, of course: always
try to mentally follow what happens: first, expansion; then command.

regards

[1] "expressive" might be the more correct term.

- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAldeexMACgkQBcgs9XrR2kZavQCdFzPmHOqAeLeBRDiEQvkRN0i/
2ksAoIEiv3jza1zaUih7nHxomteT0hHr
=+rsl
-----END PGP SIGNATURE-----


Reply to: