LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Testing for enter in bash script (http://www.linuxquestions.org/questions/programming-9/testing-for-enter-in-bash-script-333983/)

bwysocki 06-15-2005 03:49 PM

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..

jailbait 06-15-2005 03:54 PM

"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

perfect_circle 06-15-2005 04:11 PM

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.

bwysocki 06-15-2005 04:27 PM

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.

perfect_circle 06-15-2005 04:46 PM

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.

bwysocki 06-15-2005 04:50 PM

Man it's late in the day :)

It should have read: if [ "$err_level" -gt 0 ]

perfect_circle 06-15-2005 05:07 PM

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...

carl.waldbieser 06-15-2005 06:36 PM

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).

bwysocki 06-16-2005 09:11 AM

Quote:

Originally posted by carl.waldbieser
How about:

Code:

if [ -n "$var_to_test" ]; then
...
fi


That did it. Thank you Carl! :)


All times are GMT -5. The time now is 04:59 AM.