Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
06-15-2005, 03:49 PM
#1
LQ Newbie
Registered: May 2005
Distribution: SuSE
Posts: 9
Rep:
Testing for enter in bash script
This is a simple question.
I'm writing a shell script where I prompt the user for a name. If they hit enter, I want to skip that part of the script. For example...
Code:
echo "Enter owner name:"
read $owner
if [ $owner != "" ]
then
chown $owner *
fi
The problem I'm having is it always goes inside the if block. What should the if be looking for if the user hits the enter key?
Thanks..
06-15-2005, 03:54 PM
#2
Guru
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Squeeze, Fedora 14
Posts: 7,268
Rep:
"The problem I'm having is it always goes inside the if block. What should the if be looking for if the user hits the enter key?"
Check for a non zero return code from read.
----------------------------
Steve Stites
06-15-2005, 04:11 PM
#3
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,782
Rep:
I think your script may create errors. Try this:
Code:
#!/bin/bash
echo "Enter owner name:"
read owner
if [ "$owner" != "" ]
then
echo "The owner is $owner"
fi
I don't think its correct to put $ in read, since a variable is expected. Its the same with for:
not :
Code:
for $i in blablabla
Also , It's better to protect your variables with a " " in if.
I get the feeling that your read does not put anything in owner variable.
06-15-2005, 04:27 PM
#4
LQ Newbie
Registered: May 2005
Distribution: SuSE
Posts: 9
Original Poster
Rep:
Sorry, the "read $owner" was a typo.
Well, checking for the return code works well. In fact it works too well.
Code:
echo "Enter owner"
read owner
err_level=$?
if [ err_level -gt 0 ]
then
chown owner *
fi
If I put a echo $err_level after I set the variable, it always returns a 0. No matter if I enter a name or just hit enter.
06-15-2005, 04:46 PM
#5
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,782
Rep:
Quote:
Originally posted by bwysocki
Sorry, the "read $owner" was a typo.
Well, checking for the return code works well. In fact it works too well.
Code:
echo "Enter owner"
read owner
err_level=$?
if [ err_level -gt 0 ]
then
chown owner *
fi
If I put a echo $err_level after I set the variable, it always returns a 0. No matter if I enter a name or just hit enter.
do you mean
Code:
if [ $err_level -gt 0 ]
I don't know what the reterning value of read should be. It's an internal Bash command.
06-15-2005, 04:50 PM
#6
LQ Newbie
Registered: May 2005
Distribution: SuSE
Posts: 9
Original Poster
Rep:
Man it's late in the day
It should have read: if [ "$err_level" -gt 0 ]
06-15-2005, 05:07 PM
#7
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,782
Rep:
Quote:
Originally posted by bwysocki
Man it's late in the day
It should have read: if [ "$err_level" -gt 0 ]
I'm not trying to look smart. It's just that a mistake like this could be the problem, and I'm not in the position to know
which of those are typos and which not...
06-15-2005, 06:36 PM
#8
Member
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197
Rep:
How about
:
Code:
if [ -n "$var_to_test" ]; then
...
fi
In bash, "help test | less" should show you all the different string tests (as well as all the other tests).
06-16-2005, 09:11 AM
#9
LQ Newbie
Registered: May 2005
Distribution: SuSE
Posts: 9
Original Poster
Rep:
Quote:
Originally posted by carl.waldbieser
How about :
Code:
if [ -n "$var_to_test" ]; then
...
fi
That did it. Thank you Carl!
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 05:34 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News