n
=
ARGV.size
puts " I was given #{n} arguments when I was called. "
puts " The arguments were: "
ARGV.each do |arg|
puts arg
end
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 " )
puts( " hello from ruby " )
exec( " pwd " )
puts( " hello again from ruby " )
2.windows:
#system(
"
notepad.exe
"
)
system( " cmd /c dir " )
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技术博客
--------------------------------------------------------------------------------
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
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 :
Class: GetoptLong
The GetoptLong Class allowsclass 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 -n 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: 0, 1, 2
Class methodsClass methods:
new
Instance methods:
each, each_option, error_message, get, get_option, ordering=, set_error, set_options, terminate, terminated?
Attributes:
error, ordering, quiet, quiet
The GetoptLong Class allowsclass 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 -n 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: 0, 1, 2
Class methodsClass 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
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: