interactive shellcheck in bash
#!/bin/bash
# by: Cody Kochmann
# returns the current session's history
history-session(){
s1=$(history | cut -c 8-)
s2=$(cat ~/.bash_history)
diff <(echo "$s1" ) <(echo "$s2") | grep "<" | cut -c 3-
}
# generates a random string
gen-password(){
openssl rand -base64 4096 | openssl dgst -sha256 | sed "s/(stdin)= //g"
}
# runs shellcheck against the current session
shellcheck-session(){
# generate a file for it to look at
filepath="/tmp/shellcheck"
if [ ! -d "$filepath" ]; then mkdir "$filepath" ; fi
filepath="$filepath/$(gen-password).sh"
echo "#!/bin/bash" > "$filepath"
history-session | grep -v 'shellcheck-' >> "$filepath"
output=$(shellcheck "$filepath")
if [[ $(cat $filepath | grep . -c) > 1 ]]
then
output=$(echo "$output" | grep . | sed -E s'/In \/tmp\/shellcheck\/[a-z0-9]{1,}.sh /on /g')
if [[ $(echo "$output"|grep . -c) == 0 ]]
then
echo "No issues detected!"
else
echo "$output"
fi
fi
}
# runs chellcheck after every command
shellcheck-start(){
trap 'shellcheck-session' DEBUG
echo "shellcheck is now in interactive mode."
}