Getopts inside a function is not working

Hi All,

I am using geopts inside a function in shell script.

But it is doesnt seem to read the input args and I always gt empty value in o/p.

my code is

http://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/base/pgrs-sm.gif 


This message has not been sent.
#!/bin/ksh IFS=' ' readargs(){ OPTION=$1 OPTIND=1 while getopts "i:n:o" OPTION do case $OPTION in i) input="$OPTARG" ;; n) nput="$OPTARG" ;; o) output="$OPTARG" ;; esac done echo "i is $input o is $output n is $nput\n" } for i in `cat $1` do scriptname=`echo

Draft




This message has not been sent.

Actionshttp://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/resources/clear1x1.gif
Click here to continue working on this message.


 


Thursday, July 25, 2013 11:08 AM


http://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/resources/clear1x1.gifhttp://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/resources/clear1x1.gifhttp://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/resources/clear1x1.gif




#!/bin/ksh
IFS='
'
readargs(){
OPTION=$1
OPTIND=1
while getopts "i:n:o" OPTION
do
        case $OPTION in
                    i)
                       input="$OPTARG"
                       ;;
                    n)
                       nput="$OPTARG"
                       ;;
                    o)
                       output="$OPTARG"
                      ;;

        esac
done
echo "i is $input o is $output n is $nput\n"
}
for i in `cat $1`
do
    scriptname=`echo $i|awk '{print $1}'`
    stringopts=`echo $i|awk '{$1="";print}'`
    echo $scriptname
    readargs $stringopts
done






Data looks like this

>cat file.txt
a.ksh -o hi -i hello -n how
c.ksh -i sam -n mi -o ki

Anything wrong with my code?

This works for me:

#! /bin/sh

function readargs {
    OPTIND=1
    while getopts ":i:o:n:" opts
    do
        case $opts in
        i ) echo $OPTARG ;;
        o ) echo $OPTARG ;;
        n ) echo $OPTARG ;;
        esac
    done
}

while read file args
do
    echo $file
    readargs $args
done < file.txt