ruby system call

 

n = ARGV.size
puts 
" I was given #{n} arguments when I was called. "
puts 
" The arguments were: "
ARGV.each 
do  |arg|
  puts arg
end

 1.linux:

   

system( " echo " , " hello from the system " )
puts(
" hello from ruby " )
exec(
" pwd " )
puts(
" hello again from ruby " )

 

2.windows:

 

#system( " notepad.exe " )
system(
" cmd /c dir  " )

 

3.调用shell的六种方法:

转载至http://blackanger.blog.51cto.com/140924/43730

 

 六种用ruby调用执行shell命令的方法  

--------------------------------------------------------------------------------

2007 - 09 - 23   12 : 14 : 41  标签:shell ruby ruby调用shell命令   [推送到博客圈] 


版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http:
// blackanger.blog.51cto.com/140924/43730 
碰到需要调用操作系统shell命令的时候,Ruby为我们提供了六种完成任务的方法:
1 .Exec方法:
    Kernel#exec方法通过调用指定的命令取代当前进程:
  例子:
      $ irb
      
>>  exec  ' echo "hello $HOSTNAME" '
         hello nate.local
      $
值得注意的是,exec方法用echo命令来取代了irb进程从而退出了irb。主要的缺点是,你无法从你的ruby脚本里知道这个命令是成功还是失败。
 
2 .System方法。
  Kernel#system方法操作命令同上, 但是它是运行一个子shell来避免覆盖当前进程。如果命令执行成功则返回true,否则返回false。
 
 $ irb             
  
>>  system  ' echo "hello $HOSTNAME" '
  hello nate.local
  
=>   true
  
>>  system  ' false '  
  
=>   false
  
>>  puts $ ?
  
256
  
=>  nil
  
>>  
3 .反引号(Backticks,Esc键下面那个键)
$ irb
  
>>  today  =  `date`
  
=>   " Mon Mar 12 18:15:35 PDT 2007n "  
  
>>  $ ?
  
=>  # < Process::Status: pid = 25827 ,exited( 0 ) >
  
>>  $ ? .to_i
  
=>   0
这种方法是最普遍的用法了。它也是运行在一个子shell中。
4 .IO#popen
  $ irb
  
>>  IO.popen( " date " |f| puts f.gets }
  Mon Mar 
12   18 : 58 : 56  PDT  2007
  
=>  nil
5 .open3#popen3
$ irb
  
>>  stdin, stdout, stderr  =  Open3.popen3( ' dc '
  
=>  [# < IO: 0x6e5474 > , # < IO: 0x6e5438 > , # < IO: 0x6e53d4 > ]
  
>>  stdin.puts( 5 )
  
=>  nil
  
>>  stdin.puts( 10 )
  
=>  nil
  
>>  stdin.puts( " + " )
  
=>  nil
  
>>  stdin.puts( " p " )
  
=>  nil
  
>>  stdout.gets
  
=>   " 15n "  
6 .Open4#popen4
$ irb
  
>>  require  " open4 "  
  
=>   true
  
>>  pid, stdin, stdout, stderr  =  Open4::popen4  " false "  
  
=>  [ 26327 , # < IO: 0x6dff24 > , # < IO: 0x6dfee8 > , # < IO: 0x6dfe84 > ]
  
>>  $ ?
  
=>  nil
  
>>  pid
  
=>   26327
  
>>  ignored, status  =  Process::waitpid2 pid
  
=>  [ 26327 , # < Process::Status: pid = 26327 ,exited( 1 ) > ]
  
>>  status.to_i
  
=>   256

 
 

 
本文出自 “引路人” 博客,请务必保留此出处http:
// blackanger.blog.51cto.com/140924/43730

本文出自 51CTO.COM技术博客 


 

4.cmdline options

require  ' getoptlong'

parser
= GetoptLong.new

parser.set_options([
" -h " , " --help " ,GetoptLong::NO_ARGUMENT],
                          [
" -t " , " --test " ,GetoptLong::REQUIRED_ARGUMENT])
                        
loop   do
  begin
    opt,arg
= parser.get
    break 
if   not  opt
    puts(
" #{opt} #{arg} " )
    
    
case  opt
      when 
" -h "
        puts 
" Usage:This is where we should write some helpful information about this.  "
        break
      when 
" -t "
        puts 
" Testing the argument feature where you put #{arg} "
      
end
      
      rescue
=> err
      break
    
end
  
end
  

 

 

-------------------------------------------------------------   Class: GetoptLong
The GetoptLong 
class allows you to parse command line options similarly to the GNU getopt_long() C library call. Note, however, that GetoptLong is a pure Ruby implementation.

GetoptLong allows 
for POSIX-style options like --file as well as single letter options like -f

The empty 
option -- (two minus symbols) is used to end option processing. This can be particularly important if options have optional arguments.

Here 
is a simple example of usage:

    # 
== Synopsis
    #
    # hello: greets user, demonstrates 
command line parsing
    #
    # 
== Usage
    #
    # hello [
OPTION] ... DIR
    #
    # 
-h, --help:
    #    show help
    #
    # 
--repeat x, -n x:
    #    repeat x times
    #
    # 
--name [name]:
    #    greet user by name, 
if name not supplied default is John
    #
    # 
DIR: The directory in which to issue the greeting.

    require 
'getoptlong'
    require 'rdoc/usage'

    opts 
= GetoptLong.new(
      [ 
'--help', '-h', GetoptLong::NO_ARGUMENT ],
      [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
      [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
    )

    
dir = nil
    name 
= nil
    repetitions 
= 1
    opts.each 
do |opt, arg|
      
case opt
        when 
'--help'
          RDoc::usage
        when 
'--repeat'
          repetitions = arg.to_i
        when 
'--name'
          if arg == ''
            name = 'John'
          else
            name 
= arg
          
end
      
end
    
end

    
if ARGV.length != 1
      puts 
"Missing dir argument (try --help)"
      
exit 0
    
end

    
dir = ARGV.shift

    
Dir.chdir(dir)
    
for i in (1..repetitions)
      
print "Hello"
      
if name
        
print ", #{name}"
      
end
      puts
    
end

Example 
command line:

    hello 
-6 --name -- /tmp

-------------------------------------------------------------------------------

Constants:
ORDERINGS:         [REQUIRE_ORDER 
= 0, PERMUTE = 1, RETURN_IN_ORDER = 2]
ARGUMENT_FLAGS:    [NO_ARGUMENT 
= 0, REQUIRED_ARGUMENT = 1,     OPTIONAL_ARGUMENT = 2]
STATUS_TERMINATED: 
012

Class methods:
new

Instance methods:
each, each_option, error_message, get, get_option, ordering=, set_error, set_options, terminate, terminated?

Attributes:
error, ordering, quiet, quiet

5.cmdline arguments

 

n = ARGV.size
puts 
" I was given #{n} arguments when I was called. "
puts 
" The arguments were: "
ARGV.each 
do  |arg|
  puts arg
end

 

 

6.use ruby to gain environmental variables:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值