default value for variable

I got this problem couple of time. I'm comparing the values of two
variables through unix's test command.

But if any of the variable contains null value, test operator gives
the error.
Can we assign some default value to variable or some better way to do
it.

Please see the code below

=======================================
$SERVER='abc'
cnt=`echo "set nocount on\nselect [at] [at] servername \n\n" | isql -Usa -P
$PASSWORD -S$SERVER -c | grep -v "\-\-\-\-\-"`


if test $cnt -ne $SERVER
then
echo "Problem with the server"
exit
fi
=======================================

if "$cnt" contains null value the code will not work otherwise it
works fine.

Any help is highly appreciated.

SK
surajkumar1 [ Do, 10 April 2008 23:48 ] [ ID #1939285 ]

Re: default value for variable

On 2008-04-10, surajkumar1 [at] gmail.com wrote:
> I got this problem couple of time. I'm comparing the values of two
> variables through unix's test command.
>
> But if any of the variable contains null value, test operator gives
> the error.
> Can we assign some default value to variable or some better way to do
> it.
>
> Please see the code below
>
> =======================================
> $SERVER='abc'

There should be no dollar sign, and the quotes are unnecessary:

SERVER=abc

> cnt=`echo "set nocount on\nselect [at] [at] servername \n\n" | isql -Usa -P
> $PASSWORD -S$SERVER -c | grep -v "\-\-\-\-\-"`
>
>
> if test $cnt -ne $SERVER

You are using integer comparison on non-numeric strings; use '!='
rather than '-ne'.

> then
> echo "Problem with the server"
> exit
> fi
> =======================================
>
> if "$cnt" contains null value the code will not work otherwise it
> works fine.

if test ${cnt:-0} != ${SERVER:-0}

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
cfajohnson [ Fr, 11 April 2008 00:05 ] [ ID #1940019 ]

Re: default value for variable

surajkumar1 [at] gmail.com wrote:
> I got this problem couple of time. I'm comparing the values of two
> variables through unix's test command.
>
> But if any of the variable contains null value, test operator gives
> the error.
> Can we assign some default value to variable or some better way to do
> it.
>
> Please see the code below
>
> =======================================
> $SERVER='abc'
> cnt=`echo "set nocount on\nselect [at] [at] servername \n\n" | isql -Usa -P
> $PASSWORD -S$SERVER -c | grep -v "\-\-\-\-\-"`
>
>
> if test $cnt -ne $SERVER
> then
> echo "Problem with the server"
> exit
> fi
> =======================================
>
> if "$cnt" contains null value the code will not work otherwise it
> works fine.
>
> Any help is highly appreciated.

The most straightforward solution is to surround the arguments with
double-quotes:

if test "$cnt" != "$SERVER"

Note that, as Chris pointed out, the string not-equal operator is !=,
not -ne.

--
Gary Johnson
Gary Johnson [ Fr, 11 April 2008 00:42 ] [ ID #1940022 ]

Re: default value for variable

On 2008-04-10, surajkumar1 [at] gmail.com <surajkumar1 [at] gmail.com> wrote:
<snip>
>
> if test $cnt -ne $SERVER
<snip>

The classic way of avoiding a null argument is to simply concatenate
an arbitrary string to it:

if test x$cnt -ne x$SERVER

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
Chris Mattern [ Fr, 11 April 2008 16:13 ] [ ID #1940046 ]

Re: default value for variable

On Fri, 11 Apr 2008 09:13:15 -0500, Chris Mattern
<syscjm [at] sumire.gwu.edu> wrote:

>The classic way of avoiding a null argument is to simply concatenate
>an arbitrary string to it:
>
>if test x$cnt -ne x$SERVER

My bash does not like those x'es with an -ne arithmetic operator.


--
Webmail for Dialup Users
http://www.isp2dial.com/freeaccounts.html
jak [ Fr, 11 April 2008 19:13 ] [ ID #1940069 ]

Re: default value for variable

In article <slrnfvusfr.99n.syscjm [at] sumire.gwu.edu>,
Chris Mattern <syscjm [at] sumire.gwu.edu> wrote:

> On 2008-04-10, surajkumar1 [at] gmail.com <surajkumar1 [at] gmail.com> wrote:
> <snip>
> >
> > if test $cnt -ne $SERVER
> <snip>
>
> The classic way of avoiding a null argument is to simply concatenate
> an arbitrary string to it:
>
> if test x$cnt -ne x$SERVER

Actually, the way to deal with a null argument is just to quote it, e.g.

test "$cnt" -ne "$SERVER"

The x-prefix hack is used to deal with arguments that may begin with
"-", as they might be interpreted as test options.

--
Barry Margolin, barmar [at] alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
Barry Margolin [ Fr, 11 April 2008 22:19 ] [ ID #1940078 ]
Linux » comp.unix.shell » default value for variable

Vorheriges Thema: perl or sed for string search and replace
Nächstes Thema: character substitution