We'd like to remind Forumites to please avoid political debate on the Forum... Read More »
We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
How to do for loop in single ssh connection
Options

selenanur
Posts: 1 Newbie
in Techie Stuff
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.
0
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
- 350.8K Banking & Borrowing
- 253.1K Reduce Debt & Boost Income
- 453.5K Spending & Discounts
- 243.8K Work, Benefits & Business
- 598.7K Mortgages, Homes & Bills
- 176.8K Life & Family
- 257.1K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.1K Discuss & Feedback
- 37.6K Read-Only Boards