0

Regarding this post How to include python script inside a bash script , I get used to work with python HereDoc. However, I want to include that python HereDoc inside a for loop, and so, it needs to be idented but it does not seem to be recognized by Visual Studio starting in blue color from the first HereDoc. Also python is blank because it is not written yet, but I wanna know if it is possible to call for a HereDoc inside a Bash Shell Loop.

datarange=$(LC_ALL="POSIX" seq -f %.15g 0.5 0.25 5.5 | sed '/\./!s/$/.0/') 
for i in $datarange
do
    cp -r MovableFiles/mode-1 D$i
    cp -r MovableFiles/mode-4 D$i
    echo -n CWD:; /bin/pwd 
    cd $PBS_O_WORKDIR
    echo -n CWD:; /bin/pwd 
    cd D$i
    cd mode-1
    echo -n CWD:; /bin/pwd 
    # changes input files for mode-1
    python3 - << HERE



    HERE

    cd mode-4
    echo -n CWD:; /bin/pwd 
    # changes input files for mode-4
    python3 - << HERE




    HERE
done

What I want to do is a loop over the input files on "MovableFiles", moving them to directories D$i and changing a specific line on the fly (regarding the $i number), but I tend to do the text-change in Python3 so that's why I need a HereDoc or any other way to use Python3 inside a loop.

6
  • Doesn't it work if you simply don't indent? I mean, the first python line should start at the very first position of the line, and then you can indent for python. Since sane languages (sorry, python) don't consider white space significant, you can freely break the indentation of your bash script for the few lines of python. Does that not work for you? – terdon Mar 29 at 18:05
  • Finally solution was given by @terdon . If I indent both EOF, an error triggers, but if I do not indent the end-EOF, it works perfectly. – PARCB Mar 30 at 10:37
  • @PARCB, did you look at that linked question above? – ilkkachu Mar 30 at 10:40
  • @PARCB yes, the quesiton that ilkkachu linked is the better solution: you can both keepn your indentation and have the code work. – terdon Mar 30 at 10:41
2

You can declare each block of Python code with local column-1 indentation within its own Bash function.

emitPy_Mode_4 () { cat <<'EOF'
Your python source
    goes here with
        any indentation you like
Just fine
EOF
}

You can invoke it within the Bash loop through stdin like:

emitPy_Mode_4 | python3 -

or as a Here String like:

python3 - <<<"$( emitPy_Mode_4 )"

A Here Document is either completely quoted, or completely unquoted (depending on whether the start marker in <<EOF is quoted). Unquoted, it will be subject to shell expansions, and vulnerable to characters that mean something else in the text being passed, unless each occurrence is itself escaped.

If the HereDoc is wrapped in a function, it can be split into sections with different quoting, to control expansion. Required variables can be passed into the function:

emitPy_Mode_4 () {  #.. Takes one filename argument.
cat <<'EOF'    #.. Preamble
Your python source
EOF
cat <<EOF      #.. Inject variable.
    myPathName = '${1}'
EOF
cat <<'EOF'    #.. Remainder.
    goes here with
        any indentation you like
Just fine
EOF
}

IIRC, the unquoted section can include any shell construct, like a process expansion, which can call any other function, or external command, to inject text into the HereDoc.

1
  • 1
    You could also just store it in a variable rather than a function – jesse_b Mar 29 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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