在Tcl中执行管道shell命令

时间:2016-04-18 13:28:42

标签: shell tcl

我想在Tcl中执行这些管道shell命令:

grep -v "#" inputfile | grep -v ">" | sort -r -nk7 | head

我试试:

exec grep -v "#" inputfile | grep -v ">" | sort -r -nk7 | head

并收到错误:

Error: grep: invalid option -- 'k'

当我尝试只管道其中两个命令时:

exec grep -v "#" inputfile | grep -v ">" 

我明白了:

Error: can't specify ">" as last word in command

更新:我还尝试了{}和{bash -c'...'}:

exec {bash -c 'grep -v "#" inputfile | grep -v ">"'} 

Error: couldn't execute "bash -c 'grep -v "#" inputfile | grep -v ">"'": no such file or directory

我的问题:如何在tcl脚本中执行初始管道命令?

由于

3 个答案:

答案 0 :(得分:2)

>在这里造成了问题。

您需要将其从tcl shell中删除,以使其在此处运行。

exec grep -v "#" inputfile | grep -v {\\>} | sort -r -nk7 | head

或(这是更好的,因为你少了grep

exec grep -Ev {#|>} inputfile | sort -r -nk7 | head    

如果您查看正在运行此目录的目录(假设为tclsh或类似),您可能会看到之前创建了一个奇怪命名的文件(即|)。

答案 1 :(得分:2)

纯Tcl:

-double

-integer可能比-k

更合适

编辑:在为{编写{基于0的} sort选项时,我错误地翻译了命令-index的基于1的lsort索引{1}}。现在已经纠正了。

文档:fileutil包,ifjoinlappendlrangelsortpackage,{{ 3}},putsregexp

答案 2 :(得分:2)

问题在于exec在自己(或在单词的开头)看到>时表示“特殊事物”,因为它表示重定向。不幸的是,没有实际的方法可以直接避免这种情况;这是Tcl的语法系统没有帮助的领域。你最终不得不做这样的事情:

exec grep -v "#" inputfile | sh -c {exec grep -v ">"} | sort -r -nk7 | head

您也可以将整个管道移动到Unix shell端:

exec sh -c {grep -v "#" inputfile | grep -v ">" | sort -r -nk7 | head}

虽然坦白说这是你可以在纯Tcl中做的事情,然后它也可以移植到Windows ......

相关问题