Getopts in the subshell of ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getopts in the subshell of ksh
# 1  
Old 09-05-2013
Getopts in the subshell of ksh

Hi,

the getopts doesnt seem to be working in the subshell of the ksh. when I echo $@ and $* from the subshell it shows nothing. even when I am capturing the parameters from the outer shell and passing while invoking the file then I am still not getting it properly.

the below code is in the inner script
Code:
while getopts ":f:v:s" opt; do
#        case $opt in
#           "-f") shift;
#                sqltype ="$opt"
#                 echo "OPTION IS $opt"
#       ((sqltype != "c"|"h")) && { printf "%b\n" "${USAGE}";  exit 1; }
#       ;;
#   "-v") vname="$2"
#       ;;
#   "-s") dbstring="$2"
#        ;;
#   *) echo printf "%b\n" "${USAGE}"
#       exit 1
#       ;;
#
#       esac
echo $opt
shift
done

invoking from the outer script like this
Code:
$o=$@
. ./innerscript $o

Invoking the outer script like
Code:
./outerscript -f parameter

I am using ksh here. can some one please help me with this? in the above code its not getting in the while loop itself.

Many thanks!!

Last edited by Don Cragun; 09-05-2013 at 10:54 PM.. Reason: CODE tags; not QUOTE tags!
# 2  
Old 09-05-2013
Remove the # at the start of all of the lines in your while loop. The # and everything following it on a line is treated as a comment; not code to be executed.

PS You also need to change:
Code:
sqltype ="$opt"

to:
Code:
sqltype="$opt"

(no space before the equals sign).

PPS You'll also want to change the first line of outerscript from:
Code:
$o=$@

to:
Code:
o="$@"

(no dollar sign in front of o and double quotes around $@ even though the latter might not make any different with the arguments you're passing in this example).

Last edited by Don Cragun; 09-05-2013 at 10:58 PM.. Reason: Add PPS.
# 3  
Old 09-05-2013
Quote:
Originally Posted by Don Cragun
Remove the # at the start of all of the lines in your while loop. The # and everything following it on a line is treated as a comment; not code to be executed.
I understand that. but my point is its not even getting into the while loop and not displaying the echo statement.
# 4  
Old 09-05-2013
Quote:
Originally Posted by hitmansilentass
I understand that. but my point is its not even getting into the while loop and not displaying the echo statement.
Please see the PPS in my earlier note...
# 5  
Old 09-05-2013
Also, why are the arguments (:) before the options (f, v, s)?
# 6  
Old 09-05-2013
Thanks guy's. I just figured the issue. the code was within a function hence it was not capturing the values. anyway, I am getting another issue now that its not capturing second flag values. do you know why?

Its probably to do with the arguments but its been very long since I have worked on shell scripts so I am still recalling a lot of stuff.

Code:
while getopts "f:v:s" opt; do
echo $opt
        case $opt in
           "f") shift;
                 typeset -l sqltype=$OPTARG
       [[ $sqltype != "c" ]] &&  [[ $sqltype != "h" ]] && { printf "%b\n" "${USAGE}";  exit 1; }
       ;;
   "v") vname="$OPTARG"
       ;;
   "s") dbstring="$OPTARG"
        ;;
   *) echo printf "%b\n" "${USAGE}"
       exit 1
       ;;

        esac
done


Last edited by Scott; 09-05-2013 at 11:35 PM.. Reason: Again, CODE tags not QUOTE tags, please...
# 7  
Old 09-05-2013
You can remove the shift command, it's not required.
Code:
$ cat myScript
while getopts f:v:s opt; do
  case "$opt" in
    f) echo f has $OPTARG;;
    v) echo v has $OPTARG;;
    s) echo s has nothing;;
  esac
done

$ ./myScript -f f_opt -s -v v_opt
f has f_opt
s has nothing
v has v_opt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

2. Shell Programming and Scripting

Using getopts in ksh

I want to use getopts in ksh scripting to obtain multiple values currently im using while getopts vt: opt do case "$opt" in -v) vn=$OPTARG;; -t) tn="$OPTARG";; ;; # terminate while loop esac done however variables vn tn dont store any... (3 Replies)
Discussion started by: E.sh
3 Replies

3. Shell Programming and Scripting

ksh "getopts" -- Unsetting an Option

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its... (4 Replies)
Discussion started by: Matt Miller
4 Replies

4. Shell Programming and Scripting

Help on getopts in ksh

I am trying to make a Shell Script in KSH on Linux box and Solaris box which takes few arguments... with long options using getopts (not getopt). I have my sample code... at the end I will say my requirement... Please help me folks... $ cat dummy #!/bin/bash # Argument = -t test -r server... (6 Replies)
Discussion started by: explorer007
6 Replies

5. Shell Programming and Scripting

Basename in subshell

Hi All, I would like to improve my bash scripting skill and found a problem which I do not understand. Task is to search and print files in directory (and subdirecories) which contains its own name. Files can have spaces in name. This one works fine for files in main directory, but not for... (4 Replies)
Discussion started by: new_item
4 Replies

6. Shell Programming and Scripting

getopts in a subshell

Hello, I've a little problem with one of my ksh scripts and I manage to narrow it to the script here: #!/bin/ksh writeLog() { paramHandle="unknown" OPTIND=1 while getopts :i: option $* do case $option in i) paramHandle=${OPTARG} ;; esac done echo... (2 Replies)
Discussion started by: Dahu
2 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

ksh and getopts

Hello, I'm writing a script that uses command line options and arguments. To handle these, I'm using getopts. Is there a way to set up an option where an argument is optional? What I'm trying to do is offer -v for verbose output, -vv for increased output -vvv etc. Any suggestions? ... (1 Reply)
Discussion started by: garskoci
1 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

Subshell Question

The profile of the user is empty. Then before I run the script I want I run a parameter file that populates the variables for oracle. ORACLE_HOME ORACLE_BASE ORACLE_SID PATH etc ... But it seems that these variables are not making it to the shell I am in because when I do an echo on... (6 Replies)
Discussion started by: lesstjm
6 Replies
Login or Register to Ask a Question