LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 07-20-2006, 11:57 AM   #1
prospekrisal
LQ Newbie
 
Registered: May 2006
Location: Kuching, Malaysia
Posts: 26

Rep: Reputation: 15
need correction for shell script


even i enter 3 number this message is always displayed "Enter 3 number please"

here is the code


echo enter 3 numbers
read n1 n2 n3
if test "$n1" -o -z "$n2" -o -z "$n3"
then
echo "Enter 3 number please"
elif [ $n1 -ge $n2 -a $n1 -a $n3 ]
then
echo $n1 is maximum number
elif [ $n2 -ge $n1 -a $n2 -a $n3 ]
then
echo $n2 is maximum number
elif [ $n3 -ge $n1 -a $n3 -a $n2 ]
then
echo $n2 is maximum number
fi
 
Old 07-20-2006, 12:13 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

This:

if test "$n1" -o -z "$n2" -o -z "$n3"

Should be:

if test -z "$n1" -o -z "$n2" -o -z "$n3"

But there are some other 'strange' things:

$n1 -ge $n2 -a $n1 -a $n3 => Why the double -a check? Are you really checking if the could be files(-a file = True if file exists)?

I need to start assuming to give further advise, I'll just wait if you need any further advise.

Hope this helps.
 
Old 07-20-2006, 12:34 PM   #3
prospekrisal
LQ Newbie
 
Registered: May 2006
Location: Kuching, Malaysia
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna

But there are some other 'strange' things:

$n1 -ge $n2 -a $n1 -a $n3 => Why the double -a check? Are you really checking if the could be files(-a file = True if file exists)?

Hope this helps.
it's wrong. I've fixed it. But I fount other strange things.. If I enter
3333 5 2563584 the ,maximum number is 5

how can?
 
Old 07-20-2006, 12:43 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Yep found that out myself too. The logic is not correct. Here's a working version:

Code:
#!/bin/bash

echo "enter 3 numbers"
read n1 n2 n3

if [ -z "$n1" -o -z "$n2" -o -z "$n3" ]
then
  echo "Enter 3 numbers please."
elif [ $n1 -ge $n2 ] && [ $n1 -ge $n3 ] # Both must be true
then
  echo $n1 is maximum number
elif [ $n2 -ge $n3 ] # now we only need to check if n2 >= n3
then
  echo $n2 is maximum number
else # If we get here, n3 must be it.
  echo $n3 is maximum number
fi
If parts of the code are unclear, don't hesitate to ask.
 
Old 07-20-2006, 01:03 PM   #5
prospekrisal
LQ Newbie
 
Registered: May 2006
Location: Kuching, Malaysia
Posts: 26

Original Poster
Rep: Reputation: 15
I got the logic,,,, thanksss
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 07:29 AM
Shell script - correction srnkumar Linux - Newbie 1 04-14-2006 01:19 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration