I'm trying to iterate lines in a text file where each line is a command that I send to the command line to run. I want to keep track of each command and it's output by putting them in variables but i'm unsure how to assign a separate variable to each iteration. Here's my code to clarify.
example command.txt file:
echo "hello world"
echo "I am here"
ls
code to read and run the commands line by line
file = open("/home/user/Desktop" + "commands.txt", "r+")
lines = file.readlines()
for x in range(0,len(lines)):
lines[x].strip()
os.system(lines[x])
# here I want to save each command to a different variable.
# (for ex.) command_x = lines[x] (so you get command_1 = echo "hello world", and so on)
# here I want to save the output of each command to a different variable.
# (for ex.) output_x = (however I access the output...)
The reason I want to do this is so I can create a command log file where it will state the command given and the output. log file will look something like this:
Time/Date
command: echo "hello world"
output: hello world
command: echo "I am here"
output: I am here
command: ls
output: ... you get the point.