2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 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
    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
    Mar 4, 2014 at 14:49

1 Answer 1

3
\$\begingroup\$

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"
\$\endgroup\$
8
  • \$\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
    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\$ Mar 4, 2014 at 17:23
  • \$\begingroup\$ Both of your scripts misinterpret $2 in a single-quoted context, i.e. literally. \$\endgroup\$ 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 write ssh -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
    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 literal $2? \$\endgroup\$ Mar 4, 2014 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.