LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   echo ' symbol (http://www.linuxquestions.org/questions/linux-newbie-8/echo-symbol-733390/)

mwkemo 06-16-2009 11:44 AM

echo ' symbol
 
Hey,

I am having problem with echo command. It works, but ' symbol is removed from output, is there a way to fix this?

echo -n 'USE lgsl; INSERT INTO lgsl (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', '30003', 'callofduty2');' > mysql.run

and after reading the mysql.run file there is no ' symbol. I did try to replace symbol with code \048' but this is not working for me. Propebly I'm doing something wrong but dont know what.

THX for help

unSpawn 06-16-2009 11:50 AM

Make the outer quotes be double quotes?

mwkemo 06-16-2009 12:01 PM

No, this will not work as this command is used with other command that uses double quotas. Something like this

ssh admin@192.168.1.1:"cd test; echo -n "USE lgsl; INSERT INTO lgsl (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', '30003', 'callofduty2');" > mysql.run; cd test2"

it is example, but it is not working if I am using the double quote. After "cd test" the script stops. If I remove double quotes after echo -n and use one quote then the script is working but it removes the quotes for '192.168.0.1', '30003', '30003', 'callofduty2' .And this quote needs to be there.

Dinithion 06-16-2009 12:13 PM

What about backslash?

echo This\' works for me.

jamescondron 06-16-2009 12:21 PM

As Dinithion says, you have to use a backslash to escape it

catkin 06-16-2009 01:15 PM

Hello mwkemo :)

I don't use ssh so can't test but how about something like this ...

Code:

dquote='"'

ssh admin@192.168.1.1:"cd test; echo -n ${dquote}USE lgsl; INSERT INTO lgsl (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', '30003', 'callofduty2');$dquote > mysql.run; cd test2"

AIUI, the mechanism is that the shell you are issuing the command from parses the line and creates a single word of everything between the " characters. Parsing removes the outer " characters and substitutes the $dquote variable so it now looks like

Code:

cd test; echo -n "USE lgsl; INSERT INTO lgsl (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', '30003', 'callofduty2');" > mysql.run; cd test2
This is passed to another shell (on a remote system?) that parses it again. Parsing removes the outer " characters so it now looks like

Code:

cd test; echo -n USE lgsl; INSERT INTO lgsl (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', '30003', 'callofduty2'); > mysql.run; cd test2
What we can't see in the code above is that the string from USE to ); is a single word, the argument for the echo -n command.

If this works and it's OK to have a space before USE lgsl in file mysql.run, you can simplify it by changing

Code:

echo -n ${dquote}USE
to
Code:

echo -n $dquote USE
Best

Charles

unSpawn 06-16-2009 02:21 PM

Quote:

Originally Posted by mwkemo (Post 3576115)
No, this will not work as this command is used with other command that uses double quotas.

http://www.catb.org/~esr/faqs/smart-...html#beprecise

rjlee 06-16-2009 02:29 PM

The quoting options for bash are quite simple, but double-quotes behave differently from single-quotes.

Put simply, double-quotes expands variables and allows you to escape special characters (like dollar signs and quotes) using a backslash, while single-quotes do not.

Single quotes just produce a literal string, and aren't very useful when you want to nest quotes, because you can't escape things. There is an alternative to escaping: if you leave no space between any two parameters, then they are run together as one (quoted or not). This is just an example, but you could use something like this to produce a single quote in parentheses (where the parentheses are single-quoted):
Code:

echo '('"'"')'
Note that a single quote is treated as a regular character when inside a double quotes, and so doesn't need escaping.


Your example uses BASH to launch another bash shell, and passes a single string to it that is then treated as a command, so you have to escape your quotes (by wrapping them in other quotes) in order to prevent them from being treated as ending quotes. This is called nesting strings.

To take your example, you could nest it using escaped double-quotes like this:
Code:

ssh admin@192.168.1.1 "cd test; echo -n \"USE lgsl; INSERT INTO lgsl
  (ip, q_port, c_port, type)
  VALUES ('192.168.0.1', '30003', '30003', 'callofduty2');\" > mysql.run; cd test2"

Hope that helps,

—Robert J Lee

mwkemo 06-16-2009 02:37 PM

Thx for replay, unfortunatlly this is not working. I will c/p the command that I'm using. The problem is that I can't put double quotes inside first double quotes, if there is only one double quote symbol inside " " the script will stop. So there is no way that i can use double quotes in my command.

Code:

ssh -i /usr/local/gcp/userdata/sparta/sparta -f -o ConnectTimeout=1 -o PasswordAuthentication=no -o StrictHostKeyChecking=no -p 1111 sparta@192.168.0.1 "cd 192.168.0.1-30001; echo -n 'USE lgsl; INSERT INTO lgsl (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30001', '30001', 'callofduty2');' > mysql.run; mysql -u gamecp_2 -h 192.168.0.1 -pabcde < mysql.run;"
If I put variable dquote='"' the script will stop to work in that place.
The /' is not working, I get some error message when i try to echo with /'

Maybe to replace the ' symbol with something else that echo command will output to file and after the echo creates the mysql.run file using the sed command to change to ' symbol. This will probably work but Iam looking for more simpler solution.

mwkemo 06-16-2009 02:51 PM

rjlee,

THX for this tip, with \" the script is working and mysql.run file looks ok, it puts all ' symbols to file. The problem is solved.

Just out of curiosity, what if I need to put one more double quotes inside echo. For ex.

Code:

ssh admin@192.168.1.1 "cd test; echo -n \"USE lgsl; INSERT INTO "lgsl" (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', 30003', 'callofduty2');\" > mysql.run; cd test2"
if I wrap around lgsl database name double qoutes. Will this work?

THX milion times

rjlee 06-16-2009 04:02 PM

Quote:

Originally Posted by mwkemo (Post 3576276)
Just out of curiosity, what if I need to put one more double quotes inside echo. For ex.

Code:

ssh admin@192.168.1.1 "cd test; echo -n \"USE lgsl; INSERT INTO "lgsl" (ip, q_port, c_port, type) VALUES ('192.168.0.1', '30003', 30003', 'callofduty2');\" > mysql.run; cd test2"

Well, you'd need to pass the double-quote character to the database name, like this:

Code:

ssh admin@192.168.1.1 "cd test;
echo -n \"USE lgsl; INSERT INTO \\\"lgsl\\\" (ip, q_port, c_port, type) VALUES
('192.168.0.1', '30003', 30003', 'callofduty2');\" > mysql.run; cd test2"

this will pass the actual string
Quote:

…INSERT INTO \"lgsl\"…
into the ssh session, which will pass the same string to the shell which invokes echo, which in turn will pass the double quotes as is to echo.


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