Getopts inside a function is not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getopts inside a function is not working
# 1  
Old 07-25-2013
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
Code:
Image 


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.

ActionsImage
Click here to continue working on this message.


 


Thursday, July 25, 2013 11:08 AM


ImageImageImage




#!/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
Code:
>cat file.txt
a.ksh -o hi -i hello -n how
c.ksh -i sam -n mi -o ki

Anything wrong with my code?

Last edited by jim mcnamara; 07-25-2013 at 08:09 AM..
# 2  
Old 07-25-2013
This works for me:
Code:
#! /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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

3. Shell Programming and Scripting

If loop inside function not working.

check_deplver () { dir=/abc/def/ghi if ssh -o StrictHostKeychecking=no $1 "" 2> /dev/null then echo " output is " ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc" 2> /dev/null else echo " directory not presnt" fi } This is not working. But... (7 Replies)
Discussion started by: NarayanaPrakash
7 Replies

4. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

5. Programming

Changing value inside function

I have the following code and want to update shiftDesc inside the function. Is it correct to declare the argument as: int shiftDesc void prValue_vd( FILE* stream, // name of output stream int shift, // amount of shift to the right const char* value,... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

getopts function in Perl

Hi All I have searches getopts function in Perl a lot, but yet i didn't cleared with it. First I want to know what is the meaning of getopts('t:c:', \%options); and please explain getopts function in an easy way.. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

7. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

8. Shell Programming and Scripting

value inside a function

i have a script like this #!/bin/ksh initialize() { x=20 } ... ... ... x=10 initialize; echo $x (2 Replies)
Discussion started by: trichyselva
2 Replies

9. Shell Programming and Scripting

ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse... (4 Replies)
Discussion started by: lyonsd
4 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question