Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
1  
for code coloring, you can have a look at syntax highlighting help –  Vogel612 Mar 4 '14 at 13:28
1  
It should be, they say "languages including C and friends, Java, Python, Bash". I changed the line to <!-- language: lang-sh --> But I guess it's far from perfect (Bash synthax is anything but strict). –  Agostino Mar 4 '14 at 13:45
1  
As far as I understood that question, you are asking about: "How can I make my Editor parse strings as code?". Is that correct? –  Vogel612 Mar 4 '14 at 14:39
    
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. –  Agostino Mar 4 '14 at 14:49

1 Answer 1

up vote 3 down vote accepted

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"
share|improve this answer
    
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. –  Agostino Mar 4 '14 at 16:19
1  
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" –  glenn jackman Mar 4 '14 at 17:23
    
Both of your scripts misinterpret $2 in a single-quoted context, i.e. literally. –  200_success Mar 4 '14 at 17:26
    
@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. –  Agostino Mar 4 '14 at 21:58
1  
@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? –  glenn jackman Mar 4 '14 at 22:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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