Run command after Prompting from User...

U

Usman Akram

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 :-(

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: (e-mail address removed)
 
S

Sandor Szücs

i do this but not working :-(

user_name =3D gets

system ('adduser -m $user_name')

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



regards, Sandor Sz=FCcs
--=
 
R

Raveendran Jazzez

Usman said:
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
http://raveendran.wordpress.com
 
U

Usman Akram

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: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top