LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-25-2012, 01:39 AM   #1
satya123
Member
 
Registered: Mar 2011
Posts: 31

Rep: Reputation: 0
How to deal with spaces in command line arguments


Hi all,

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?

Thanks in advance
 
Old 05-25-2012, 01:47 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
sure, there is a way, use "SW HEALTH"
in general use "$var" anywhere (and see manuals about quoting)
 
Old 05-25-2012, 02:04 AM   #3
satya123
Member
 
Registered: Mar 2011
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
Old 05-25-2012, 02:11 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by satya123 View Post
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.
 
Old 05-25-2012, 02:12 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
you must call as hc -t "SW HEALTH" -l otherwise it will be split and your script will get it in two pieces.
 
Old 05-25-2012, 02:26 AM   #6
satya123
Member
 
Registered: Mar 2011
Posts: 31

Original Poster
Rep: Reputation: 0
Neither "" nor '' work
 
Old 05-25-2012, 02:29 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
both should work, just you should write "$OPTARG" also otherwise your string will be split.
 
Old 05-25-2012, 02:38 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by satya123 View Post
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<"
                ;;
 
Old 05-25-2012, 04:41 AM   #9
satya123
Member
 
Registered: Mar 2011
Posts: 31

Original Poster
Rep: Reputation: 0
Sorry but even "$OPTARG" does not work. it still gives only SW
 
Old 05-25-2012, 04:56 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by satya123 View Post
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"!
 
Old 05-25-2012, 05:21 AM   #11
satya123
Member
 
Registered: Mar 2011
Posts: 31

Original Poster
Rep: Reputation: 0
Sorry all. It works fine. I was sending the argument to getopts without enclosing it in " ". Now it works fine.
THANK YOU
 
Old 05-25-2012, 05:24 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
great!




_____________________________________
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")
 
Old 05-25-2012, 05:32 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by satya123 View Post
Sorry all. It works fine. I was sending the argument to getopts without enclosing it in " ". Now it works fine.
THANK YOU
Good

Threads can be marked SOLVED via the Thread Tools menu.
 
Old 05-25-2012, 10:32 AM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When you have a chance, read these three links. It's vital in scripting to understand exactly how the shell handles arguments and whitespace:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
Old 05-25-2012, 11:59 AM   #15
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line arguments bioinformatics_guy Linux - Newbie 2 02-12-2009 07:26 AM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 09:40 AM
Command line arguments?? almagerenia Linux - Newbie 1 09-08-2006 04:05 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 02:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration