Hi

I am a new user of python and I want to use command.getoutput as follows:
val="/usr1/test/hello/ "
date1 =commands.getoutput('ls -lrt $val | grep "LATEST" | cut -d ">" -f2 | tr -d " "')
My problem is that I want to use "/usr1/test/hello/ " inside the command using variable "val"(Which is already defined above.).

So my query is that what is the syntax for using a variable inside the command.

Recommended Answers

All 2 Replies

Sorry, I don't have Linux so I can't test it out, but try to build your command string before using it as an argument:

val="/usr1/test/hello/ "

command_str = 'ls -lrt $%s | grep "LATEST" | cut -d ">" -f2 | tr -d " "' % val

# test it
print command_str

"""
my output -->
ls -lrt $/usr1/test/hello/  | grep "LATEST" | cut -d ">" -f2 | tr -d " "
"""

#date1 = commands.getoutput(command_str)

Not sure if you need the $

Thanks for suggestion.
It is working without $.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.