LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-31-2005, 03:19 PM   #1
shalomajay
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.


******************
cat test.sh
pwd
echo Enter Name
read name
if [ -z $name] **line 4
then exit
fi

more cantor.sh
echo Enter mail
read mail
if [ -n $mail]; then exit **line 11
fi

pwd
******************
The error i get:
test.sh: line 4: [: missing `]'
test.sh: line 11: [: missing `]'

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

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

more cantor.sh
echo Enter mail
read mail
if [ -n $mail ]; then 
    exit
fi
 
Old 10-31-2005, 03:48 PM   #3
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,027
Blog Entries: 5

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
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.

Either:

[ -z $name ] # notice new space

or:

[ -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, 04:57 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

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

Code:
pwd
echo Enter Name
read name
if [ -z "$name" ];then 
  exit 1
fi
Why?

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
string

Just a suggestion .. PSM
 
  


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


All times are GMT -5. The time now is 11:47 PM.

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