Skip to content

Instantly share code, notes, and snippets.

@kkirsche
Last active December 19, 2021 13:36
Embed
What would you like to do?
Useful functions to have in a shell
# Load via:
# if [ -f "${HOME}/.functions.sh" ]; then . "${HOME}/.functions.sh"; fi
# MIT License
if [ "${PLATFORM}" = "Linux" ]; then
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
fi
function extract() {
local c e i
((${#})) || return
for i; do
c=''
e=1
if [[ ! -r ${i} ]]; then
echo "${0}: file is unreadable: \`${i}'" >&2
continue
fi
case ${i} in
*.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
c=(bsdtar xvf);;
*.7z) c=(7z x);;
*.Z) c=(uncompress);;
*.bz2) c=(bunzip2);;
*.exe) c=(cabextract);;
*.gz) c=(gunzip);;
*.rar) c=(unrar x);;
*.xz) c=(unxz);;
*.zip) c=(unzip);;
*) echo "${0}: unrecognized file extension: \`${i}'" >&2
continue;;
esac
command "${c[@]}" "${i}"
((e = e || ${?}))
done
return "${e}"
}
function up() {
if [[ "${#}" -lt 1 ]]; then
cd ..
else
CDSTR=""
for ((i=0; i<${1}; i++)); do
CDSTR="../${CDSTR}"
done
cd "${CDSTR}" || return
fi
}
function over() {
if [[ "${#}" -lt 1 ]]; then
up
fi
if [[ "${#}" -lt 2 ]]; then
up
cd "${1}"
else
# command count_up path_over
up "${1}"
cd "${2}"
fi
}
function public_ip() {
curl -w "\n" https://canihazip.com/s
}
function take() {
mkdir -p "${@}" && cd "${@:$#}" || exit
}
function open_command() {
local open_cmd
# define the open command
case "${OSTYPE}" in
darwin*) open_cmd='open' ;;
cygwin*) open_cmd='cygstart' ;;
linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
open_cmd='cmd.exe /c start ""'
[[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
} ;;
msys*) open_cmd='start ""' ;;
*) echo "Platform $OSTYPE not supported"
return 1
;;
esac
${=open_cmd} "$@" &>/dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment