152 lines
3.8 KiB
Plaintext
152 lines
3.8 KiB
Plaintext
# Shell Functions
|
|
|
|
# Shell note pad for Bash
|
|
noteb () {
|
|
local notes_file="$HOME/.notes"
|
|
local timestamp=$(date +"%F %T")
|
|
local input_line
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
# No arguments, display the notes file
|
|
tac "$notes_file"
|
|
return 0
|
|
elif [[ "$1" == "-a" ]]; then
|
|
# Add a note with prompting (using Bash's read -p)
|
|
read -r -p "$(date +"%F %T | ") " input_line
|
|
if [[ -n "$input_line" ]]; then
|
|
echo "$timestamp | $input_line" >> "$notes_file"
|
|
echo "Note added to $notes_file"
|
|
return 0
|
|
else
|
|
echo "No note entered."
|
|
return 1
|
|
fi
|
|
elif [[ "$1" == "-c" ]]; then
|
|
# Clear the notes file
|
|
read -r -p "Clear notes file? [Y/n] " clear_response
|
|
if [[ "$clear_response" =~ ^[Yy]$ ]]; then
|
|
> "$notes_file" # Truncate the file
|
|
echo "Notes file cleared."
|
|
else
|
|
echo "Notes file not cleared."
|
|
fi
|
|
return 0
|
|
else
|
|
# Treat arguments as the note (default action if not -a or -c)
|
|
input_line="$*"
|
|
echo "$timestamp | $input_line" >> "$notes_file"
|
|
echo "Note added to $notes_file"
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
# Shell Note Pad for ZSH
|
|
notez () {
|
|
local notes_file="$HOME/.notes"
|
|
local timestamp=$(date +"%F %T")
|
|
local input_line
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
# No arguments, display the notes file
|
|
cat "$notes_file"
|
|
return 0
|
|
elif [[ "$1" == "-a" ]]; then
|
|
# Add a note with prompting
|
|
read -r "?$(date +"%F %T | ") " input_line
|
|
if [[ -n "$input_line" ]]; then
|
|
echo "$timestamp | $input_line" >> "$notes_file"
|
|
echo "Note added to $notes_file"
|
|
return 0
|
|
else
|
|
echo "No note entered."
|
|
return 1
|
|
fi
|
|
elif [[ "$1" == "-c" ]]; then
|
|
# Clear the notes file
|
|
read -r "?Clear notes file? [Y/n] " clear_response
|
|
if [[ "$clear_response" =~ ^[Yy]$ ]]; then
|
|
> "$notes_file" # Truncate the file
|
|
echo "Notes file cleared."
|
|
else
|
|
echo "Notes file not cleared."
|
|
fi
|
|
return 0
|
|
else
|
|
# Treat arguments as the note (default action if not -a or -c)
|
|
input_line="$*"
|
|
echo "$timestamp | $input_line" >> "$notes_file"
|
|
echo "Note added to $notes_file"
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
# tar/gzip a directory
|
|
# I use this before 'rm -rf'ing a directory I think is useless
|
|
# if nothing breaks after a predetermined amount of time, then the tar file is safe to delete
|
|
tardir() {
|
|
#ensure $1 is a directory
|
|
if [ ! -d "${1}" ]; then
|
|
echo "$1 is not a directory. Aborting."
|
|
return 1
|
|
fi
|
|
#strip trailing slash
|
|
target="${1%/}"
|
|
echo "Dir size: $(du -hs ${target})"
|
|
tar czf "${target}".tar.gz "${target}"
|
|
echo "Archive size: $(du -hs ${target}.tar.gz)"
|
|
}
|
|
|
|
# Replacement for 'for i in $(ls); do du -hs $i; done' that handles spaces
|
|
function dudir() {
|
|
if ! (($#)); then
|
|
dir=$(pwd)
|
|
else
|
|
dir="${1}"
|
|
fi
|
|
# redefine $IFS
|
|
o=$IFS
|
|
IFS=$(echo -en "\n\b")
|
|
for i in $(ls "${dir}"); do
|
|
du -hs "${i}"
|
|
done
|
|
# reset $IFS
|
|
IFS=$o
|
|
}
|
|
|
|
# HAHA! Weather in the terminal
|
|
weather() {
|
|
if [ $(command -v curl) ]; then
|
|
if ! (($#)); then
|
|
curl wttr.in/44113
|
|
else
|
|
curl wttr.in/$1
|
|
fi
|
|
else
|
|
echo "curl not installed. Aborting."
|
|
fi
|
|
}
|
|
|
|
# Replaces "ps aux | grep <something>" with "psaux <something>". Shorter to type, more informational and doesn't include the command itself in the output.
|
|
psaux () { pgrep -f "$@" | xargs ps -fp 2>/dev/null; }
|
|
|
|
# mail a file
|
|
email() {
|
|
if ! (($#)); then
|
|
echo "USAGE: email {filename} {email-address}"
|
|
return 1
|
|
else
|
|
efile=${1}
|
|
eaddr=${2}
|
|
echo "See Attached" | mail -s "File: ${efile}" -a ${efile} ${2}
|
|
fi
|
|
}
|
|
|
|
# git testmerge
|
|
testmerge() {
|
|
echo "git merge --no-commit --no-ff $1"
|
|
git merge --no-commit --no-ff $1
|
|
echo "git diff --cached"
|
|
git diff --cached
|
|
echo "'git merge --abort' when done"
|
|
}
|