Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何绕过;子进程异常退出”;_Shell_Tcl - Fatal编程技术网

Shell 如何绕过;子进程异常退出”;

Shell 如何绕过;子进程异常退出”;,shell,tcl,Shell,Tcl,我试图以Tcl脚本的形式执行以下两行代码。 由于file1中有时不存在关键字1,grep返回状态代码1,exec将其视为错误并停止执行第二行。我如何强制它运行两条线,无论是否有匹配 exec grep keyword_1 file_1 > report_1 exec grep keyword_2 file_2 > report_2 您可以使用catch命令捕获异常 if {[catch {exec grep keyword_1 file_1 > report_1} resul

我试图以Tcl脚本的形式执行以下两行代码。 由于file1中有时不存在关键字1,grep返回状态代码1,exec将其视为错误并停止执行第二行。我如何强制它运行两条线,无论是否有匹配

exec grep keyword_1 file_1 > report_1
exec grep keyword_2 file_2 > report_2

您可以使用
catch
命令捕获异常

if {[catch {exec grep keyword_1 file_1 > report_1} result]} {
    puts "problem in executing grep on file1"
    puts "Reason : $result"
}
if {[catch {exec grep keyword_2 file_2 > report_2} result]} {
    puts "problem in executing grep on file2"
    puts "Reason : $result"
}
如果您不关心正在执行的命令的状态或错误消息,那么它可以简单地写为

catch {exec grep keyword_1 file_1 > report_1}
catch {exec grep keyword_2 file_2 > report_2}

参考:

您可以使用
catch
命令捕获异常

if {[catch {exec grep keyword_1 file_1 > report_1} result]} {
    puts "problem in executing grep on file1"
    puts "Reason : $result"
}
if {[catch {exec grep keyword_2 file_2 > report_2} result]} {
    puts "problem in executing grep on file2"
    puts "Reason : $result"
}
如果您不关心正在执行的命令的状态或错误消息,那么它可以简单地写为

catch {exec grep keyword_1 file_1 > report_1}
catch {exec grep keyword_2 file_2 > report_2}

参考:您可以忽略grep的退出状态,如下所示:

exec sh -c {grep keyword_1 file_1 > report_1; true}
exec sh -c {grep keyword_2 file_2 > report_2; true}
但是,按照@Dinesh的建议,可能最好使用
catch

如果您有一个现代化的Tcl,也可以使用
试试

try {exec grep keyword_1 file_1 > report_1} trap CHILDSTATUS {} {}
try {exec grep keyword_2 file_2 > report_2} trap CHILDSTATUS {} {}

您可以忽略grep的退出状态,如下所示:

exec sh -c {grep keyword_1 file_1 > report_1; true}
exec sh -c {grep keyword_2 file_2 > report_2; true}
但是,按照@Dinesh的建议,可能最好使用
catch

如果您有一个现代化的Tcl,也可以使用
试试

try {exec grep keyword_1 file_1 > report_1} trap CHILDSTATUS {} {}
try {exec grep keyword_2 file_2 > report_2} trap CHILDSTATUS {} {}

还考虑搜索是否会更好地直接在TCL中完成。grep在野外的许多用途都是纯粹的过度使用。@Dinesh-Catch{…}效果很好,这正是我所需要的。非常感谢。也可以考虑在TCL中直接搜索是否更好。grep在野外的许多用途都是纯粹的过度使用。@Dinesh-Catch{…}效果很好,这正是我所需要的。非常感谢。请参阅并向下滚动至“儿童状态”部分。本质上,
exec
将非零退出状态处理为错误,当找不到模式时,
grep
返回“1”。谢谢,是的,我读过那篇文章,但我不知道它如何使用catch忽略此错误代码。请参阅并向下滚动到“子状态”部分。本质上,
exec
将非零退出状态处理为错误,当找不到模式时,
grep
返回“1”。谢谢,是的,我读过那篇文章,但我不知道它如何使用catch忽略此错误代码。