Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I'm writing a SSH wrapper and things are looking pretty good. However, if I want to send a complex Unix command such as below, I'm running into so problems.

cat /proc/cpuinfo | grep "^cpu MHz.*" | awk -F": " '{print $2}' | sed 's@\.@@g' | uniq

The first double quote i can easily embed in the string by adding \ to the double quote :

string text = "cat /proc/cpuinfo | grep \"^cpu MHz.*\" | awk -F\": \" '{print $2}' | sed 's@\.@@g' | uniq";


I added a \ right before the double quote at \"^cpu MHz.*\". That works fine, however I'd like to escape an entire string like
's@\.@@g' 


Can someone please point me in the right direction.

Cheers and thanks one again.
Posted

There's a good article at Escaping in C#: characters, strings, string formats, keywords, identifiers[^]

Basically, you should be able to use the @ symbol.

For example:

C#
String tempVar = @"cat /proc/cpuinfo | grep "^cpu MHz.*" | awk -F": " '{print $2}' | sed 's@\.@@g' | uniq";


That tells C# to take everything as is. It escapes the whole string. Although, with double quotes I am not sure. You may still have to do a Replace() and change them.
 
Share this answer
 
Comments
lewax00 25-Apr-13 16:10pm    
Just a note: to use double quotes in a literal string, you need use two, e.g. @"grep ""^cpu MHz.*"""
Sergey Alexandrovich Kryukov 25-Apr-13 16:19pm    
My 5, but I think to root of the problem is deeper, so I provided a different advice. Please see my answer.
—SA
Espen Harlinn 25-Apr-13 19:15pm    
5'ed!
Solution 1 should give you the complete direction on how to write string literals.

However, the root of the problem is much deeper. The real problem is that you are trying to hard-code string constants. Moreover, I suspect the with your coding style you allow yourself immediate constants which is even worse.

(If you don't understand what are those constants, here is some simple illustration:
C#
const string myValue = "This is my constant string"; // explicit constant 

//...

string message = "This is my constant string" + ": blah-blah-blah"; // each literal is an immediate constant, which is really bad
MessageBox(message); // because, should you ever use the same string elsewhere, you violate the DRY principle


Please see: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].)
Even such constants and sometimes even the immediate constants can come handy, especially in the middle of research or development, in a software product, they do not belong in code. They should go in data files or resources embedded in executable modules. If you do this, you will write strings as is, without a need to bind you mind with escaping.

—SA
 
Share this answer
 
Comments
Espen Harlinn 25-Apr-13 19:14pm    
Good advice :-D
Sergey Alexandrovich Kryukov 25-Apr-13 19:41pm    
Thank you, Espen.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900