我想处理Ruby中的命令行输入:
> cat input.txt | myprog.rb
> myprog.rb < input.txt
> myprog.rb arg1 arg2 arg3 ...
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?特别是我想处理空白STDIN,我希望有一个优雅的解决方案.
#!/usr/bin/env ruby
STDIN.read.split("\n").each do |a|
puts a
end
ARGV.each do |b|
puts b
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试为node.js创建一个可在多个环境中工作的脚本.特别是对我来说,我在OS X和Ubuntu之间来回切换.在前者中,Node安装为node,但后者是nodejs.在我的脚本的顶部,我可以:
#!/usr/bin/env node
Run Code Online (Sandbox Code Playgroud)
要么
#!/usr/bin/env nodejs
Run Code Online (Sandbox Code Playgroud)
我宁愿让脚本作为任一环境的可执行文件运行,只要安装了节点而不是让一个或另一个必须指定命令(./script-name.jsvs. node script-name.js).
是否有任何方法可以指定备份hashbang或者在node.js的任何一种情况下都兼容?
我需要一个可直接执行的python脚本,所以我用它启动了该文件#!/usr/bin/env python.但是,我也需要无缓冲输出,所以我试过#!/usr/bin/env python -u,但是失败了python -u: no such file or directory.
我发现#/usr/bin/python -u的作品,但我需要它来获得python在PATH支持虚拟env环境.
我有什么选择?
可能重复:
Python输出缓冲
有没有办法从我的代码中获得运行python -u的效果?如果失败了,我的程序可以检查它是否在-u模式下运行并退出并显示错误消息,如果没有?这是在linux上(ubuntu 8.10服务器)
我希望能够在GDB中设置一个断点,让它运行到那一点 - 并在此过程中,打印出已经"逐步完成"的行.
这是一个例子,基于这个带有a main和a函数的简单文件,每个都有两个断点:
$ cat > test.c <<EOF
#include "stdio.h"
int count=0;
void doFunction(void) {
// two steps forward
count += 2;
// one step back
count--;
}
int main(void) {
// some pointless init commands;
count = 1;
count += 2;
count = 0;
//main loop
while(1) {
doFunction();
printf("%d\n", count);
}
}
EOF
$ gcc -g -Wall test.c -o test.exe
$ chmod +x test.exe
$ gdb -se test.exe
...
Reading symbols from /path/to/test.exe...done.
(gdb) b …Run Code Online (Sandbox Code Playgroud) I'm trying to understand one of the answers to this question:
Cannot pass an argument to python with "#!/usr/bin/env python"
#!/bin/sh
''''exec python -u -- "$0" ${1+"$@"} # '''
Run Code Online (Sandbox Code Playgroud)
This works well, but I do not understand why it works with four ticks at the beginning of that line rather than three.
In addition, why the hash near the end of that string?
我在我的python脚本的顶部有规范的shebang.
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
但是,我仍然经常想在运行脚本时将无缓冲的输出导出到日志文件中,所以我最终调用:
$ python -u myscript.py &> myscript.out &
Run Code Online (Sandbox Code Playgroud)
我可以像这样在shebang中嵌入-u选项吗?
#!/usr/bin/env python -u
Run Code Online (Sandbox Code Playgroud)
并且只打电话:
$ ./myscript.py &> myscript.out &
Run Code Online (Sandbox Code Playgroud)
......仍然没有缓解?我怀疑这不起作用,并希望在尝试之前检查.有什么东西可以实现这一目标吗?
为了使用es6,我们在命令行中传递和声标志
node --harmony myscript.js
Run Code Online (Sandbox Code Playgroud)
有没有办法从文件内部执行此操作,例如use harmony?
#! /usr/bin/node
use harmony
class MyScript {
constructor (options) {
this.options = options;
}
get options () {
return this.options
}
}
Run Code Online (Sandbox Code Playgroud) 我知道这是不正确的.我只是想知道perl如何解析它.
所以,我正在玩perl,我想要的是perl -ne我输入perl -ie的行为是有趣的,我想知道发生了什么.
$ echo 1 | perl -ie'next unless /g/i'
Run Code Online (Sandbox Code Playgroud)
那就是perl Aborted (core dumped).阅读perl --help我看到-i需要扩展备份.
-i[extension] edit <> files in place (makes backup if extension supplied)
Run Code Online (Sandbox Code Playgroud)
对于那些不知道-e只是eval的人.所以我想三件事中的一件可能发生了,或者被解析为
perl -i -e'next unless /g/i' 我得到了undef,剩下的就是e的论据perl -ie 'next unless /g/i' 我得到了论证e,其余的就像文件名一样悬挂perl -i"-e'next unless /g/i'" 整件事作为我的论据我跑的时候
$ echo 1 | perl -i -e'next unless /g/i'
Run Code Online (Sandbox Code Playgroud)
该计划不会中止.这让我相信'next unless /g/i'没有被解析为文字参数-e.毫不含糊地将上述方法解析,并且它具有不同的结果.
那是什么?好好玩一点,我得到了
$ echo 1 | perl …Run Code Online (Sandbox Code Playgroud) 什么是在shebang线中包含解释器选项的可接受的,可移植的方式,即.我该怎么办呢
#!/usr/bin/env python -c
Run Code Online (Sandbox Code Playgroud)
或者(更重要的是)类似的东西
#!/usr/bin/env java -cp "./jars/*:./src" -Xmn1G -Xms1G -server
Run Code Online (Sandbox Code Playgroud)
并让它正确解析?现在ubuntu似乎只是将整个事情放在一起,尽管其他系统会解析这个问题没有问题.
http://en.wikipedia.org/wiki/Shebang_%28Unix%29
描述了问题,但没有解决方案.