在Ruby脚本中执行shell脚本命令(Executing shell script commands in Ruby script)

编程入门 行业动态 更新时间:2024-10-20 21:03:11
在Ruby脚本中执行shell脚本命令(Executing shell script commands in Ruby script)

我有一个非常基本的问题。 我正在运行一个Ruby脚本来访问Linux中目录的内容。 执行ruby脚本时,目录将通过命令行传递。

我的问题是如何在ruby的命令中使用命令行参数?

我有这样的设置:

usrDirectory = ARGV[0] lsCmd = `ls -l`

我需要使用像ls -l usrDirectory这样的东西。 我可以像它一样将它插入命令吗?

I have a very basic question. I'm running a Ruby script to access the contents of a directory in Linux. The directory is passed through the command line when the ruby script is executed.

My question is how would I use the command line argument in the command for ruby?

I have it set like such:

usrDirectory = ARGV[0] lsCmd = `ls -l`

I need to use something like ls -l usrDirectory. Could I just insert it into the command like is?

最满意答案

以上是正确的,如果你想让ls输出到标准输出,这会让它更清洁:

system("ls", "-l", dir)

这将使Ruby打印输出到标准输出,而不是像上面那样将输出放在变量中。

The above is right, and if you want to have ls output to standard out, this makes it a little cleaner:

system("ls", "-l", dir)

This will make Ruby print the output to your standard out instead of putting the output in the variable as the above does.

更多推荐

本文发布于:2023-07-22 09:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   命令   shell   Ruby   script

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!