0

Below scrip reads lines of commands to execute from a cmdAll.txt. Is there a way that I merge the command file into the script file itself. I remember I have seen such data blocks using <<EOF somewhere but I am not sure how to do it.

myScript.py file:

#!/usr/bin/python
i=1
import enms
s = enms.open()
with open('cmdAll.txt') as f:
    for cmd in f:
        r = s.terminal().execute(cmd)
        filename = "output_%d.txt" % i
        fp = open(filename, 'w')
        for line in r.get_output():
            print line
            fp.write(line + "\n")
        fp.close()
        i+=1
f.close()
enms.close(s)

cmdAll.txt file:

cmget get * en.(enbid) -t
cmget get * gn.(gnbid) -t
cmget get * gnssinfo.(latitude,longitude) --table
cmget get * GpsSyncRef.(latitude,longitude) --table
8
  • Can't you just inline the string and split newlines? Commented May 4, 2022 at 20:44
  • I know that solution, however I am interested in that block data file, as the number of commands may be hundred lines... Commented May 4, 2022 at 20:46
  • Is it just a static file that never changes? Or is it dependent on input/things? 1000s of lines is not really a problem, it won't take more than a few MB of memory. Commented May 4, 2022 at 20:47
  • the commands changes and if I inline them the script will be messy. The solution I am asking is to clean up the code and data in single file easy to maintain each time the list of commands changes. no performance concerns here. Commented May 4, 2022 at 20:52
  • What's wrong with your current solution? It looks very clean... Commented May 4, 2022 at 21:03

1 Answer 1

1

I think the <<EOF bit you saw somewhere had to do with running Python in a shell script in a shell script, but you're asking for the inverse. You want your shell commands to live in your python script.

If you're not going to read the commands from a .txt file, and you would rather they live in the script, you might as well just make it clean and make a list out of them.

import enms

cmds = [
    "cmget get * en.(enbid) -t",
    "cmget get * gn.(gnbid) -t",
    "cmget get * gnssinfo.(latitude,longitude) --table",
    "cmget get * GpsSyncRef.(latitude,longitude) --table"
]

s = enms.open()
for cmd in cmds:
    r = s.terminal().execute(cmd)

If you prefer the commands exist as a single string, you could do something like this, where you are splitting on newlines. Just as Eric Jin suggested

import enms

comds = '''cmget get * en.(enbid) -t
cmget get * gn.(gnbid) -t
cmget get * gnssinfo.(latitude,longitude) --table
cmget get * GpsSyncRef.(latitude,longitude) --table'''

s = enms.open()
for cmd in cmds.split("\n"):
    r = s.terminal().execute(cmd)

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

Comments

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.