I have a script that sends multiple commands after logging in a remote machine via SSH.
Problem is, it's barely readable. I've tried quoting everything with "", but it's terrible (everything appears in pink), and here documents are just as bad (everything appears in gray). I'm using Gedit as an editor, but I tried Emacs as well.
Can I make the editor parse the remotely executed code as code?
#!/bin/bash
function remoteExec()
{
echo "remote execution starting"
ssh -n "1ドル" << 'EOF'
#the following code is _not_ highlighted by real editors
currIt=\"2ドル\" #I hope this parameter is passed correctly
while [ "$currIt" -lt 5 ]; do
echo "hello"
currIt=$(($currIt + 1))
done
EOF
}
I'd rather not create a separate script just for these lines, because then I'd have to copy it on the remote machine, or pipe it, and I have arguments too.
EDIT: as Glenn noticed, as far as highlighting goes, there isn't much to do. I'm open to solutions putting the code in a separate script. I need to transfer the code to the remote host then, and pass it the necessary arguments.
-
1\$\begingroup\$ As far as I understood that question, you are asking about: "How can I make my Editor parse strings as code?". Is that correct? \$\endgroup\$Vogel612– Vogel6122014年03月04日 14:39:03 +00:00Commented Mar 4, 2014 at 14:39
-
\$\begingroup\$ Not really. I need a readable way to execute some lines of code on a remote machine, possibly without creating a new script. The fact that the editor does not parse the current code as such makes it not readable. \$\endgroup\$Agostino– Agostino2014年03月04日 14:49:39 +00:00Commented Mar 4, 2014 at 14:49
1 Answer 1
You're quoting the heredoc terminator, so 2ドル will certainly not be expanded
When you want to pass multiple commands to ssh, wrap them as a single script to an interperter, thus ssh sees one single command.
Lots of whitespace for readability.
And yes, not much opportunity for syntax highlighting.
Since you need variable expansion within the script, you need to worry about quoting variables you want to be expanded on the remote host. I changed the while loop to a for loop to minimize the number of $
-signs to escape.
If the script you want to send is more complex, be careful about how you use single and double quotes.
function remoteExec()
{
echo "remote execution starting"
ssh -n "1ドル" << EOF
bash -c '
for (( currIt="2ドル"; currIt < 5; currIt++ )); do
echo "hello"
done
'
EOF
}
Given an arbitrary number of args:
function remoteExec()
{
local host=1ドル
local startIt=2ドル
local quotedArgs
shift 2
for arg in "$@"; do
quotedArgs+="\"$arg\" "
done
echo "remote execution starting"
ssh -n "$host" << EOF
bash -c '
for (( currIt="$startIt"; currIt < 5; currIt++ )); do
echo "hello"
done
args=( $quotedArgs )
printf "%s\n" "\${args[@]}" # escape this sigil
'
EOF
}
remoteExec host 3 arg1 arg2 "this is arg3"
-
\$\begingroup\$ Thanks for making it more generic. I'm starting to think that using a separate script instead of a heredoc would make this more readable. How would you do that? Thanks again. \$\endgroup\$Agostino– Agostino2014年03月04日 16:19:24 +00:00Commented Mar 4, 2014 at 16:19
-
1\$\begingroup\$
scp local_script.sh $host:/path/to/script.sh; ssh -n $host "chmod u+x /path/to/script.sh && /path/to/script.sh $arg1 $arg2"
\$\endgroup\$glenn jackman– glenn jackman2014年03月04日 17:23:28 +00:00Commented Mar 4, 2014 at 17:23 -
\$\begingroup\$ Both of your scripts misinterpret
2ドル
in a single-quoted context, i.e. literally. \$\endgroup\$200_success– 200_success2014年03月04日 17:26:00 +00:00Commented Mar 4, 2014 at 17:26 -
\$\begingroup\$ @200_success is it ok if on local I double quote variables like
./myScript.sh "1ドル" "2ドル"
but in ssh commands I don't and just writessh -n user@host "cd $dir;./myScript.sh 1ドル 2ドル"
? BTW In the second case the script hangs after I give it the password... Thanks. \$\endgroup\$Agostino– Agostino2014年03月04日 21:58:42 +00:00Commented Mar 4, 2014 at 21:58 -
1\$\begingroup\$ @200_success, no they don't: a heredoc is essentially a double-quoted string. It's just like this:
x=foo; y="this is '$x'"; echo "$y"
. Or, are you suggesting the OP wants to send a literal2ドル
? \$\endgroup\$glenn jackman– glenn jackman2014年03月04日 22:11:25 +00:00Commented Mar 4, 2014 at 22:11