我有一个简单的bash脚本.
它的目的是监视http访问日志文件(test.log)并将更新的命中率输出到文件(out.log):
stdbuf -o0 tail -f test.log | awk -F'[ "]+' '{
ipcount[$1]++;
print "test" > "out.log"; #Truncate out.log
for (i in ipcount) {
printf "%15s - %d\n", i, ipcount[i] >> "out.log";
printf "%15s - %d\n", i, ipcount[i] }
}'
Run Code Online (Sandbox Code Playgroud)
主要逻辑是有效的.我唯一的问题是重定向到"out.log"似乎不起作用.最后一个printf将预期结果输出到标准输出.但另外两个printf没有输出任何东西到"out.log",我无法弄清楚为什么.out.log拥有所有权限(777)
我想将参数传递给来自 stdin 的节点脚本。
一般来说,我正在拍摄这样的东西
nodeScript.js | node {{--attach-args??}} --verbose --dry-run
Run Code Online (Sandbox Code Playgroud)
这将与
node nodeScript.js --verbose --dry-run
Run Code Online (Sandbox Code Playgroud)
这是一个简化的说明脚本,dumpargs.js
console.log("the arguments you passed in were");
console.log(process.argv);
console.log("");
Run Code Online (Sandbox Code Playgroud)
这样你就可以:
node dumpargs.js --verbose --dry-run file.txt
[ 'node',
'/home/bill-murray/Documents/dumpargs.js',
'--verbose',
'--dry-run',
'file.js' ]
Run Code Online (Sandbox Code Playgroud)
现在的问题是,如果该脚本出现在标准输入中(例如,通过cat或curl)
cat dumpars.js | node
the arguments you passed in were
[ 'node' ]
Run Code Online (Sandbox Code Playgroud)
有没有一种好方法可以将参数传递给它?
不是节点:使用 bash,
dumpargs.sh这次使用Run Code Online (Sandbox Code Playgroud)echo "the arguments you passed in were" printf "> $@" echo答案看起来像
Run Code Online (Sandbox Code Playgroud)cat dumpargs.sh …