Reading command options one by one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading command options one by one
# 1  
Old 02-14-2012
Reading command options one by one

Hi,

Just some questions on the script below...?

Given: bash-2.03$ command -a option1 name1 name2
Code:
ParseOptions()
{
    local Len=${#@}
    local Ctr=2 #always start at 2
    local Name=()
    local Iter=0
    while [ $Ctr -lt $Len] ; do
        if [ $Ctr -eq "-a" ]            <- Is this correct? so I can get the $2 ("-a")
            Ctr=$(($Ctr + 1))
            local Option=$Ctr         <- Is this correct?
        else
            Name[$Iter]=$Ctr         <- Is this correct?
            Iter=$(($Iter + 1))
        fi
        Ctr=$(($Ctr + 1))
    done
}

Is there a better way to do this?
Note that the command may or may not have options, and the options can be interchanged (ex. bash-2.03$ command name3 -a option2)


also I need to check for this:
Code:
bash-2.03$ command name3 -a option2 name2

-> Should I make this an error?
kindly advise.


Thanks!

Last edited by Franklin52; 02-15-2012 at 03:32 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-14-2012
Refer to the bash documentation for information on how to use the getopts builtin.

Regards,
Alister
# 3  
Old 02-14-2012
Quote:
Originally Posted by alister
Refer to the bash documentation for information on how to use the getopts builtin.

Regards,
Alister
hmm ok..
but upon checking I think I cannot use it I don't have a '-<letter> option for the
names...and the names can be either before or after the '-a' option..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ls command options

Hi, If I want to list files with names containing a certain letter like " a " using just one ls command, is there any way of doing that? Note that it is containing a letter instead of one of the following (starting, ending with a letter or having the letter in between). what I want is to show... (1 Reply)
Discussion started by: AAAnni
1 Replies

2. Shell Programming and Scripting

Help executing command with options

Hi, I have this command in a shell script and I can get it to echo ok, but when I try to execute the command I get a "file not found" error. Which is strange because, if I copy and paste the same command at the cli it works ok. What am I doing wrong please? (16 Replies)
Discussion started by: bbbngowc
16 Replies

3. Programming

Reading long options in C++ program

I am reading arguments passed to a C++ program which accepts long options. Long options start with '--', with the value joined with the option by an = sign, with no intervening spaces. An example is as follows: programName --vdz=15.0 I want to store 'vdz' in variable 'key', whereas... (4 Replies)
Discussion started by: kristinu
4 Replies

4. Shell Programming and Scripting

Reading command line options from bash script

I have the following code and I am calling it using ./raytrac.bash -u and getting problems. For some reason opt_usage is still 0. opt_usage=0 iarg=0 narg=$# while (($iarg < $narg)) do (( iarg = $iarg + 1 )) arg=$argv usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/... (22 Replies)
Discussion started by: kristinu
22 Replies

5. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

6. Shell Programming and Scripting

Help with shell script to run the commands reading options from local file

I have to use shell script to run series of commands on another unix box by connecting through SSH and giving user credentials. For running commands on remote machine I have to use options reading from a local file. Process: Connecting to remote unix server <host1.ibm.com> through ssh Login: ... (2 Replies)
Discussion started by: itsprout
2 Replies

7. Shell Programming and Scripting

Restricting the ls command options

Hi I want the 'ls' command to display only the file size,date modified and name of the file.What i could see with different options is this: $ls -got packagecount.csv $-rwxrwxrwx 1 393137 Aug 21 14:46 packagecount.csv Now what should be my possible... (4 Replies)
Discussion started by: sushovan
4 Replies

8. HP-UX

Linux - HP UX Command options

Just I gone with the script, I found some command's options which are not compatible with " HP-UX ". If I found any alternate commands to the following, most probably I will solve the issue here. 1. " iostat -x " --> this command's option( x ) is not available in HP-UX... (2 Replies)
Discussion started by: pk_eee
2 Replies

9. Shell Programming and Scripting

how to? launch command with string of command line options

my description from another thread... here's my code: #!/bin/bash IFS=$'\n' function OutputName() { input=$1 echo $input input=`echo "$input" | sed -e 's/.//'` input=`echo "$input".avi` output_name=$input } if ]; then echo... (5 Replies)
Discussion started by: TinCanFury
5 Replies

10. UNIX for Advanced & Expert Users

Split Command options

HI! All iam using Split command to split a large .txt file in to smaller files, The syntax iam using split -25000 Product.txt iam getting four output files but not in .txt format but in some other format , when i checked the properties the Type of the output files is Type can any... (7 Replies)
Discussion started by: mohdtausifsh
7 Replies
Login or Register to Ask a Question