LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-07-2011, 01:42 AM   #1
hasmeet.singh337
LQ Newbie
 
Registered: Jan 2011
Posts: 18

Rep: Reputation: 0
Post Error in shell script


hi everybody,
I have created a shell script to check the input number as positive or negative integer and if no argument is found then it should display a message that you must input a argument..

Script is given as:

vi pntest
#!/bin/sh
#
#
#Script to check wether integer is positive or negative
#
#
if [ $# -eq 0]
then
echo "$0 : You must give or supply one integer"
exit 1
fi
if test $1 -gt 0
then
echo "$1 is a positive number"
else
echo "$1 is a negative number"
fi


commands executed on it
chmod 755 pntest
./pntest

output is:

./pntest: line 7: [: missing `]'
./pntest: line 12: test: -gt: unary operator expected
is a negative number
 
Old 03-07-2011, 01:49 AM   #2
Ashkan_s
Member
 
Registered: Jul 2008
Distribution: Fedora
Posts: 77

Rep: Reputation: 22
you need a spcae between 0 and ]
Code:
if [ $# -eq 0]
Code:
if [ $# -eq 0 ]

Last edited by Ashkan_s; 03-07-2011 at 01:54 AM.
 
1 members found this post helpful.
Old 03-07-2011, 02:39 AM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
I tried your program.

Code:
~/foo$ sh pntest
pntest : You must give or supply one integer
~/foo$ sh pntest 5
5 is a positive number
~/foo$ sh pntest -5
-5 is a negative number
~/foo$ sh pntest 0
0 is a negative number
You might want to reconsider whether or not 0 is truly a negative number. (Or is 0 technically not an integer?)

One solution might be to add a third else clause to check for 0 and respond appropriately.

Last edited by Telengard; 03-07-2011 at 02:40 AM.
 
  


Reply



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
[SOLVED] Help with shell script error thebombzen Linux - Software 1 02-14-2011 09:21 PM
shell script error _Linux_Learner Linux - Newbie 11 02-21-2010 11:21 AM
Help With A Shell Script Error PhantomFX Linux - Newbie 3 02-17-2005 04:42 AM
Shell Script Error Gerardoj Linux - General 2 12-03-2003 09:44 AM
Shell script error. chuck77 Linux - General 3 11-29-2001 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration