Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done..

But I could not even do following simple task.

How do I Change existing variable value only when user enteres non-empty string. ?

 
#!/usr/bin/ksh
uid="scott"
# Assign new value user enters to uid, else leave it alone.
 
echo "Press Enter to select existing value \r\n"
echo "uid[${uid}]: \c"; read resp;
 
# if user changes uid to something, then only change original value.
if [ ! -n "${resp}" ]; then
  echo "${resp}" | od -bcx
  # I do not want to capture "Enter" or space characters into this.
  uid=${resp}
fi
echo "uid --> ${uid}"

I want to see output with "uid --> scott" in this case.

 
$> test.sh   
Press Enter to select existing value 
uid[scott]: 
0000000 012
          \n
            0a00
0000001
uid --> 

Change

if [ ! -n "${resp}" ]; then

to

if [ ! -z "${resp}" ]; then

and it should work for you.

Murphy, your suggestion worked.

$>  test.sh  
Press Enter to select existing value 

uid[scott]:                                 # Selected Enter here.
uid --> scott

$>  test.sh
Press Enter to select existing value 

uid[scott]: aaaaa
0000000 141 141 141 141 141 012
           a   a   a   a   a  \n
            6161    6161    610a
0000006
uid --> aaaaa

I still could not understand why I can't use [ ! -n string ] instead of [ -z string ] or vice-versa .
Here is what the reference page I am looking at says about these two condition checking flags for ksh.

-z string	       --->         True if length of string is zero
-n string	       --->         True if length of string is non-zero

Can someone explain what are true differences between -z, -n flags !!? Where can I find true reference for this?

Just experiment. For example, the following will also work in your script

if [  -n "${resp}" ]; then

An even better way is to use [] as in

if [[  -n "${resp}" ]]; then

This script works.. But I am not sure why its working..
I thought [ -n string ] is supposed to check for zero length string. But in my case even a uninitialized variable has 1 byte length..

#!/usr/bin/ksh
uid="scott"
# Assign new value user enters to uid, else leave it alone.

echo "Value of \${resp} --> "; echo "${resp}" | od -bcx
echo "Length of \${resp} --> \c"; echo ${resp} | wc -C

echo "Press Enter to select existing value \r\n"
echo "uid[${uid}]: \c"; read resp;

echo "Value of \${resp} --> "; echo "${resp}" | od -bcx
echo "Length of \${resp} --> \c"; echo ${resp} | wc -C

# if user changes uid to something, then only change original value.
if [ -n "${resp}" ]; then
  echo "${resp}" | od -bcx
  # I do not want to capture "Enter" or space characters into this.
  uid=${resp}
fi
echo "uid --> ${uid}"

Can someone explain why every empty variable has following bytes? with byte length 1.?

> test.sh   
Value of ${resp} --> 
0000000 012
          \n
            0a00
0000001
Length of ${resp} -->        1
Press Enter to select existing value 

uid[scott]: 
Value of ${resp} --> 
0000000 012
          \n
            0a00
0000001
Length of ${resp} -->        1
uid --> scott

You can simply use the following parameter expansion which returns $resp if it's not null or $uid otherwise:

uid=${resp:-$uid}

Regards,
Alister

Thaks Alister, that is the right way to do it.
I will also try not to use --> echo ${variable} | wc -C for finding length of string instead use --> echo "${#variable}"

test.sh

#!/usr/bin/ksh
uid="scott"

# Assign new value user enters to uid, else leave it alone.
echo "Press Enter to select existing value \r\n"
echo "uid[${uid}]: \c"; read resp;uid=${resp:-$uid}
 
echo "uid --> ${uid}"

$> test.sh

Press Enter to select existing value 

uid[scott]: 
uid --> scott

Kchinnam, you are seeing the newlines ('\n") in your od output because you are using the echo command to pipe the variable in question to od. The echo command adds a newline to its output by design.

Try the following example

uid="scott"
echo "${uid}" |  od -bcx
printf "${uid}" |  od -bcx

gid=
echo "${gid}" |  od -bcx
printf "${gid}" |  od -bcx

Here is the output from this example

$ ./example
0000000  163 143 157 164 164 012
           s   c   o   t   t  \n
            6373    746f    0a74
0000006
0000000  163 143 157 164 164
           s   c   o   t   t
            6373    746f    0074
0000005
0000000  012
          \n
            000a
0000001
$