How can we execute a loop of screens, each of them executing a loop so that it picks both inner and outer variables?
Consider this:
# does not print i
for i in 1 2; do
screen -dmS screen-${i} bash -c 'for j in 1 2; do
echo screen-${i}-iter-${j}
done; exec bash'
done
This one does not work because the non-interpolating quotes imply the variables inside are intrinsic to bash -c, right? So ${i} is empty.
Consider also:
# does not print j
for i in 1 2; do
screen -dmS screen-${i} bash -c "for j in 1 2; do
echo screen-${i}-iter-${j}
done; exec bash"
done
The second one also does not work because the interpolating quotes imply interpolation outside of bash -c. So ${j} is empty.
How can I get around this issue? Is it more appropriate to do it in another language?
I tried Python but could not get around the screens dying when the python script completed. So I couldn't see what is in the screen. Anyway, the screen has to stay open once whoever is calling it dies. My attempt in python follows for completeness.
import subprocess
def screen(i):
proc = subprocess.Popen('screen -S screen-'+str(i), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
command = f"for j in 1 2; do\necho screen-{i}-iter-${{j}}\ndone; exec bash\n"
proc.stdin.write('{}'.format(command).encode('utf-8'))
for i in range(2):
screen(i)
"screen-${i}"'-iter-${j}'. Here${i}is double-quoted,${j}is single-quoted. Is this what you want to achieve? – Kamil Maciorowski Jan 18 '24 at 10:09