default value for variable
default value for variable
am 10.04.2008 23:48:58 von surajkumar1
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 @@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
Re: default value for variable
am 11.04.2008 00:05:30 von cfajohnson
On 2008-04-10, surajkumar1@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 @@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
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
Re: default value for variable
am 11.04.2008 00:42:34 von Gary Johnson
surajkumar1@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 @@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
Re: default value for variable
am 11.04.2008 16:13:15 von Chris Mattern
On 2008-04-10, surajkumar1@gmail.com wrote:
>
> if test $cnt -ne $SERVER
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
Re: default value for variable
am 11.04.2008 19:13:24 von jak
On Fri, 11 Apr 2008 09:13:15 -0500, Chris Mattern
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
Re: default value for variable
am 11.04.2008 22:19:07 von Barry Margolin
In article ,
Chris Mattern wrote:
> On 2008-04-10, surajkumar1@gmail.com wrote:
>
> >
> > if test $cnt -ne $SERVER
>
>
> 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@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***