Reputation: 339
I was trying to execute the following 2 lines as Tcl scripts. As keyword1 sometimes doesn't exist in file1, grep returns status code 1, which exec treats as error and stops execute the second line. How do I force it run both lines no matter there is a match or not.
exec grep keyword_1 file_1 > report_1
exec grep keyword_2 file_2 > report_2
Upvotes: 0
Views: 7249
Reputation: 247142
You could ignore grep's exit status like this:
exec sh -c {grep keyword_1 file_1 > report_1; true}
exec sh -c {grep keyword_2 file_2 > report_2; true}
but, probably better to use catch
as @Dinesh suggests
Or use try
if you have a modern Tcl
try {exec grep keyword_1 file_1 > report_1} trap CHILDSTATUS {} {}
try {exec grep keyword_2 file_2 > report_2} trap CHILDSTATUS {} {}
Upvotes: 2
Reputation: 16436
You can catch the exceptions using catch
command.
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"
}
If you dont care status of the command which you are executing or the error messages, then it can be written simply as,
catch {exec grep keyword_1 file_1 > report_1}
catch {exec grep keyword_2 file_2 > report_2}
Reference : catch
Upvotes: 3