The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Add single quotes in string mrjunsy UNIX for Dummies Questions & Answers 1 07-18-2008 07:09 AM
Awk:Find length of string omitting quotes jayakumarrt Shell Programming and Scripting 2 05-09-2008 12:48 AM
simple question on string concat the_learner High Level Programming 2 08-23-2007 08:09 PM
problem with single quotes in a string and findbug bob122480 Shell Programming and Scripting 9 01-19-2007 07:38 AM
concat string mpang_ Shell Programming and Scripting 1 07-25-2006 03:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-22-2008
Registered User
 

Join Date: Jul 2008
Posts: 4
Stumble this Post!
String concat that keeps quotes

Hi All,

I hope you can help. i am concatenating String variables using the following method.
Code:
command="$command$x"
i have created a script which takes a set of args passed to the script using the $*


Code:
#example script
args=$*
count=0
for x in $args 
do
    count=`expr $count + 1`
    if [ "$count" -gt "3" ] 
        then
            command="$command$x"
    fi
done

echo $command
this seems to work if i write
Code:
example this is a test to see if it works
it should output
test to see if it works

Now i am finally coming to my question
if i write

Code:
example this is a test to "see if" it works
it still prints out
test to see if it works
it always seems to remove the quotes from the string. how can i keep the quotation in the output

Many thanks for your help
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
The quotation marks are not passed in literally. Compare:

Code:
vnix$ echo foo
foo
vnix$ echo "foo"
foo
vnix$ echo '"foo"'
"foo"
If you change the loop so that it prints every item it processes, you should find that "see if" is handled as a single argument.
See further http://www.grymoire.com/Unix/Quote.html

As an aside, note that "$@" should be preferred over $*
Reply With Quote
  #3 (permalink)  
Old 07-22-2008
Registered User
 

Join Date: Jul 2008
Posts: 4
Stumble this Post!
Hi era,

I have noticed that
echo $@
or
echo $*

seem to remove the quotes from the arguments in the scripts. Is there a away to keep this

i.e example script
example this is an "example" test
echo $* or $@
echo's
this is an "example" test

rather than

this is an example test
Reply With Quote
  #4 (permalink)  
Old 07-23-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 724
Stumble this Post!
If you want to keep the quotes you can escape them with a slash when writing the parameter like
Code:
./mach.ksh \"example\" test
$@: "example" test
$*: "example" test
Reply With Quote
  #5 (permalink)  
Old 07-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
Read the grymoire.com quoting tutorial from the link above. Quote characters are inherently special; you can't get literal quotes through in the shell without additional quoting or other escaping mechanisms.
Reply With Quote
  #6 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 4
Stumble this Post!
thanks era, i have been through the link you provide UNIX Shell Quote
this make perfect sense, unfortunately I am in big trouble i am using an application which allows me access to the arguments i cant make changes to the original commands that are passed,
so i cant wrap them round / or '

i can just use these arguments to create scripts which i use.

The problems is some of the commands that can be passed have quotations which seem to be disappear

so i assume there is no walk around this?

Many thanks
Reply With Quote
  #7 (permalink)  
Old 07-23-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 625
Stumble this Post!
Question tr data before & after commands

Can you do something like
Code:
tr '"' '~'
your stuff, then
tr '~' '"'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0