We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
How to do for loop in single ssh connection

I'm trying to pass the parameter from python to shell script. Inside shell script, I need to use the parameter to execute another python script.
What I need is:
1)Inside the shell script I need to get the passed parameter.
2)With a single ssh connection, I need to loop the parameter in for loop. (I don't want to open multiple ssh connection for each word)
3)Inside for loop, I need to execute another python script bypassing the variable as a parameter.
My error: Inside the shell script, I'm not able to loop that variable. Where $i is empty.
What I had tried:
test.py
import subprocess words = ["abbc", "deef", "ghhi"] join_word = ' '.join(words) append_word = '"'+join_word+'"' ssh_key = #Key bastion_ip = #ip_address subprocess.call(["sh", "test.sh",ssh_key, bastion_ip ,append_word])
test.sh
ssh -i $1 ubuntu@$2 -o StrictHostKeyChecking=no << EOF echo $3 #where $3 contains "abbc deef ghhi" for i in $3 do python3 hello.py $i done EOF
Thanks in advance.
Comments
-
You may wish to try stackoverflow or similar, this isn't really a forum for programming or shell scripting problems. More a forum for "what is the best gaming laptop for under £50" type questions.
While it's not your problem with the while loop, I'm pretty sure you don't mean the two < in test.sh , HTML entities have no place in a shell script. You could probably use awk to split up $3 with space as a delimiter, and a combination of sed and awk to figure out how many words $3 contains so you have a number to use in a for loop. Or maybe look into foreach .Proud member of the wokerati, though I don't eat tofu.Home is where my books are.Solar PV 5.2kWp system, SE facing, >1% shading, installed March 2019.Mortgage free July 20230 -
Ignore ^^ ,I know you are using << to pass << EOTquick and dirty I know but my pi is down at the mo
ssh -i $1 ubuntu@$2 -o StrictHostKeyChecking=no << EOF echo $3 #where $3 contains "abbc deef ghhi"
#all vars , cut 3-end , translate space to new-line then read i echo $* | cut -d" " -f 3- | tr " " "\n" | while read i
do
python3 hello.py $i done EOF
4.8kWp 12x400W Longhi 9.6 kWh battery Giv-hy 5.0 Inverter, WSW facing Essex . Aint no sunshine ☀️ Octopus gas fixed dec 24 @ 5.74 tracker again+ Octopus Intelligent Flux leccy0
Confirm your email address to Create Threads and Reply

Categories
- All Categories
- 351.7K Banking & Borrowing
- 253.4K Reduce Debt & Boost Income
- 454K Spending & Discounts
- 244.7K Work, Benefits & Business
- 600.1K Mortgages, Homes & Bills
- 177.3K Life & Family
- 258.4K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.2K Discuss & Feedback
- 37.6K Read-Only Boards