Posts Tagged ‘shell’
Grepping lines with multiple matches
I want to filter log files by matching lines that contains all of set of matches (I don’t want to alternate them).
For example lines like:
RawURL /1.2/user/authorize?ip=10.254.142.175&method=GET
I want to match lines that contain the terms “RawURL” and “/1.2/user/authorize?” without resorting to piping multiple greps:
$ grep RawURL | grep /1.2/user/authorize? *.txt
This means I want a regexp like:
RawURL.+\/1.2\/user\/authorize
Translating to grep extended regex, this becomes:
$ grep -E RawURL.+/1.2/user/authorize *.txt
Note: There is no need to enforce non-greedy matching.
Regular expressions, character classes and special characters
The expression:
RawURL.+\/1.2\/user\/authorize
Is not equivalent to:
RawURL[.]+\/1.2\/user\/authorize
Because in the second one, the special character ‘.’ is no longer special. Character classes have all special characters turned off.
Rake — shell task failure
For my current project, we’re running cucumber as one of our tasks. Our report generation runs afterwards — which we need to happen regardless of cucumber’s exit code (cucumber exits with code 1 when any tests fail).
Handling shell task failure
We noticed that if cucumber fails, rake exits immediately — without running our subsequent report tasks.
We’re running cucumber with rake’s ruby function — which runs the ruby interpreter in its own process by invoking a shell command.
Pseudo-stacktrace (irrelevant source lines omitted):
Shell::CommandProcessor.system(command, *opts) Rake::Win32.rake_system(*cmd) FileUtils.sh(*args, &block) FileUtils.ruby(*args, &block)
The key to this is the behaviour of rake’s FileUtils.sh. Examining the source shows:
If it is not supplied a block, and the shell command fails, and rake exits with error status.
Aside: For some reason, Kernel.system echoes errors.
Example
This task will cause rake to exit immediately with status 1:
task 'this-one-fails' do
ruby("xxx")
end
while the following task does not cause rake to terminate normally, any subsequent task will still run:
task 'this-one-succeeds' do
ruby("xxx") do |success, exit_code|
puts "Exited with code: #{exit_code.exitstatus}" unless success
end
end
Getting the last exit status
The $? variable contains the status of the last child process to terminate. This may be useful if steps subsequent to the failed one require this information.
