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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change existing variable value only user enters non-empty string.
# 1  
Old 05-18-2010
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. ?


Code:
 
#!/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.

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

# 2  
Old 05-19-2010
Change
Code:
if [ ! -n "${resp}" ]; then

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

and it should work for you.
# 3  
Old 05-19-2010
Murphy, your suggestion worked.

Code:
$>  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.

Code:
-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?

Last edited by kchinnam; 05-19-2010 at 08:02 PM.. Reason: formatting adjustments
# 4  
Old 05-19-2010
Quote:
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
Code:
if [  -n "${resp}" ]; then

An even better way is to use [[ ]] as in
Code:
if [[  -n "${resp}" ]]; then

# 5  
Old 05-19-2010
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..

Code:
#!/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.?

Code:
> 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


Last edited by kchinnam; 05-19-2010 at 10:31 PM.. Reason: elaboration
# 6  
Old 05-20-2010
You can simply use the following parameter expansion which returns $resp if it's not null or $uid otherwise:
Code:
uid=${resp:-$uid}

Regards,
Alister
# 7  
Old 05-20-2010
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
Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change date time stamp of existing file

I have a file hello.txt which was created today (today's date timestamp) I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now. uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v Can you please suggest a easy way to do that ? (12 Replies)
Discussion started by: mohtashims
12 Replies

2. Shell Programming and Scripting

Script stops running after assigning empty string for a variable

Hi, This is the first time I see something like this, and I don't why it happens. Please give me some help. I am really appreciate it. Basically I am trying to remove all empty lines of an input.. #!/bin/bash set -e set -x str1=`echo -e "\nhaha" | grep -v ^$` #str2=`echo -e "\n" |... (4 Replies)
Discussion started by: yoyomano
4 Replies

3. Shell Programming and Scripting

How do I change a variable to something only if it's empty?

I feel like it is just a matter of using the $ operators correctly, but I can't seem to get it... hostname="network" ip="192.168.1.1" netmask="" variables=( $hostname $ip $netmask ) for var in ${variables} do if ; then $var="--" fi done echo... (7 Replies)
Discussion started by: etranman1
7 Replies

4. Shell Programming and Scripting

How to change last character in string with a variable value.

Hey Guys, I have text such as this. 28003,ALCORN,2 28009,BENTON,2 28013,CALHOUN,2 28017,CHICKASAW,2 47017,CARROLL,2 05021,CLAY,0 The last digit after the final "," is a variable value. This is the base file. I have to do further execution on this file later and I need to update the... (7 Replies)
Discussion started by: chagan02
7 Replies

5. Shell Programming and Scripting

awk: change string variable

Hi. How to change string variable in awk? for example, I parse with awk script text file named some_name_with_extension.txt I want to print only some_name in my script .... varCompName = FILENAME print varCompName How to put not all symbols from FILENAME to variable? thank you This... (4 Replies)
Discussion started by: cintlt
4 Replies

6. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

7. Solaris

Add existing user into an existing group

Pre: no gpasswd/adduser there is just usermod can be used, also there is no -a option for usermod. How should I add a user into a group? (4 Replies)
Discussion started by: a2156z
4 Replies

8. UNIX for Dummies Questions & Answers

New Variable with Existing variable( Urgent)

hi all, i want use the variable value as a new variable name. print output of new variable. for i in COMPUTER1 COMPUTER2 do flag_name=${i}_FLAG eval ${flag_name}=123 echo $i'_FLAG' done output is COMPUTER1_FLAG COMPUTER2_FLAG i need output as 123 123 (2 Replies)
Discussion started by: arvindng
2 Replies

9. UNIX for Dummies Questions & Answers

how can i change the date of an existing file

Hi, Plz suggest me how can i change the date of a file. Suppose my file has been created in some date and i want to give it present date. How can i do this???? (2 Replies)
Discussion started by: adityam
2 Replies

10. Shell Programming and Scripting

if test for empty and non-existing file

How to write this condition in ksh? if myfile is empty or myfile does not exist then do action1 fi is this OK? if ] -o ] then then do action1 fi Thanks. (3 Replies)
Discussion started by: GNMIKE
3 Replies
Login or Register to Ask a Question