The question comes from this question here: Save all the terminal output to a file, specifically this post: https://unix.stackexchange.com/a/323920/478293 but I can't post a comment on there asking this since I need 50 reputation and direct messaging the editor or poster seems non-existent on this site....
Ok, so I've tried adding some lines from the savelog block to the smart script block to make this work for myself but I don't know if I did it right... It says the rawlog file doesn't exist, which I guess is kinda true since it's trying to convert a rawlog file that doesn't yet exist, how do I make it so that it wait till it exists?
I'm guessing I need an if else statement somewhere in there but not sure what to use to tell it to wait until the file is created and then convert and have a readable text file copy of it, along with the original rawlog file, in the same directory automatically without needing to invoke the savelog command in terminal. Any ideas?
My code:
# Execute "script" command just once
smart_script(){
# if there's no SCRIPT_LOG_FILE exported yet
if [ -z "$SCRIPT_LOG_FILE" ]; then
# make folder paths
logdirparent=~/Terminal_typescripts
logdirraw=raw/$(date +%F)
logdir=$logdirparent/$logdirraw
logfile=$logdir/$(date +%F_%T).$$.rawlog
txtfile=$logdir/$(date +%F_%T).$$'.txt'
# if no folder exist - make one
if [ ! -d $logdir ]; then
mkdir -p $logdir
fi
export SCRIPT_LOG_FILE=$logfile
export SCRIPT_LOG_PARENT_FOLDER=$logdirparent
# make .rawlog readable and save it to .txt file
cat $SCRIPT_LOG_FILE | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > $txtfile
# quiet output if no args are passed
if [ ! -z "$1" ]; then
script -f $logfile
script -f $txtfile
else
script -f -q $logfile
script -f -q $txtfile
fi
exit
fi
}
# Start logging into new file
alias startnewlog='unset SCRIPT_LOG_FILE && smart_script -v'
# Manually saves current log file: $ savelog logname
savelog(){
# make folder path
manualdir=$SCRIPT_LOG_PARENT_FOLDER/manual
# if no folder exists - make one
if [ ! -d $manualdir ]; then
mkdir -p $manualdir
fi
# make log name
logname=${SCRIPT_LOG_FILE##*/}
logname=${logname%.*}
# add user logname if passed as argument
if [ ! -z $1 ]; then
logname=$logname'_'$1
fi
# make filepaths
txtfile=$manualdir/$logname'.txt'
rawfile=$manualdir/$logname'.rawlog'
# make .rawlog readable and save it to .txt file
cat $SCRIPT_LOG_FILE | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > $txtfile
# copy corresponding .rawfile
cp $SCRIPT_LOG_FILE $rawfile
printf 'Saved logs:\n '$txtfile'\n '$rawfile'\n'
}
shopt -s -o xtrace # debug
shopt -s -o verbose # debug
to the top of your functions as two separate lines, the additional output will show you what's wrong.