I'm wondering if there's a way to use the history command but only list commands that were run successfully. Likewise, would there be a way to list ones that produced errors?
I know each command has an exit status of:
0 - Successful execution of command
1 - Command fails because of an error during expansion or redirection, the exit status is greater than zero.
2 - Incorrect command usage
12 - Command found but not executable
127 - Command not found
and can be checked proceeding the command with
After running these 4 commands:
ls help lss ls nonexistentfile
I tried testing to print only successful commands (exit status of 0), which should be only 'ls' and 'help':
for j in `history | tail -5 | head -4 | cut -d ' ' -f5-`; do $j > cmd_out; if [[ `echo $?` != 0 ]]; then :; else echo $j | grep -v 'bash'; fi; done; rm cmd_out
ls help bash: lss: command not found ls bash: nonexistentfile.txt: command not found
This somewhat works, but there are two problems:
- For some reason,
grep -v 'bash'is not excluding lines containing 'bash', but if I do
grep 'bash', it will include only the lines with 'bash', I'm not sure why one works and not the other.
- It's separating each line/command by string. The last
lscommand should be
ls nonexistentfile, but it's running
nonexistentfileas separate commands.
Any idea on what I should adjust in the command, or how to go about doing this?