我想到了这个命令
pub global list -v 2>/dev/null | awk '{split($0, a," "); printf \
"pub global activate "; if(a[8]=="") printf a[3]; else printf \
"-sgit " a[8]; print "" }' | xargs -0 bash -c
更新我所有全局激活的包。
pub global list -v
输出
FINE: Pub 1.9.0-edge.43835
MSG : dart_style 0.1.3
MSG : den 0.1.5
MSG : linter 0.0.1 from Git repository "git@github.com:dart-lang/linter.git"
MSG : polymer 0.15.5+1
MSG : stagehand 0.1.5+4
MSG : test_runner 0.2.16
上面的命令从输出中生成这些命令
pub global activate dart_style
...
pub global activate -sgit "git@github.com:dart-lang/linter.git"
...
这很复杂。 有没有更简单的方法?
最佳答案
好吧,您不需要在 awk
中应用 split
。
另一个改进,摆脱 if
条件并使用 ternary
运算符(如 Etan 在评论中解释):
awk '{var=$8==""?$3:"-sgit "$8;print "pub global activate "var }'
最后的内容应该接近于:
pub global list -v 2>/dev/null |\
awk '{var= $8==""? $3: "-sgit " $8
print "pub global activate "var }'| bash -s
或者 system
调用可以在 awk
代码中完成:
pub global list -v 2>/dev/null |\
awk '{var=$8=="" ? $3: "-sgit " $8
cmd="pub global activate "var
system(cmd)
close(cmd)}'
关于bash - 更新所有全局激活的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585049/