Skip to content

Instantly share code, notes, and snippets.

@tpruzina
Last active October 8, 2018 12:36
Show Gist options
  • Save tpruzina/abc05a47db41c5cec0ab99284733a9e6 to your computer and use it in GitHub Desktop.
Save tpruzina/abc05a47db41c5cec0ab99284733a9e6 to your computer and use it in GitHub Desktop.
useful/convinient bash/zsh function snippets
# for stuff that lacks shebang, use simple file extension based file open with the right application
# somewhat similar to xdg open
function e()
{
if [ -f $1 ]; then
case $1 in
*.exe) wine $@ ;;
*.jpg) gpicview $@ ;;
# ...
# *.sh) chmod +x $1 && ./$@ ;; #this is just lazy
*) $@ ;;
esac
fi
}
# auto extract to current directory based on file extension
# example usage: extract archive.tar.xz
# put this somewhere in your {ba,z}sh.rc
function extract ()
{
GZIP="-p 8"
if [ -f $1 ]; then
case $1 in
*.rpa) unrpa $1 ;;
*.7z) 7z x $1 ;;
*.tar.zst) tar -I zstd -xpvf $@;;
*.tar.xz) tar -xvf $@ ;;
*.tar.bz2) tar -jxvf $1 ;;
*.tar.gz) tar -zxvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar -xvf $1 ;;
*.tbz2) tar -jxvf $1 ;;
*.tgz) tar -zxvf $1 ;;
*.zip) unzip $1 ;;
*.rar) unrar x $1 ;;
*) echo "'$1' cant recognize extension." ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# colorizes input
# grep --color like highlighter
# usage: cat file | hi red "rexeg1" | hi blue "regex2"
function hi()
{
declare -A fg_color_map
fg_color_map[black]=30
fg_color_map[red]=31
fg_color_map[green]=32
fg_color_map[yellow]=33
fg_color_map[blue]=34
fg_color_map[magenta]=35
fg_color_map[cyan]=36
if [ $# -eq 1 ]; then
fg_c=$(echo -e "\e[1;${fg_color_map[red]}m")
c_rs=$'\e[0m'
sed -u s"/$1/$fg_c\0$c_rs/g"
else
fg_c=$(echo -e "\e[1;${fg_color_map[$1]}m")
c_rs=$'\e[0m'
sed -u s"/$2/$fg_c\0$c_rs/g"
fi
}
# mcd: "(recursively) create directory and cd to it" shortcut
# usage: mcd test/ex1 # mkdir test && mkdir test/ex1 && cd test/ex1
function mcd()
{
mkdir -p "$@" && eval cd "\"\$$#\"";
}
# (radare2) automatically create a profile for opened binary
# so that analysis doesn't have to be redone again next time you open
# uses md5 of target for naming the profile and preventing name collisions
# usage: r2prof file.bin
function r2prof()
{
BACKUP="$@"
MD5=""
NAME=""
while [[ "$#" -gt 0 ]]; do
if [ -f "$1" ]; then
NAME=$(realpath $1)
MD5=$(echo $NAME | md5sum | awk '{print $1}')
echo "MD5 $MD5"
echo "NAME $NAME"
fi
shift
done
if [[ $MD5 == "" ]]; then
radare2 $BACKUP
else
echo "executing radare2 -p $MD5 $BACKUP"
radare2 -p $MD5 ${ARG}
fi
}
# create a temporary directory and cd to it while preserving ".."
# uses symlink that gets removed after cd e.g. realpath might not work
# use this to create temporary directory for builds (cmake ..), etc
# usage: tmcd
function tmcd
{
TMPDIR=$(mktemp -d)
BASE=$(basename ${TMPDIR})
ln -s ${TMPDIR} ${BASE} || return
cd ./"${BASE}" || return
rmdir ../"${BASE}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment