If else with one variable

Hello,

Can someone tell me please what am missing here :-


echo "Who are you!"

read line

 if [ "$line" != "" ]; then
   echo "Hi $line"
 
else

set line="Kim"

echo " Hi $line"

fi

I need the output to be Hi Kim if no input was giving. I know how to do it if I use two variables , I just want to use one variable (in my code line)

Thank yo

Your requirements are ambiguous. What do you mean by "no input was given"?:

  1. Do you mean that the user just hit <return> without entering any other data?
  2. Do you mean that the user typed in a name, but didn't hit <return>? (If it is this one, how long do you want to wait after a key is pressed before deciding that another key will not be pressed?)
  3. Do you mean that the user did not hit any keys at all? (If this is it the last one, how long do you want to wait before deciding that no response will be given?)

What OS are you using?

What shell are you using?

Haven't understood the question/problem, but maybe this helps:

echo "Who are you?" 
read line 
echo "Hi ${line:=Kim}"

Try

#!/bin/bash

echo "Who are you!"
read line

if [ ! -z "$line" ]; then
	echo "Hi $line"
else
	line="Kim"
	echo " Hi $line"
fi

Don,
I meant when user hit enter without entering any data. Sorry for the confusing. Thanks for helping.

Cochise,
sorry for the confusion. I just need like the following:-
Who you are?
If answer was giving then will print hi answer.
If answer wasn't giving by leaving it blank or hitting enter without entering data then answer should be hi kim. I will try your code and let you know. thanks for help

Akshay,
Looks like what I want , I will run it and let you know. thank you for help

with your logic:

 echo "who are you ?"
read line
if [ "$line" = "" ];then 
line=kim
echo "Hi $line"
else
echo "Hi $line"
fi

Could be shorter..

read -p "Who are you: " line
[ -z "$line" ] && line=kim
echo "Hi $line"

Hope this helps

Although the test [ -z $line ] will work as long as no more than one word was typed in; it isn't doing what most people expect.

If an empty line is entered, it will expand to [ -z ] which will yield TRUE because "-z" is not an empty string (not because $line expands to an empty string).

If a single word is entered (and it does not contain any pattern matching characters that would match more than one file in the current directory), then it will expand to [ -z word ] which will evaluate to TRUE as expected.

If more than one word is entered or any pattern matching characters in a single word expand to a list of more than one filename, you will either get a syntax error or some random result depending on what the 1st word is and, if there are more than two words (after pattern matching), it will also depend on the subsequent words.

To use this form reliably, the line:

[ -z $line ] && line=kim

should be changed to:

[ -z "$line" ] && line=kim

I'm a bit confused now...

Now i belived -z is testing if argument is empty.
Thus, expecting that an empty line (variable in general) returns true.

 ~ $ line=" "
+ ~ $ [ -z "$line" ] && echo empty
 ~ $ [ -z $line ] && echo empty
:( ~ $ 

However, i got the 'multiple expanding part'...

 ~ $ line=$(ls)
:) ~ $[ -z $line ] && echo empty
bash: [: too many arguments
2 ~ $ 

So in conclusion, the quotes around make it less error prone, but it doesnt matter whether or not the variable contains spacechar.
Truely a standards guy you are, me gotta get used again to do quotes around there again :wink:

Thank you Don.

Thank you every one for sharing and helping , all codes which you provided to me working fine , now I'am confused with which one I go with. I like the one provided by sea because it is the shorter one. Thank you Don for the info

As long as you only use bash as your shell, the code sea suggested (with the expansion of $line inside double quotes) will do what you want:

#!/bin/bash
read -p'Who are you: ' line
[ -z "$line" ] && line=kim
echo "Hi $line"

The -p option to the read shell built-in is bash specific. The read -p option in ksh means something very different (read from a co-process), and in many other shells it would just be rejected as an unknown option. The equivalent Korn shell-specific code would be:

#!/bin/ksh
read line?'Who are you: '
[ -z "$line" ] && line=kim
echo "Hi $line"

If you only want to use constructs that will work with any shell that is based on Bourne shell syntax (which will include the default shell on any Linux or UNIX system), try:

#!/bin/sh
printf 'Who are you: '
read line
[ -z "$line" ] && line=kim
echo "Hi $line"

Could be even shorter:

read -p "Who are you: " line
echo "Hi ${line:=Kim}"

Is this sort of parameter expansion a ksh speciality? I wonder why you prefer those awkward if/else constructs...?

No, ${line:=Kim} is not ksh -specific. It is supported by any shell that meets the POSIX requirements for shell parameter expansions. It will not, however, work with a pure Bourne shell (such as /bin/sh on Solaris systems).

Note, however, that as I said before, read -p is bash -specific and will not work with ksh .

Thanks for pointing this out.

To make this complete, the ksh variant would be:

read line?"Who are you: "
echo "Hi ${line:=Kim}"

Are programmers still paid for number of lines of code?

read -p "Who are you: " line
if [ -z $line ];then
 WHO=Kim
 else
 WHO=$line
fi
echo "hi $WHO"

As has already been pointed out in this thread, the above code is error prone and will fail with a syntax error if a user types in something like first last in response to the prompt and the read -p option is a non-standard extension that happens to work with bash , but will not work with a pure Bourne shell nor with a Korn shell.