Hacker News new | comments | show | ask | jobs | submit login
How to determine which commands failed/succeeded in your Bash_History file?
5 points by bashit 9 days ago | hide | past | web | favorite | 2 comments
I have a python application I use for taking notes in markdown. It automates several tasks for me and one of those tasks is parsing my command history and logging certain commands to a markdown file.

Parsing out the command and even the timestamp of each command (if your bashrc is setup correctly) in the Bash_History file is easy. However, what if one of these commands was typed wrong or perhaps I forgot a parameter or flag which caused the command to fail. There isn't any bash/zsh environment variable I can set to add this bit of information to the bash_history file like you would for including the timestamp.

How can I effectively and most easily solve this problem?

To be extra clear. Say I have a bash_history file with the following commands. How can I distinguish between failed/succeeded commands?

---Bash_History File---

lsla

ls -la

cdd

cd

---END OF FILE---

For simplicity, lets say I want to parse this file and log all these commands that succeeded but ignore the commands that failed which are <lsla> and <cdd>. Obviously <lsla> is wrong as it's missing the space and '-' character. The <cdd> is wrong cause (for sake of argument) lets say I fat fingered this command. That leaves two commands that succeeded. For a Bash_History file of 5,000 commands this becomes more complicated as each command could have several ways it could look (such as different params and options). So a lookup "dictionary" type of method wouldn't work here.

Therefore, how can I easily determine which commands succeeded and which failed?

What method would best work across both bash/zsh?

Are there environment variables I have to set? For example, to see the timestamp of each command I set the following variable in my bashrc

HISTTIMEFORMAT="%d/%m/%y %T "

Do I need to write my own bash_history program for this? Would like to avoid that.






> Do I need to write my own bash_history program for this? Would like to avoid that.

I didn't even know about HISTTIMEFORMAT, but seeing as how it puts the time in a "comment", which I didn't even know was possible for these readline history files, maybe there's some bash hook you can define that will have access to the return code of each command run and append that to the history file with a comment character on your own. This working depends on having configured bash so that it writes to the history file incrementally (as soon as each command is run and not until you exit bash).

If that fails, in your shoes, I would look into patching bash. I don't think you'll find explicit support for what you want (you've already looked in the documentation, right?), but I'd be happy to be proven wrong.


You may be interested in this HN comment from roughly a year ago, in which a user created a playful bash prompt script that displays the number of successful commands run in a row: https://news.ycombinator.com/item?id=15563117. Sample prompt:

  [0] /home/bar/g3 14:19 $ echo "boo"
  boo

  [1] /home/bar/g3 14:19 $
With this as an example ($LAST_STATUS in particular) it seems like writing a custom bash_history program would be very possible.



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: