Run command after Prompting from User

Hi,

   i am new in Ruby, and i got a problem, that is when i run the system
command..

     i want to add users to the Linux OS, so for that i have to prompt
the username from the user, and i store that username in a variable,

   Now please tell me that how can i use that variable in the system()
function of Ruby...

i do this but not working :frowning:

user_name = gets

system ('adduser -m $user_name')

so please any body tell me that how can i run the system command, by
using variable in the system() function…???

from
Name: Lucky
Email: ajaonchat@yahoo.com

···

--
Posted via http://www.ruby-forum.com/.

Usman Akram wrote:

i do this but not working :frowning:

user_name = gets

system ('adduser -m $user_name')

so please any body tell me that how can i run the system command, by
using variable in the system() function....???

U:>irb
irb(main):001:0> user_name = "timothy"
=> "timothy"
irb(main):002:0> puts "user_name is #{user_name}"
user_name is timothy

See "Strings" here:
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html

···

--
Posted via http://www.ruby-forum.com/\.

0)
Try that in the Interactive Ruby (irb) and you will get the problem.

1)
user_name is your local defined variable, $user_name is another, global
variable name.
$user_name.nil? # returns true

2)
If you want to use your variable in a string you have to use doublequotes "
instead of singlequotes ' and in ruby you can use the following syntax:
"mystring #{var}"
"mystring "+ var
"mystring " << var

one simple solution with proof of shell injection is:

tries = 0
username = ""
while not username.match(/\A[a-zA-Z0-9]+\Z/)
   tries = tries.succ
   exit! if tries > 3
print "username: "
username = gets.chomp.strip
end
system('adduser -m ' + username)

regards, Sandor Szücs

···

On 21.07.2008, at 12:00, Usman Akram wrote:

i do this but not working :frowning:

user_name = gets

system ('adduser -m $user_name')

--

Usman Akram wrote:

so please any body tell me that how can i run the system command, by
using variable in the system() function....???

Hi Akram,

May be it will help u more

irb(main):003:0> a=`ipconfig`
=> "\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter Local Area
Connecti
on:\r\n\r\n Connection-specific DNS Suffix . : \r\n IP
Address. .
. . . . . . . . . . : 192.168.1.45\r\n Subnet Mask . . . . . . .
. . . .
: 255.255.255.0\r\n Default Gateway . . . . . . . . . :
192.168.1.1\r\n"

irb(main):004:0> a
=> "\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter Local Area
Connecti
on:\r\n\r\n Connection-specific DNS Suffix . : \r\n IP
Address. .
. . . . . . . . . . : 192.168.1.45\r\n Subnet Mask . . . . . . .
. . . .
: 255.255.255.0\r\n Default Gateway . . . . . . . . . :
192.168.1.1\r\n"

irb(main):005:0>

Regards,
P.Raveendran

···

--
Posted via http://www.ruby-forum.com/\.

Hi Raveendran Jazzez,

    i am using Ruby for programming in the Linux OS not in Windows, so
plz help me to Run command after Prompting from User... on Linux
operating system, i want to add users to the linux os by using the Ruby
Programming....

from
Usman Akram
email: ajaonchat@yahoo.com

···

--
Posted via http://www.ruby-forum.com/.