0

I am trying to send a variable from my code in Python to a shell command in Ubuntu. Can anyone help me with how to do that? Currently, I have

import os
s=5
command = os.popen('stress -c 5 -i 1 -m 1 --vm-bytes 128M -t s')

I want to send the s variable to this command instead of directly say the time of time-out.

4 Answers 4

4

Use the standard subprocess module:

import subprocess
s = 5
cmd = ['stress', '-c', '5', '-i', '1', '-m', '1',
      '--vm-bytes', '128M', '-t', str(s)]
subprocess.call(cmd)

Using the subprocess module, allows each argument to be passed separately and distinctly. You only have to convert them to strings.

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

2 Comments

The first method won't work if the value of s itself contains double quotes. The second method is really the only reliable method.
Sure, and I also agree the second method is more reliable. However, in my case the first method works.
0

I'm assuming the 's' in your command string is what you want to assume the value of your variable 's'? All you need to do is this:

command = os.popen('stress -c 5 -i 1 -m 1 --vm-bytes 128M -t ' + str(s))

If I misread it and you are trying to replace the 5, it is basically the same

command = os.popen('stress -c ' + str(s) + ' -i 1 -m 1 --vm-bytes 128M -t s')

4 Comments

While it will work, it's a bad habit. Either the values included in a command line should be properly escaped, or the command should be split into tokens on Python's side. If s comes from an untrusted source, it could be ; rm -rf / ;
@viraptor I agree about the escaping, but it looked like the variable was a constant in the OP's script so it was safe to do the quick version. As a technicality, rm -rf / doesn't work (I've tested it), you need rm -rf /* since rm won't let you delete root. Even then there are a handful of files it won't let you delete, but that will make the os nearly unusable.
I know it doesn't work - that's why I'm always using the broken version in examples :)
@viraptor Maybe I shouldn't have had all those drinks and played command line russian roulette that one night...
0

If you are using for example Google Colab and you are running the code with "!" in a line, you can put "$" behind the variable. for example:

folderName = "somedir"
!mkdir $folderName

or

!mkdir {folderName}

All according to This stackoverflow post

Comments

-2

This is a string replacement operation which you can get all the info here.

Or you can just concatenate the strings together. (good tutorial)

5 Comments

Use external links to support your answer, not as the entire answer itself.
I believe that the answer is string replacement and the links are supporting the answer by giving details how to do that....
There is no way to use this answer without consulting the external resource if you don't already know what the string replacment operation is. It's also not a good answer in the presence of quoting issues.
So what you are saying as long as I don't do the whole work and give exactly what is needed it is not an answer that is supposed to be accepted at all?
I see what you mean I didn't give enough context for the link, will do next time :).

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.