Skip to content

Instantly share code, notes, and snippets.

@wildkatz2004
Last active April 13, 2018 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wildkatz2004/24af1a497fadaedfa172971dc8ae4736 to your computer and use it in GitHub Desktop.
Save wildkatz2004/24af1a497fadaedfa172971dc8ae4736 to your computer and use it in GitHub Desktop.
#!/bin/bash
# shellcheck disable=2034,2059
true
# shellcheck source=lib.sh
CHECK_CURRENT_REPO=1 . <(curl -sL https://raw.githubusercontent.com/wildkatz2004/wordpress-vm/master/lib.sh)
unset CHECK_CURRENT_REPO
# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
debug_mode
if [[ "no" == $(ask_yes_or_no "Do you want log everything from the terminal?") ]]
then
echo "Skipping log creation..."
sleep 1
else
# Secure phpMyadmin
if [ -f $UNIXUSER_ALIAS]
then
rm $UNIXUSER_ALIAS
fi
touch "$UNIXUSER_ALIAS"
cat <<-UNIXUSER-ALIAS > "$UNIXUSER_ALIAS"
# Make some possibly destructive commands more interactive.
alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'
# Add some easy shortcuts for formatted directory listings and add a touch of c$
alias ll='ls -lF --color=auto'
alias la='ls -alF --color=auto'
alias ls='ls -F'
# Make grep more user friendly by highlighting matches
# and exclude grepping through .svn folders.
alias grep='grep --color=auto --exclude-dir=\.svn'
# Shortcut for using the Kdiff3 tool for svn diffs.
alias svnkdiff3='svn diff --diff-cmd kdiff3'
# 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
# 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
# quiet output if no args are passed
if [ ! -z "$1" ]; then
script -f $logfile
else
script -f -q $logfile
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'
}
UNIXUSER-ALIAS
fi
echo "smart_script" >> "/home/$UNIXUSER/.bashrc"
exit 0
@wildkatz2004
Copy link
Author

wildkatz2004 commented Apr 13, 2018

To run:
Paste all of the following in to terminal and hit Return: curl -L -o 'smart_script.sh' https://gist.githubusercontent.com/wildkatz2004/24af1a497fadaedfa172971dc8ae4736/raw/1421656388a02cbc03e2374874c9bf56cb03a6ff/smart_script.sh && sudo bash smart_script.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment