Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page KSH GetOpts problem
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-16-2008   #1 (permalink)
anyk3y
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?
anyk3y is offline   Reply With Quote
Old 06-17-2008   #2 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 982
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?
scm is offline   Reply With Quote
Old 06-17-2008   #3 (permalink)
anyk3y
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"
anyk3y is offline   Reply With Quote
Old 06-17-2008   #4 (permalink)
anyk3y
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.
anyk3y is offline   Reply With Quote
Old 06-17-2008   #5 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 209
Try using the eval command:

Code:
eval getopts $line
vsemaska is offline   Reply With Quote
Old 06-17-2008   #6 (permalink)
anyk3y
Just Joined!
 
Join Date: Jun 2008
Posts: 4
you are genious. Thanks alot!
anyk3y is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:59 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0