|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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 Java” Download Now! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
http://www.tldp.org/LDP/abs/html/internal.html#EX33
pan about a page up and read the section on getopts -Steven |
|
#3
|
|||
|
|||
|
Quote:
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. ![]() |
|
#4
|
|||
|
|||
|
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 ![]() |
|
#5
|
||||
|
||||
|
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:
yes, this is good for error checking. thank you. i will try and incorporate it into the program. Quote:
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > how to assign options to scripts ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|