Using Getopt Option


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using Getopt Option
# 1  
Old 04-08-2010
Using Getopt Option

Hi

I need to use getopt option and I have no idea what it is or how to use it.

I need to use it on this awk script:

Code:
awk -F, -v cellid="$1" -v paramval="$2" -v oldfile="$3" -v newfile="$4" '$2==cellid{$3=newvalue}1' OFS="," $3 > $4

I tried reading up on it but I just confuse myself more.

Please help.

---------- Post updated at 07:10 AM ---------- Previous update was at 06:40 AM ----------

Here is my code so far:

Code:
awk -F, -v cellid="$1" -v paramval="$2" -v oldfile="$3" -v newfile="$4" '$2==cellid{$3=newvalue}1' OFS="," $3 > $4
function displayUsage()
{
                echo "Usage: [--usage]";
}
function displayHelp()
{
                displayUsage
                echo ""
                echo " -v       variable"
                echo " -i       cellid"
}
while getopts "vi:" option
do
        case $option in
                v )
                        echo "-v variable"
                        ;;
                i )     echo "-i cellid"
                        ;;
                u )     displayUsage;;
                h )     displayHelp;;
        esac
done

Can getopt simply display what the variable is? Like a definition of the variable?
# 2  
Old 04-08-2010
Quote:
Originally Posted by ladyAnne
Hi

I need to use getopt option and I have no idea what it is or how to use it.
The idea behind getopt is, instead of having the first argument always be the source, the second argument always the destination, the third a conversion type, the fourth a debug file, the fifth just a flag, etc, etc, etc, your script would take parameters like:
Code:
myscript -i source -o dest -c conv -d debug

...in any order, so this would have the same effect:
Code:
myscript -d debug -o dest -i source -c conv

There's a scripting builtin for this called getopts. Here's an example with two arguments to expand however you wish. The -i argument expects a string, the -f argument does not.

Code:
#!/bin/bash

INPUT="default"
FLAG=""

while getopts ':i:f' OPT
do
        case "${OPT}" in
        f)      FLAG=1
                ;;
        i)      INPUT="${OPTARG}"
                ;;
        ?)      echo "Unknown argument"
                exit 1
                ;;
        esac
done

echo "FLAG=${FLAG} INPUT=${INPUT}"

Note the string ":i:f". The very first : tells it to fill in OPTARG when its supposed to. The : following i tells it that i expects an argument. There's no : following f, meaning f expects no argument.

Code:
$ ./opt.sh -f -iasdf
FLAG=1 INPUT=asdf
$ ./opt.sh -f -i asdf
FLAG=1 INPUT=asdf
$ ./opt.sh -i asdf -f
FLAG=1 INPUT=asdf
$ ./opt.sh -iasdf -f
FLAG=1 INPUT=asdf
$ ./opt.sh -f
FLAG=1 INPUT=default
$ ./opt.sh -f -q
Unknown argument
$

This script can then process arguments in any order the same way. You're being asked to build a simple shell wrapper around your awk script to do the same thing I believe.
# 3  
Old 04-09-2010
Thank you for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding another option to getopt

I am trying to code for the addition of a new argument to the command line, the option D to a code that already has ABC (below). When I use make to compile it, it displays: invalid option --D. I did define the global d variable, as well as initialized it inside the main function of the C code. I... (9 Replies)
Discussion started by: Alabama
9 Replies

2. Shell Programming and Scripting

getopt invalid option selection

What is the significance of the *) and ?) in the below code. while getopts a:b:c:he opt do case $opt in a) _name="$OPTARG";; b) _project="$OPTARG";; c) line="$OPTARG";; e) _cmd="XX";; h) Projects=1;; *) echo "$OPTARG is an invalid option"; my_exit 1;; ?)... (3 Replies)
Discussion started by: posix
3 Replies

3. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

4. Shell Programming and Scripting

--alternative option in getopt

Hi, i need to use --alternative option of getopt for ex . getopt -o a:c: --alternative pw: -- "$@" if i use like this, i am not getting any output.Please help me how to correct this.i need to have a combination of long and short options.But long options have to begin with - and not... (0 Replies)
Discussion started by: padmisri
0 Replies

5. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

6. Shell Programming and Scripting

getopt help

:) Can anybody help me about how to use getopt in shell scripting. (3 Replies)
Discussion started by: darshakraut
3 Replies

7. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

8. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

9. Shell Programming and Scripting

getopt

#!/bin/sh set -- `getopt "abco:" "$@"` a= b= c= o= while : do case "$1" in -a) a=1;; -b) b=1;; -c) c=1;; -o) shift; o="$1";; --) break;; esac shift done shift # get rid of -- # rest of script... # e.g. ls -l $@ (6 Replies)
Discussion started by: Hitori
6 Replies

10. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question