String concat that keeps quotes


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers String concat that keeps quotes
# 1  
Old 07-23-2008
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
# 2  
Old 07-23-2008
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 $*
# 3  
Old 07-23-2008
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
# 4  
Old 07-23-2008
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

# 5  
Old 07-23-2008
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.
# 6  
Old 07-23-2008
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
# 7  
Old 07-23-2008
Question tr data before & after commands

Can you do something like
Code:
tr '"' '~'
your stuff, then
tr '~' '"'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python concat list with a string variable

Hello! #!/usr/bin/env python hn = "simsa-svr" for container in containerslist: Name = container I want to print Name along with hn i,e simsa-svr. I tried like Name = container'-'.join(hn) did not work. Needed result: lilly2232321-simsa-svr (2 Replies)
Discussion started by: ashokvpp
2 Replies

2. Shell Programming and Scripting

Concat String with variable after a 'grep' and awk

Here is the structure of my file: MyFile.txt g-4.n.g.fr 10.147.243.63 g-4.n.g.fr-w1 Here is my sript: test.sh #! /bin/sh ip=10.147.243.63 worker=$(grep -e $ip $1 | awk '{ print $3; }') echo "" echo $worker echo "" echo $worker echo "" echo "$worker.v.1" echo... (7 Replies)
Discussion started by: chercheur111
7 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

How to find a string with double quotes?

I have thousands of files in a directory. I need to find/list all files that have the below matching string - RETURNCODE: "1017" Thank you! (5 Replies)
Discussion started by: esmgr
5 Replies

5. Shell Programming and Scripting

String between quotes

Hi, Need to capture a string between 1st & last quote. String can be anything like a b "c" d e Output: c a "b c" d e Output: b c a "b c" d "e" f Output: b c d e sed 's/.*"\(.*\)".*/\1/g' Above helps me to find the string between last quote. Need to find the string between 1st &... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

6. Shell Programming and Scripting

Get string between quotes separate by commas

I'm a beginner with shell and tried to do this per hours and everytinhg gives different want i do. So I have a lot of file in *.csv ( a.csv, b.csv ...) in each file csv , it has some fields separeted by commas. ----- "joseph";"21","m";"groups";"j.j@gmail.com,j.j2@hotmail.com"... (6 Replies)
Discussion started by: flaviof
6 Replies

7. Shell Programming and Scripting

need to enclose a string in quotes

I have a script which I call and pass a text string to it. This string is then is assigned to a variable in the script. I then call another script and pass that variable to the second script, but when I do, the quotes are lost and the second script gets a total of three variables 'my', 'lovely' and... (3 Replies)
Discussion started by: iskatel
3 Replies

8. Shell Programming and Scripting

Add double quotes around the string

I have a line in multiple scripts:select into table /dir1/dir2/file.dat dir1 and dir2 are the same but file.dat is different from script to script. I need to include /dir1/dir2/file.dat into double quotes in each file of my directory:select into table "/dir1/dir2/file.dat" (13 Replies)
Discussion started by: surfer515
13 Replies

9. Programming

simple question on string concat

This is a simple question... char *str = NULL; int num = 0; scanf ("%d", &num); str = ??? I want str to point to the string num.txt For e.g: If user enters num = 5, str should point to "5.txt" How do I do that ? Thanks (2 Replies)
Discussion started by: the_learner
2 Replies

10. Shell Programming and Scripting

concat string

hey, I want to concat whole bunch of strings together but somehow they don't turn out the way I want them to a="HELLO " b="WORLD " c=$a$b I was expecting c to be "HELLO WORLD " but it... (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question