trengrj / recent

log bash history to an sqlite database
MIT License
53 stars 2 forks source link

tcsh support #9

Open saltlakeryan opened 2 years ago

saltlakeryan commented 2 years ago

I've hacked together tcsh support by creating a ~/bin/tcsh-log-recent.sh and configuring .cshrc to call it (similar to the bash setup):

#!/bin/tcsh

#usage: call script with arguments: PID, EXIT_CODE, HISTORY_LINE (formatted as described below)
# make sure to run this to set proper history format in .cshrc:
# alias precmd '~/bin/tcsh-log-recent.sh $$ $? `history 1`'
#
# expect history command to output in this format (though only one line):
#     55    14:58   vim foo bar bla
#     56    14:58   ls /tmp /usr
#     60    14:59   which command
#     ...

set CMD_PID=$1
set CMD_RESULT=$2
set CMD_SEQUENCE=$3
set CMD_HISTORY=`echo $* | cut -d ' ' -f 5-`

log-recent -p $CMD_PID -r $CMD_RESULT -c " $CMD_SEQUENCE  $CMD_HISTORY"
chmaynard commented 1 year ago

@saltlakeryan I would like to write a similar shell script for bash so that I can log commands in other contexts. In your script, can you explain what the set CMD_HISTORY line is doing?

saltlakeryan commented 1 year ago

Yeah, my understanding is that echo $* outputs all of the arguments to the current script. Since the script is called with the arguments of $$, $?, and history 1, it should echo out something like:

4075 0 25 14:00 vim /tmp/somefile.txt

The cut command uses space as a delimiter and captures everything from the 5th field onward. In the above example, it would get trimmed to:

vim /tmp/somefile.txt

The only issue I've noticed is that wildcards and other special shell interpretations get captured after evaluation. For example, if I execute ls * in a directory with file1, file2, and file3, it gets stored in the sqlite db as ls file1 file2 file3. Here's an example of that:

mypc [/tmp/test]% touch file1 file2 file3 file4
mypc [/tmp/test]% ls *
file1  file2  file3  file4
mypc [/tmp/test]% recent -n 1
2022-10-05 14:07:40 ls file1 file2 file3 file4