The Shell lost the inverted comma in a nested ssh command


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
The Shell lost the inverted comma in a nested ssh command

Hi,

i want use this Comand for my psql request


Code:
sh ssh -o StrictHostKeyChecking=no rootatemailaddress.de sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'"



but the shell lost the inverted comma for datnam=\'$DB\'. The request deliver datnam=DB1 but for the psql request i need datnam='DB'. Where is my mistake? The psql comand need the inverted comma left and right from the $DB variable.

Last edited by RudiC; 1 Week Ago at 06:17 AM..
# 2  
Old 1 Week Ago
Quite involved a remote command. I can't replicate your entire setup, but for a simplified command, dropping the sh from the front, and removing the '" from the end made it work. Give it a try?
# 3  
Old 1 Week Ago
Could you try these variations?:-
Code:
ssh -o StrictHostKeyChecking=no rootatemailaddress.de 'sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'"'       # Wrapping it all in single quotes should pass the double quotes, but you may need to escape the single quotes within the double quotes
ssh -o StrictHostKeyChecking=no rootatemailaddress.de 'sudo psql -U postgres -c \'select pg_terminate_backend(pid) from pg_stat_activity where datnam=$DB;\''             # backslash and two single quotes at the end
ssh -o StrictHostKeyChecking=no rootatemailaddress.de "sudo psql -U postgres -c \"select pg_terminate_backend(pid) from pg_stat_activity where datnam=\"\$DB\";\""       # backslash and two double quotes at the end, escaping the $ for the DB name
ssh -o StrictHostKeyChecking=no rootatemailaddress.de "sudo psql -U postgres -c \"select pg_terminate_backend(pid) from pg_stat_activity where datnam=\\\"\$DB\\\";\""     # extra escaping so that the quoted string passed ends up as \"$DB\" to the remote shell.
ssh -o StrictHostKeyChecking=no rootatemailaddress.de "sudo psql -U postgres -c \"select pg_terminate_backend(pid) from pg_stat_activity where datnam=\"\$DB\";\""       # Which seems to work okay if I insert an echo before the sudo (because I don't have postgress installed on my local machine.

They are variations to try passing quotes and/or quoted text. I presume that you want to pick up the value of $DB from the remote machine rather than the local machine.

Does that get you anywhere? If you want to just dummy run it, add an echo just before the sudo within the quotes (of whatever type) and it should show you what it would run on the remote machine. Where do you pick up the vale of $DB? Is that set in the profile of the account you are connecting to? Should we worry that the sudo command has no password requirement?



I hope that this helps,
Robin
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Check if file exists via ssh in ssh (nested) say170 Shell Programming and Scripting 2 10-23-2014 04:31 PM
Using shell command need to parse multiple nested tag value of a XML file NeedASolution Shell Programming and Scripting 3 01-09-2014 04:01 PM
Shell script for ssh command listening Kijana_kenn Shell Programming and Scripting 2 10-10-2013 08:09 AM
Attempting to pass my array as a nested parameter to loop. A little lost. briandanielz Shell Programming and Scripting 7 11-01-2012 07:11 PM
issue on ssh command in ksh shell leobdj Shell Programming and Scripting 4 09-18-2012 04:33 AM
Data pipe lost when using ssh in shell script siavash Shell Programming and Scripting 2 02-01-2012 06:28 AM
ssh command to execute shell script in another server only4satish UNIX for Dummies Questions & Answers 5 01-12-2012 01:33 PM
Shell Script with ssh command toughlittleone Shell Programming and Scripting 4 04-14-2011 01:35 AM
executing command in a remote machine through ssh - shell script rateeshkumar Shell Programming and Scripting 1 03-22-2011 03:33 AM
Shell script with wget in ssh command sunrexstar Shell Programming and Scripting 4 11-21-2010 04:01 AM
Help with ssh command in shell script vikash_k Shell Programming and Scripting 6 05-21-2009 11:20 AM
How to use ssh command in the shell script without user intervention? Dendany83 Shell Programming and Scripting 3 04-07-2009 01:07 PM
Shell nested ifs philmetz Shell Programming and Scripting 8 10-02-2008 01:48 AM
Nested Echo Command Help findprakash UNIX for Dummies Questions & Answers 2 09-22-2007 12:26 AM
difference between double inverted coma and single inverted comma abhisekh_ban Shell Programming and Scripting 1 02-15-2007 10:03 AM