Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables to a subprocess call [duplicate]

I am trying to pass my variables from raw_input to my subprocess command. I am new to Python. Any help would he appreciated.

#!/usr/bin/python  import subprocess  print "\nWhat user name" username = str(raw_input('username: ')) print "\nWhat is the user id" userid = int(raw_input('Enter user id: ')) print "\nWhat is the user\'s primary group?" primarygroup = int(raw_input('Enter group: ')) print "\nWhat is the user\'s secondary group?" secondarygroup = int(raw_input('Enter group: '))  subprocess.call(['useradd' '-m' '-g' _primarygroup '-G' _secondarygroup '-u' _userid _username])  print"\nThe user has been added" 
like image 1000
rsouthard Avatar asked Jan 25 '11 15:01

rsouthard


1 Answers

Try separating the values with commas:

subprocess.call(['useradd', '-m', '-g', _primarygroup, '-G', _secondarygroup, '-u', _userid, _username]) 

See http://docs.python.org/library/subprocess.html#subprocess.call - It takes an array where the first argument is the program and all other arguments are passed as arguments to the program.

Also don't forget to check the return value of the function for a zero return code which means "success" unless it doesn't matter for your script if the user was added successfully or not.

like image 186
ThiefMaster Avatar answered Sep 22 '22 09:09

ThiefMaster