|
From: | aero |
Subject: | Re: Unexpected positional replacement strings substitution |
Date: | Sat, 18 Feb 2017 13:49:24 +0900 |
On Sat, Feb 11, 2017 at 10:36 AM, aero <chahkang@gmail.com> 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
[Prev in Thread] | Current Thread | [Next in Thread] |