Find the answer to your Linux question:
Results 1 to 6 of 6
when I pass command line arguments to my script that contains multiple options to a function that uses a getopts function, it will work fine if you have the optargs ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    4

    KSH GetOpts problem

    when I pass command line arguments to my script that contains multiple options to a function that uses a getopts function, it will work fine if you have the optargs in quotes like (-a "arg1 arg2"), but if i am reading the command line args from a txt file, and try to pass them to the function, it doesn't see whats in the quotes as one $optarg it sees: ' "arg1 ' and ' arg2" '. any ideas on how to get around this?

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    If you show us how you're attempting pass the arguments to the function, we'll show you how to quote them properly. How are you representing the multi-word arguments in the text file?

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    4
    if i pass these lines in via command line argument by themselves they work fine, the txt file im reading each line from is as follows, and if i put quotes around $line, it reads the whole thing in and doesnt parse it for the case statement, but without quotes around $line, it works but the ones in quotes arent kept together.

    -S S1 -N N1 -c C1 -s S1 -a R -j "-Twenty -One"
    -S S2 -N N2 -c C2 -s S2 -a R2 -j "-Twenty -Two"
    -S S3 -N N3 -c C3 -s S3 -a R3 -j "-Twenty -Three"

  4. #4
    Just Joined!
    Join Date
    Jun 2008
    Posts
    4
    There seems to be some sort of a problem with the quotes. The quotes behave differently when received from the commandline as opposed to when I read them from a txt file using a { while read -r line; do..... done < txtfile.txt }. I need them to be read as they are from the command line. because getopts uses quotes and spaces (i.e. "-arg1 -arg2") to view as one option.

    so as in the above txt file when getopts goes to the -j option, it should see "-Twenty -One" as one argument for that option. But instead it sees it as -T option w option... -O option.. and throws exception.

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Try using the eval command:

    Code:
    eval getopts $line

  6. #6
    Just Joined!
    Join Date
    Jun 2008
    Posts
    4
    you are genious. Thanks alot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...