UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Avoid common pitfalls of incorporating spreadsheets into Java apps. Read about it in the free white paper: “Five Biggest Blunders when Building Spreadsheet Applications in JavaDownload Now!
  #1  
Old July 15th, 2004, 05:23 AM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how to assign options to scripts ?

okay, i have to write a program that gets two numbers from the user, and prints the numbers in between both numbers out, including the two numbers. that should not be the hard part, i can just assign a while loop to end on the higher variable.

the hard part is that the script is supposed to have two possible options. : -e and -o

if i typed :
$ script1 -o 4 9

it would print out all the odd numbers between 4 and 9.

my question is, how do i assign and define an arbitrary option ?

-ivalice

Reply With Quote
  #2  
Old July 15th, 2004, 06:32 AM
stevengs stevengs is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Germany
Posts: 394 stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 4 h 36 m 24 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
http://www.tldp.org/LDP/abs/html/internal.html#EX33

pan about a page up and read the section on getopts

-Steven

Reply With Quote
  #3  
Old July 17th, 2004, 11:27 AM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by stevengs
http://www.tldp.org/LDP/abs/html/internal.html#EX33

pan about a page up and read the section on getopts

-Steven



Thank you, that page has been a great help.

Another problem I am stuck with, however.
When I "grab" the input from the user, the integers should be stored in $1 and $2. I am trying to write an 'if' statement to compare which integer is larger, so I may proceed with the program. (If one is larger, then incrementing certain variables is in order)

But I get a 'Non-numeric error', when I write:

if [ "$1" -gt "$2" ];
then
#some code in here
fi

I changed the $2 in the 'if' statement to 0, and it runs without the error.

My question is how can I compare two integers that are in $1 and $2 to see which is larger?

-ivalice

edit: btw, the tried string conditions as well, such as ">" and "<" and it still does not work.

Reply With Quote
  #4  
Old July 17th, 2004, 12:12 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,059 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
you are comparing strings
if [ "$1" -gt "$2" ];
and if the user enter less then two strings, you get another error
try something like:
[ $# -le 1 ] && exit 1
here is a numerical comp, so check if it's numerical
case $1 in [0-9]*[0-9]) ;; exit 1;; esac
case $2 in [0-9]*[0-9]) ;; exit 1;; esac
if [ $1 -gt $2 ]
then ....
fi
see man pages 'sh, csh, ksh, test, ...'
i would use an other tool to do this
---- nota
if [ "$1" -gt "$2" ][color red];[/color]
then
#some code in here
fi
and
if [ "$1" -gt "$2" ] ; then
#some code in here
fi
are not the same

Reply With Quote
  #5  
Old July 17th, 2004, 01:45 PM
ivalice ivalice is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 ivalice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
actually they are supposed to compare integers, not strings, but i think i get what you are saying. however, i make sure to enter exactly two integers, for example:

$ ./prog4 5 8
so it should be $1=5 and $2=8
Quote:
[ $# -le 1 ] && exit 1


yes, this is good for error checking. thank you. i will try and incorporate it into the program.

Quote:
case $1 in [0-9]*[0-9]) ;; exit 1;; esac
case $2 in [0-9]*[0-9]) ;; exit 1;; esac
if [ $1 -gt $2 ]
then ....
fi


this is also good error checking, in case the user inputs a letter.

i still need a solution to compare those two numbers however. i think the last bit of code you wrote in your reply, was just to tell me that there is a difference in having a space after the "]" in this statement : "if [ "$1" -gt "$2" ] ; "

i didn't leave a space there. so that should not be a problem.


more help = much thanks !
-ivalice

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > how to assign options to scripts ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway