0

I have a simple python script which I'd like to write a shell script to increase a specific value (bearer_id) inside the script incrementally (till a fixed value like 100, 200) and repeat the other lines just as they are (example below). can you please give me some hints/or example of a shell script, on how to do this?

script.py is like:

sgw.add_bearer(dpid=256, bearer_id=1, \
saddr_ingress="10.1.1.10", \
sport_ingress=2152)

Final script should be like this, script.py:

sgwumgt.add_bearer(dpid=256, bearer_id=1, \
saddr_ingress="10.1.1.10", \
sport_ingress=2152)

sgwumgt.add_bearer(dpid=256, bearer_id=2, \
saddr_ingress="10.1.1.10", \
sport_ingress=2152)
.
.#repeat 99 times
.
sgwumgt.add_bearer(dpid=256, bearer_id=100, \
saddr_ingress="10.1.1.10", \
sport_ingress=2152)

p.s: This has to be a separate shell script which I execute it over ssh on the remote machine (which has this python script, ready to execute) then this shell script must either create a new .py file such as I mentioned above or change the existing one in that way for further steps.

Thanks a lot in advance

6
  • Why not just do all of this within python? Commented Dec 3, 2015 at 18:14
  • 2
    It doesn't look like you've heard of a loop. I recommend going through the Python tutorial; it'll answer this question and a whole lot more. Commented Dec 3, 2015 at 18:14
  • Well, I'm aware of the loop concept :) The explanation may haven't been clear enough, This has to be a separate shell script which I execute it over ssh on the remote machine (which has this python script, ready to execute) then this shell script must either create a new .py file such as I mentioned above or change the existing one in that way for further steps. sorry again for my not clear explanation Commented Dec 3, 2015 at 18:24
  • edited the question :) Commented Dec 3, 2015 at 18:27
  • You want to write code that rewrites your other code? That... doesn't sound like the best way to accomplish your underlying goal. Also, even if you use a script-rewriting script, you should probably make it write a loop. Commented Dec 3, 2015 at 18:28

2 Answers 2

1

If the only thing that is changing is the bearer_id than you could use a for loop instead

Something like:

for id in range(1,101):
    sgw.add_bearer(dpid=256, bearer_id=id, \
    saddr_ingress="10.1.1.10", \
    sport_ingress=2152)

This would greatly reduce the amount of code you needed to write as well as make it easier to modify the code later.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't need the backslash at the end of the line.
0

Looping constructs exist to solve this exact problem you have:

max_id = 100
for id in range(1, max_id+1):
    sgwumgt.add_bearer(dpid=256, bearer_id=id,
                       saddr_ingress="10.1.1.10",
                       sport_ingress=2152)

If you weren't aware you could do this then you really should spend some time with some python tutorials as this is a very fundamental building block for writing python code.

If the python script has to change the behavior based on a command line parameter you have a few options. You can use argparse or similar or perhaps do something quick with sys.argv. Here's a way you can do this with sys.argv:

import sys
if(len(sys.argv)) == 1:
    max_id == 100 #Default case
elif(len(sys.argv)) == 2:
    max_id = int(sys.argv[1])

Which you can then call on the command line with python script.py 150 which would set max_id to 150. If you need to do more complex argument parsing I would recommend using argparse.

7 Comments

Thanks! I'm aware of the loop concept :) The explanation may haven't been clear enough, This has to be a separate shell script which I execute it over ssh on the remote machine (which has this python script, ready to execute) then this shell script must either create a new .py file such as I mentioned above or change the existing one in that way for further steps. sorry again for my not clear explanation
Can you have your python script take a command line argument and call that from the shell?
I don't think so(actually not sure at the moment), it's better to rewrite a new copy of that .py script in that way. don't have access to remote machine till next two hours. I'll check it ASAP
It seems like I just don't understand your use case. You can make a new .py file from a shell script but you cannot modify an existing .py file?
yes, exactly! I can just read it. there's no write access for that .py script
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.