[英]execute shell command with tcl
我想通過tcl執行以下shell命令:
cat $file_path/file | grep test > $file_path/des_file
我使用exec但出現以下錯誤:
cat :|grep No such file or directory
如何使此命令與tcl一起正常工作
我敢打賭,你實際上是在寫:
cat $file_path/file |grep test > $file_path/des_file
之間沒有空格|
和grep
。 Tcl的exec
對此很在意,因為它無需流向外部外殼就可以組裝管道,並且對這些事情更加挑剔。
一種替代方法是僅在路徑名中沒有空格的情況下適用:
# Or however you want to make the script, such as popping up a dialog box in a GUI
set shell_script "cat $file_path/file |grep test > $file_path/des_file"
exec sh -c $shell_script
盡管您也可以不用cat
就能做到:
exec grep test < $file_path/file > $file_path/des_file
就是說,因為它是grep
,所以您可以完全在Tcl中完成它:
# Read in all the lines
set f [open $file_path/file]
set lines [split [read $f] \n]
close $f
# Filter the lines
set matches [lsearch -all -inline -glob $lines *test*]
# Write out the filtered lines
set f [open $file_path/des_file w]
puts $f [join $matches \n]
close $f
該-regexp
選項lsearch
是一個更匹配什么grep
不是做-glob
,但它的速度較慢,矯枉過正這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.