Use Python variable in bash command with os.system

I would like to enter a phone number from raw_input

and use this variable in my bash command to send a message. I don't know how to set the $ Phonenumber variable. Without RAW input, it works like a charm. I am using Python 2.7.

import os

Phonenumber = raw_input("Number?)

os.system("yowsup-cli demos --login PHONE:PASSWORD= --send '$Phonenumber' 'MESSAGE'")

      

+3


source to share


3 answers


If you want to use it os.system

to make your calls, you can simply format the command you pass to it:

import os
phone_number = raw_input("Number?")
os.system("yowsup-cli demos --login PHONE:PASSWORD= --send '{0}' 'MESSAGE'".format(phone_number))

      


However, chapner brings up a good point in his answer. You should really use a module subprocess

for this kind of thing as it is a much more flexible tool. To get the same behavior using subprocess

, just pass the list in subprocess.call

when all elements are separated by segment spaces:



import subprocess
phone_number = raw_input("Number?")
subprocess.call(["yowsup-cli", "demos", "--login" "PHONE:PASSWORD=", "--send", phone_number, "MESSAGE"])

      

You can read more about subprocess

here .

+4


source


Use subprocess

; among other features, it makes quoting much easier.



import subprocess

phone_number = raw_input("Number?")

subprocess.call(["yowsup-cli", "demos", "--login", "PHONE:PASSWORD=",
                 "--send", phone_number, "MESSAGE"])

      

+5


source


using:

os.system("yowsup-cli demos --login PHONE:PASSWORD= --send '%d' 'MESSAGE'" % Phonenumber)

      

+1


source







All Articles