我有一个非常基本的问题。 我正在运行一个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.
更多推荐
发布评论