Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-31-2005, 02:19 PM   #1
LQ Newbie
Registered: Jul 2004
Posts: 8

Rep: Reputation: 0
Silly error: If test condition

Just a small silly question: Below is my silly script but on the 4th line i'm getting an error after i do the 'if input is null' test. Basically i'm asking the user to enter a string, then i'm checking if they've entered an empty string i want to exit else carry on with the script.

echo Enter Name
read name
if [ -z $name] **line 4
then exit

echo Enter mail
read mail
if [ -n $mail]; then exit **line 11

The error i get: line 4: [: missing `]' line 11: [: missing `]'

any help would be great...cheers, Jo
Old 10-31-2005, 02:41 PM   #2
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Is it the placement of your thens?
echo Enter Name
read name
if [ -z $name]; then

echo Enter mail
read mail
if [ -n $mail ]; then 
Old 10-31-2005, 02:48 PM   #3
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,936
Blog Entries: 14

Rep: Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160
It thinks your "]" is part of the variable because you didn't put a space between or encapsulate the variable. Because it uses it for that it is expecting another one to follow but of course you don't want that.


[ -z $name ] # notice new space


[ -z ${name}] # The curly braces tell it that "name" is the whole variable rather than "name]"

Should resolve it.

If all fails you can do the tried and true method of focing it to have a value regardless:

if [ ${name}x = "x" ]

The last just appends the letter x to the variable. If you have no input then there's nothing to append to so it basically test for "x = x".

FYI to Naf: Having then on the next line is perfectly acceptable as is the way you wrote it.
Old 10-31-2005, 03:57 PM   #4
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I'd recommend something like this:

echo Enter Name
read name
if [ -z "$name" ];then 
  exit 1

1. You need a space between "$name" and the right bracket (as has already been pointed out)

2. Just like you should always return something from your C/C++ program (if you declare "int main()"), then
it's generally a good idea to explicitly return a value from your script (at least for an error exit, like this).

3. Finally, I always put the variable (here, $name) in quotes whenever I use "test -z".

Linux/Bash seems to behave fine with or without them, but most other Unix's I've dealt with in the
past tend to crash with a similar "test: missing argument" error if you ever actually give it an empty

Just a suggestion .. PSM


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
'test' error loopoo Linux - Newbie 3 08-08-2005 07:00 AM
Silly squirrelmail error Jukas Linux - Software 9 05-11-2005 12:07 PM
How to test for hardware disk error? Yalla-One Linux - Hardware 12 03-11-2005 02:58 AM
Grub - error 15, i missing something silly Oxy Linux - Software 3 02-22-2004 01:09 AM
same error message in every 2.6 test kernel apberzerk Linux - General 3 11-01-2003 06:16 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 PM.

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