0

How would I execute python dictionary key/values as commands in a linux kernel? For example:

keys = {
        "echo" : "hello",
        "touch" : "nothing"
       }

and I wanted to run the first key/value pair to run in the shell "echo hello".

I've tried using the json module, but don't know where to go from there.

martineau
  • 119,623
  • 25
  • 170
  • 301
Alias
  • 163
  • 8
  • 1
    Python dictionaries don't support duplicate keys. Given the structure you would need one key and a list: `keys = {"echo":["hello","nothing"]}` – Celius Stingher Oct 03 '19 at 13:18

1 Answers1

0

In python, the subprocess module can be used to handle system calls. It would perhaps easier to define the commands in a list

import subprocess
commands = [['echo', 'foo'], ['echo', 'bar']]
for c in commands:
    subprocess.run(c)

If you wish to use a dictionary instead, it can of course be done (this is for Python 3):

commands = {'echo': 'hello'}
for c in commands.items():
    print(c)

But here, each command (for example echo) can only appear once as mentioned in the comments.

teekarna
  • 1,004
  • 1
  • 10
  • 13