LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-15-2005, 03:49 PM   #1
bwysocki
LQ Newbie
 
Registered: May 2005
Distribution: SuSE
Posts: 9

Rep: Reputation: 0
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..
 
Old 06-15-2005, 03:54 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,467

Rep: Reputation: 153Reputation: 153
"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
 
Old 06-15-2005, 04:11 PM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
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:
Code:
for i in blablabla
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.
 
Old 06-15-2005, 04:27 PM   #4
bwysocki
LQ Newbie
 
Registered: May 2005
Distribution: SuSE
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 06-15-2005, 04:46 PM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
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.
 
Old 06-15-2005, 04:50 PM   #6
bwysocki
LQ Newbie
 
Registered: May 2005
Distribution: SuSE
Posts: 9

Original Poster
Rep: Reputation: 0
Man it's late in the day

It should have read: if [ "$err_level" -gt 0 ]
 
Old 06-15-2005, 05:07 PM   #7
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
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...
 
Old 06-15-2005, 06:36 PM   #8
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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).
 
Old 06-16-2005, 09:11 AM   #9
bwysocki
LQ Newbie
 
Registered: May 2005
Distribution: SuSE
Posts: 9

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally posted by carl.waldbieser
How about:

Code:
if [ -n "$var_to_test" ]; then
...
fi
That did it. Thank you Carl!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: how to read <enter> key linmix Programming 3 12-02-2005 04:38 PM
shell script for stress testing blackzone Programming 3 09-14-2004 12:00 AM
X-Chat script to enter a command every minute eam Programming 2 08-30-2004 03:25 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
setting script to run when system enter init 3 fowlerlfc Linux - Newbie 2 10-29-2002 03:02 PM


All times are GMT -5. The time now is 07:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration