From 4d1bdc57fa3711f174560941ba19dbfd00f5e5e5 Mon Sep 17 00:00:00 2001 From: Eragos Date: Thu, 14 Apr 2016 21:31:11 +0200 Subject: [PATCH] add some bash functions --- functions | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 functions diff --git a/functions b/functions new file mode 100644 index 0000000..1aaf2bd --- /dev/null +++ b/functions @@ -0,0 +1,75 @@ +#Docker: bash into running Docker container +dockerbash() { + docker exec -it $1 bash +} + +#Docker: bash into running Docker container +dockerlog() { + docker logs -f $1 +} + +# Simple calculator +function calc() { + local result=""; + result="$(printf "scale=10;$*\n" | bc --mathlib | tr -d '\\\n')"; + # └─ default (when `--mathlib` is used) is 20 + # + if [[ "$result" == *.* ]]; then + # improve the output for decimal numbers + printf "$result" | + sed -e 's/^\./0./' `# add "0" for cases like ".5"` \ + -e 's/^-\./-0./' `# add "0" for cases like "-.5"`\ + -e 's/0*$//;s/\.$//'; # remove trailing zeros + else + printf "$result"; + fi; + printf "\n"; +} + +# Create a new directory and enter it +function mcd() { + mkdir -p "$@" && cd "$_"; +} + +# Run `dig` and display the most useful info +function digga() { + dig +nocmd "$1" any +multiline +noall +answer; +} + +# Search in bash history +function his() { + history | grep --color=auto -i "$1"; +} + +# Decompress just about any compressed file format +function extract { + if [ -z "$1" ]; then + # display usage if no parameters given + echo "Usage: extract ." + else + if [ -f $1 ] ; then + # NAME=${1%.*} + # mkdir $NAME && cd $NAME + case $1 in + *.tar.bz2) tar xvjf ../$1 ;; + *.tar.gz) tar xvzf ../$1 ;; + *.tar.xz) tar xvJf ../$1 ;; + *.lzma) unlzma ../$1 ;; + *.bz2) bunzip2 ../$1 ;; + *.rar) unrar x -ad ../$1 ;; + *.gz) gunzip ../$1 ;; + *.tar) tar xvf ../$1 ;; + *.tbz2) tar xvjf ../$1 ;; + *.tgz) tar xvzf ../$1 ;; + *.zip) unzip ../$1 ;; + *.Z) uncompress ../$1 ;; + *.7z) 7z x ../$1 ;; + *.xz) unxz ../$1 ;; + *.exe) cabextract ../$1 ;; + *) echo "extract: '$1' - unknown archive method" ;; + esac + else + echo "$1 - file does not exist" + fi +fi +} \ No newline at end of file