passing integer in getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing integer in getopts
# 1  
Old 05-10-2010
passing integer in getopts

Hi,

I'm writing a shell script where I need to parse the command line arguments using "getopts" command. While using getopts I need to know how to pass numeral values to getopts. For example:

Code:
while getopts ":h" Option
do
        case $Option in
          h     ) display_help;;
        esac
done


In the above example apart from the -h option I also wanted to check if the user has entered any numeral values like 1,2or....

Thanks,
Arun Venktesh.V

Last edited by Scott; 05-10-2010 at 04:43 AM.. Reason: Code tags, please...
# 2  
Old 05-10-2010
This should do the job:
Code:
while getopts ":h" Option
do
    case $Option in
        h     ) display_help ;;
    esac
done
shift $((OPTIND-1))
NUMBER=$1

# 3  
Old 05-10-2010
Frans,

May be I should try to explain the scenario even better, I have the following script:

Code:
#!/bin/ksh
# This script is used to monitor the traffic results from simulator and nodes on a regular interval
# The reports are saved at a common directory

function display_help
{
echo " Usage: \"monitor_traffic intervaltime totaltime\" "
echo " intervaltime - The intervals you want the report to be collected in minutes "
echo " totaltime - The time the whole traffic session runs in hours "
echo " Example: ./monitor_traffic 60 8 (where the reports are collected for every 60 minutes for 8 hours) "
}

if [ "$1" = "" ]
then
display_help
exit 1
fi

while getopts ":h" Option
do
case $Option in
h ) display_help;;
esac
done

shift $(($OPTIND - 1))

exit 0

# Converting the minutes to seconds
interval=`expr $1 \* 60`
totaltime=`expr $2 \* 60 \* 60`
a=`expr $interval`
echo "Test"
while [ $a -le $totaltime ]
do
sleep $interval
echo "Test"
a=`expr $a + $interval`
done

Actually I wanted to incorporate more options to this script, so I'm trying to use the getopts. My requirement is apart from the numeral values I want to check for other options.

Last edited by Scott; 05-10-2010 at 06:43 AM.. Reason: Please use code tags!
# 4  
Old 05-10-2010
Quote:
Originally Posted by arun_maffy
Actually I wanted to incorporate more options to this script, so I'm trying to use the getopts. My requirement is apart from the numeral values I want to check for other options.
That's what i understood.
Therefore, the shift $(($OPTIND - 1)) command sets the first parameter after the options as $1, no matter the number of options.
You must call the script with the usual syntax like
Code:
scriptname -h -x -y 89
# or
scriptname -hxy 89
# where x and y are the other options. Note that you can put arguments to the options. 
scriptname -t 300 # where 300 is the value for the t option.
# Must be written in the script like (suppose you have h, t, x and y)
while getopts ":ht:xy" Option
# Note the ':' after 't' : Option requires an argument
do
    case $Option in
        h     ) display_help;;
        t) T=$OPTARG ;; # puts the argument in variable T
        x) process x ;;
        y) process y ;;
    esac
done
shift $(($OPTIND - 1))

This User Gave Thanks to frans For This Post:
# 5  
Old 05-10-2010
Frans,

This is really helpful...

So in the above case If I'm trying to send scriptname -t "1 2", how to pass this 2 values as input to the function.

Thanks,
# 6  
Old 05-10-2010
Quote:
Originally Posted by arun_maffy
So in the above case If I'm trying to send scriptname -t "1 2", how to pass this 2 values as input to the function
scriptname -t "1 2"
Will pass the string "1 2" as 1 value to the script.
Case A (in getopts you write 't:') : that value will be the $OPTARG for option -t ("1 2" --> $OPTARG)
Case B (you write 't') : that value will be the first parameter after the appropriate shift $((OPTIND-1)) ("1 2" --> $1).

The syntax 'scriptname -t 1 2' gives different results:
case A : 1 --> $OPTARG ; 2 --> $1
case B : 1 --> $1 ; 2 -> $2

Hope this is clear enough. Maybe you could tell what kind of options and parameters you want to pass to the script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

2. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

3. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

4. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

5. Shell Programming and Scripting

getopts help

First off, I apologize for my lack of knowledge. I realize my problem will probably seem pretty basic to everyone, but I've been at this for several hours now and I've gotten nowhere. I would contact my professor, but it is too late for that. Anyway, I'm trying to write a function called... (1 Reply)
Discussion started by: Unknown50862
1 Replies

6. Programming

warning: passing arg 1 of `inet_addr' makes pointer from integer without a cast

I use solaris10,following is tcp client code: #include "cliserv.h" int main(int argc,char argv){ struct sockaddr_in serv; char request,reply; int sockfd,n; if(argc!=2) err_quit("usage: tcpclient <IP address of server>"); if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0) ... (1 Reply)
Discussion started by: konvalo
1 Replies

7. Shell Programming and Scripting

Using getopts

I am having some trouble/questions with getopts that I can't find any solid info on with google I need it to parse things of the syntax of: -r # # # -f -c with as many repeats as possible, and it should catch erroneous commands also, but continue going... my first question is, -r... (3 Replies)
Discussion started by: TurboArkhan
3 Replies

8. HP-UX

using getopts

Is there a restriction on levels of using 'getopts' ? I have several scripts, each of which requires an option as the first parameter . If I call one prg separately it works fine, but when one prg calls another prg and passes the option on the called prg, then the called prg seems not to process... (3 Replies)
Discussion started by: vslewis
3 Replies

9. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies

10. Shell Programming and Scripting

getopts

I have a script which fires a command based on certain parameters. I am posting the code below.. The options needs be given such that -u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d. 1) How do I ensure that user misses any of the option then he should be... (5 Replies)
Discussion started by: yerra
5 Replies
Login or Register to Ask a Question