The purpose of HEREDOC in my parallel command is to avoid needing script
transferring to worker servers when using -S worker1,worker2,.. option.
But i encountered the problem with gnu parallel's positional replacement
string pattern like above.
HEREDOC script possibly contains postional replacement string pattern, It's
hard to anticipate when it will happen.
I walked through parallel tutorial again
and I found that I can automatically transfer script file with '--basefile'
option like the following.
parallel --basefile /home/user/myscript.sh --controlmaster -S
worker1,worker2 ssh {} bash < /home/user/myscript.sh ::: [SERVER LIST...]
Thank you for your reply.
On Sat, Feb 18, 2017 at 7:07 AM, Ole Tange <[email protected]> wrote:
> On Sat, Feb 11, 2017 at 10:36 AM, aero <[email protected]> wrote:
> :
> > #!/bin/bash
> >
> > SCRIPT=$(cat <<'EOF'
> > # long shell script
> > echo $1
> > echo ${1}
> > EOF
> > )
> >
> > parallel -v "ssh {} bash <<'_PARALLEL'
> > $SCRIPT
> > _PARALLEL" ::: localhost
> > -----script end--------------------------
> >
> > But I encountered an unexpected problem.
> > GNU parallel silently replaces {1} of shell "${1}" variable with supplied
> > arguement.
>
> This is a feature, not a bug (Really: It is!).
>
> {n} is the positional replacement string where n is number of the input
> source.
>
> The work around in your example is simple; just introduce a variable
> containing $1:
>
> #!/bin/bash
>
> SCRIPT=$(cat <<'EOF'
> # long shell script
> echo $1
> arg_one="$1"
> echo ${arg_one}
> EOF
> )
>
> parallel -v "ssh {} bash <<'_PARALLEL'
> $SCRIPT
> _PARALLEL" ::: localhost
>
> - o -
>
> When you give hostnames as argument which you then ssh to, I cannot
> help to suggest using --nonall and functions instead.
>
> Basically combining these:
> https://www.gnu.org/software/parallel/man.html#EXAMPLE:-
> Running-the-same-command-on-remote-computers
> https://www.gnu.org/software/parallel/man.html#EXAMPLE:-
> Calling-Bash-functions
>
> myfunc() {
> # long shell script
> echo $1
> echo ${1}
> }
> export -f myfunc
>
> parallel --tag -v -S localhost --env myfunc --nonall myfunc arg1 arg2
>
> {1} will not be replaced if it is used in a function, and -v will not
> be horribly long.
>
> If the examples were new to you, please consider reading all the
> examples and walking through `man parallel_tutorial` once a year. Your
> command line will love you for it.
>
>
> /Ole
>