Loop inside "heredoc" in shell scripting
Asked Answered
S

5

10

I need to execute series of commands inside an interactive program/utility with parameterized values. Is there a way to loop inside heredoc ? Like below .. Not sure if eval can be of any help here. Below example doesn't seem to work as the interactive doesn't seem to recognize system commands.

#!/bin/sh
list="OBJECT1 OBJECT2 OBJECT3"
utilityExecutable << EOF
for i in $list ; do
utilityCommand $i
done
EOF
Stability answered 5/8, 2016 at 4:5 Comment(3)
well, a heredoc could be a subshell. That might work, but ordinarily what you want won't work (as I think you've discovered). Becuase your example is theoretical, all I can answer is that is might be theoretically possible to resturcture your code so the for loop is outside, "wrapped-around" utilityExecutable. Use real commands in your Q, and it will be easier to help. Good luck.Fluttery
I think you need to better explain what you are trying to do. Is it that you want to send a series of input lines to utilityExecutable?Grimaldi
@Grimaldi , yes I need to send/execute a series of commands inside the utilityExecutable without exiting the utility until it finished all the commands. is this still possible using heredoc ?Stability
A
7

Instead of passing a here-document to utilityExecutable, the equivalent is to pipe the required text to it. You can create the desired text using echo statements in a for-loop, and pipe the entire loop output to utilityExecutable:

#!/bin/sh

list="OBJECT1 OBJECT2 OBJECT3"

for i in $list; do
    echo "utilityCommand $i"
done | utilityExecutable
Acrolein answered 5/8, 2016 at 5:46 Comment(0)
W
4

Yes, this is tricky and can be confusing! You have to modify your codes as follow.

#!/bin/sh
list="OBJECT1 OBJECT2 OBJECT3"
utilityExecutable << EOF
  list="$list"
  for i in \$list ; do
    utilityCommand \$i
  done
EOF

This is because heredoc uses its own variables, which are completely separate from the shell. When you are inside heredoc, you have to use and modify heredoc's own variables. So the \$ is needed to reference heredoc's own variables instead of shell variables when inside heredoc.

Wallywalnut answered 28/1, 2018 at 2:48 Comment(0)
A
3
cat << EOF
$(
    for i in {1..10}; do
        echo $i;
    done
)
EOF
Anaphase answered 19/2, 2022 at 13:49 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Guadalupe
A
2
myVar=$(
    for i in {1..5}; do
        echo hello;
        echo world;
    done;
); cat <<< $myVar
Anaphase answered 19/2, 2022 at 14:7 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Guadalupe
N
0
commandxyz -noenv<<EOF
echo "INFO - Inside eof" 
t_files=("${p_files[@]}")
#copy array
#echo \${t_files[*]} 
#all elements from array
#echo \${#t_files[@]}
#array length
for i in \${t_files[@]} ; do
        echo -e \$i;
        do other stuff \$i;
done
cat $patch_file
git apply $patch_file
EOF
Neckpiece answered 24/9, 2019 at 6:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.