Your requirements are ambiguous. What do you mean by "no input was given"?:
Do you mean that the user just hit <return> without entering any other data?
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?)
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?)
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
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.
~ $ 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
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:
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"
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 .
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.