0

I am executing exec_command in Python. The command is : find -path . -mmin -$time -print. And I am assigning time=100. But while executing the command, exec_command does not picks up the variable value. or even I do exec_command('echo $time'). It does not picks up the value.

1 Answer 1

1

Python and the shell have separate variables. Setting time in your Python script does not magically create a variable called $time that you can then use in shell commands. It especially does not magically create a shell variable on some other computer which is what it appears you're trying to do.

Instead, put the Python value into the string you're passing. For example:

command = 'find -path . -mmin -%d -print' % time
print(command)   # shows the exact command that will be executed
exec_command(command)

Be careful with this. It's very easy to create security holes if you don't know what you're doing, especially when you take strings from user input or other data sources you don't control. In this example I've used %d to substitute the value into the command, and this will keep anything but a number from being used, which should be safe.

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

1 Comment

Thnaks kindall!! this helps.

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.