Testing for no input.

Hi All

Can anyone let me have a easy way to test if no input was given to a standard read. I would think of using the lenght of the variable, however this seems to be a long cut.

Thx
J

What shell are you using?

KSH is the shell in use

You can try as,

# read value in var variable
read var
if [[ -z $var ]]
then
set var=3
fi

Are you look this one?

I have tried this, however it does not seem to work. I have added a line to echo the value of var however it does not default to 3.

oops. try as,

#!/bin/sh
# read value in var variable
read var
if [[ -z $var ]]
then
let var=3
fi
echo $var

hth.

Perfect... Thanks

Sorry, but I think there is a more elegant way: use the "${....:=...}" variable expansion. The expression "${var:=defaultvalue}" evaluates to the value of $var if $var is non-null (non-zero) or to defaultvalue otherwise. Here is an example:

typeset var="abc"
typeset foo=""
typeset bar=""

foo=${var:="def"}
print $foo              # will yield "abc", as $var was non-null
unset var
bar=${var:="def"}
print $bar              # will yield "def", as $var is not set now

bakunin

Indeed, ${foo:=default} is the most elegant way of doing this (in shells that support it - ksh, bash, et al)

But, muthukumar, I'm curious why you "let var=3" when a simple "var=3" would do?

Cheers
ZB