Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a script which can be called from command line as:
hc -t SW HEALTH -l
The argument for -t option is "SW HEALTH". But when I use getopts and use $OPTARG for getting the argument, I get only SW as the output.
Is there a way I can deal with the space and get the entire option?
Im sorry.. Maybe I was not clear.
I call the script as:
hc -t SW HEALTH -l
Inside the script, I parse the arguments as:
while getopts t:l option
do
case $option in
t) # Check based on TYPE of requirement
type=$OPTARG
;;
l) # Run command in local mode
LOCAL=1
;;
esac
done
SO my problem is:
I am expecting variable type would contain SW HEALTH. But it contains only SW. It does not consider the OPTARG with space.
Im sorry.. Maybe I was not clear.
I call the script as:
hc -t SW HEALTH -l
As pan64 suggested, you need to quote SW HEALTH:
Code:
hc -t "SW HEALTH" -l
I have shown double quotes because they are easier to see. If there's nothing in the string for the shell to substitute and the string does not contain a single quote (as is the case with SW HEALTH) then single quotes would be more appropriate.
Im sorry.. Maybe I was not clear.
I call the script as:
hc -t SW HEALTH -l
Inside the script, I parse the arguments as:
while getopts t:l option
do
case $option in
t) # Check based on TYPE of requirement
type=$OPTARG
;;
l) # Run command in local mode
LOCAL=1
;;
esac
done
SO my problem is:
I am expecting variable type would contain SW HEALTH. But it contains only SW. It does not consider the OPTARG with space.
Please use CODE tags (most easily by going into Advanced mode and using the # button) when posting code.
What is the output when you add this debugging command
Code:
t) # Check based on TYPE of requirement
type=$OPTARG
echo "DEBUG: type: >$OPTARG<"
;;
Sorry but even "$OPTARG" does not work. it still gives only SW
Is the output of echo "DEBUG: type: >$OPTARG<" "DEBUG: type: >SW<"?
If so and you are using hc -t "SW HEALTH" -l, then you could try
Code:
type="$OPTARG"
Some older shells required that.
It would be helpful if you could copy and paste from your command prompt session into this thread (ideally within CODE tags) then we could see exactly what you are doing and what the output is. Much more useful than "does not work"!
_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
While its all worked out fine, I don't think the main point was addressed.
Your script can't take account of spaces in a single argument if you don't have control of the input, although if your script only takes one argument, then you could read all the arguments and then concatenate them.
Generally It's easier to throw an error if you get something unexpected.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.