# Run command after Prompting from User

**URL:** <https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635>\
**Category:** Ruby\
**Created:** [July 21, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635 "2008-07-21T12:05:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajaonchat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ajaonchat](https://www.ruby-forum.com/u/ajaonchat)\
**Post date:** [July 21, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635/1 "2008-07-21T12:05:46Z")

</div>

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: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ajaonchat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ajaonchat](https://www.ruby-forum.com/u/ajaonchat)\
**Post date:** [July 21, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635/2 "2008-07-21T18:52:28Z")

</div>

Usman A. wrote:

> 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…???

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](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html)

---

<div class="post-metadata">

**Author:** ![ajaonchat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ajaonchat](https://www.ruby-forum.com/u/ajaonchat)\
**Post date:** [July 21, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635/3 "2008-07-21T18:58:50Z")

</div>

On 21.07.2008, at 12:00, Usman A. wrote:

> i do this but not working ☹
> 
> user\_name = gets
> 
> system (‘adduser -m $user\_name’)

1. 

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

1. 

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

---

<div class="post-metadata">

**Author:** ![ajaonchat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ajaonchat](https://www.ruby-forum.com/u/ajaonchat)\
**Post date:** [July 22, 2008, 9:01am UTC](https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635/4 "2008-07-22T09:01:31Z")

</div>

Usman A. 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

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![ajaonchat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@ajaonchat](https://www.ruby-forum.com/u/ajaonchat)\
**Post date:** [July 23, 2008, 6:25am UTC](https://www.ruby-forum.com/t/run-command-after-prompting-from-user/143635/5 "2008-07-23T06:25:22Z")

</div>

Hi Raveendran J.,

```
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 A.  
email: removed\_email\_address@domain.invalid
