LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2008, 10:55 PM   #1
wrapster
Member
 
Registered: May 2008
Posts: 155

Rep: Reputation: 30
needed help in ruby script where user interaction is necessary?


Hi all,

I am struck at this point and needed some help in ruby
I wanted to write a script that accepts the no of VNICs the user wants to create and
then ask for the name of each Vnic he wants
then use these names to create the corresponding Vnics

Eg: suppose that there are 2 Vnics to be created
Then the user should be asked for the names of these and upon entering the names the corresponding ones are created

PS: this is actually a part of the wrapper script that i am struck with!
The command to create the Vnic is already present(in solaris,Crossbow) i just need to know how to include the names provided by the user into the command so that the job is done!

This is what i came up with
Code:
#!/usr/bin/ruby

class Etherstub
   def initialize
      @estub ; @stub_name
      @vni
   end

def ether_create
    puts "enter the no of stubs you want"
    @estub = gets ;puts "Enter the names 1 by 1 "
    @estub.to_i.times {|@stub_name| ;@stub_name = gets }
       @estub.to_i.times { puts `/usr/sbin/dladm create-etherstub #{@stub_name}
` }
print "Stub created!!"
end
end

first = Etherstub.new
first.ether_create
This is the error i get
Code:
enter the no of stubs you want
2
Enter the names 1 by 1 
etherstub1
etherstub2

/usr/sbin/dladm: etherstub creation failed: object already exists
This is only creating 1 stub that too etherstub2 and not etherstub1
(I have checked it out)
I would like to know where am going wrong?

Thanks
 
Old 08-07-2008, 01:37 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
What happens if you run:
Code:
/usr/sbin/dladm create-etherstub etherstub1
/usr/sbin/dladm create-etherstub etherstub2
?
 
Old 08-07-2008, 02:46 AM   #3
wrapster
Member
 
Registered: May 2008
Posts: 155

Original Poster
Rep: Reputation: 30
it works perfectly!
in the shell there is no problem
but only while implementing it through ruby am facing this issue!
 
Old 08-07-2008, 04:33 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
What shows your code when modified as this (echo added) ?
Code:
@estub.to_i.times { puts `echo /usr/sbin/dladm create-etherstub #{@stub_name}` }
 
Old 08-07-2008, 06:56 AM   #5
wrapster
Member
 
Registered: May 2008
Posts: 155

Original Poster
Rep: Reputation: 30
Code:
enter the no of stubs you want
2
Enter the names 1 by 1 
etherstub1
etherstub2
/usr/sbin/dladm create-etherstub etherstub2
/usr/sbin/dladm create-etherstub etherstub2
Stub created!!bash-3.2#
This is what i get!
By the look of its working fine,inst it?
 
Old 08-07-2008, 08:01 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
It is not. It tries to create etherstub2 twice.
 
Old 08-07-2008, 08:10 AM   #7
wrapster
Member
 
Registered: May 2008
Posts: 155

Original Poster
Rep: Reputation: 30
yeah i know i figured out that after i wrote the mail!!!
And apparently if i choose to enter 3 stubs then the only stub being created is etherstub3 and so on....
What could be the reason?
 
Old 08-07-2008, 08:51 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by wrapster View Post
What could be the reason?
You stubbornly insisting using ruby while shell scripting is so easy ...
 
Old 08-07-2008, 09:25 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Alternatively, you can fix your script that way:
replace
Code:
    @estub.to_i.times {|@stub_name| ;@stub_name = gets }
       @estub.to_i.times { puts `/usr/sbin/dladm create-etherstub #{@stub_name}
    '}
by
Code:
    @estub.to_i.times {|@stub_name|
        @stub_name = gets
        puts `/usr/sbin/dladm create-etherstub #{@stub_name}`
    }

Last edited by jlliagre; 08-07-2008 at 09:36 AM.
 
Old 08-07-2008, 11:35 PM   #10
wrapster
Member
 
Registered: May 2008
Posts: 155

Original Poster
Rep: Reputation: 30
the point here in not of being stubborn but just a matter of later conveniency ....
Planning on framing a GUI once the console stuff are sorted out so i decided to go in for RUBY!
 
Old 08-08-2008, 01:50 AM   #11
wrapster
Member
 
Registered: May 2008
Posts: 155

Original Poster
Rep: Reputation: 30
Oh cool,
Its working
thanks for your help !
But i would really love to know of any books that gives me a detailed desc of all these.
I do have "beginning ruby" by peter cooper..
But i the input and outputs are not that clearly described!!
This is the only part that i am not too comfortable with so could you specify any good tutorials where i can learn how to code for a user interaction scenario?

Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Needed help in a ruby script with user interaction involved! wrapster Solaris / OpenSolaris 2 08-06-2008 10:47 PM
[Ruby] Tar'ing or Zip'ing with ruby script lmcilwain Programming 2 01-19-2007 06:56 PM
shell script interaction sudhasmyle Linux - Software 1 12-08-2004 03:34 PM
Interaction in shell script sudhasmyle Programming 1 12-08-2004 07:27 AM
user interaction disabling? dafri Linux - General 2 07-20-2003 05:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration