LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 02: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, 02:47 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
sure, there is a way, use "SW HEALTH"
in general use "$var" anywhere (and see manuals about quoting)
 
Old 05-25-2012, 03: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, 03:11 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 03:12 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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, 03:26 AM   #6
satya123
Member
 
Registered: Mar 2011
Posts: 31

Original Poster
Rep: Reputation: 0
Neither "" nor '' work
 
Old 05-25-2012, 03:29 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

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

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 05: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, 05:56 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 06: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, 06:24 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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, 06:32 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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, 11:32 AM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 12:59 PM   #15
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:26 AM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 10:40 AM
Command line arguments?? almagerenia Linux - Newbie 1 09-08-2006 05:05 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 09:43 AM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 03:25 AM


All times are GMT -5. The time now is 11:44 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration