使用 tcl 执行 shell 命令
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
之间没有space。 Tcl 的 exec
关心这个,因为它组装管道而不去外部 shell,而且它对这些事情更加挑剔。
备选方案之一,仅适用于路径名中没有 spaces 的情况,即:
# 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
lsearch
的 -regexp
选项比 -glob
更接近于 grep
所做的匹配,但在这种情况下它更慢且过大。
我想通过 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
之间没有space。 Tcl 的 exec
关心这个,因为它组装管道而不去外部 shell,而且它对这些事情更加挑剔。
备选方案之一,仅适用于路径名中没有 spaces 的情况,即:
# 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
lsearch
的 -regexp
选项比 -glob
更接近于 grep
所做的匹配,但在这种情况下它更慢且过大。