Skip to content

Instantly share code, notes, and snippets.

@daringer
Created June 7, 2014 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daringer/a15816b3c1ff7d4b3f55 to your computer and use it in GitHub Desktop.
Save daringer/a15816b3c1ff7d4b3f55 to your computer and use it in GitHub Desktop.
Some useful bash helper functions
#!/bin/bash
##
## indir - execute given command ($2) in
## target directory ($1) with arguments ($3..N)
##
function indir {
if [[ "$1" = "" ]] || [[ "$2" = "" ]]; then
echo "[i] Usage: indir <target_dir> <command> <arg1> .. <argN>"
return 1
fi
if [ ! -d "$1" ] || [ ! -x "$1" ]; then
echo "[-] cannot change dir into $1"
return 1
fi
pushd "$1" > /dev/null
if [[ "$?" != "0" ]]; then
echo "[-] pushd failed!"
return 1
fi
shift
$@
popd > /dev/null
if [[ "$?" != "0" ]]; then
echo "[-] popd failed!"
return 1
fi
return 0
}
##
## ppwd - a pwd with file support
## (return full-absolute-path of given file)
##
function ppwd {
if [[ "$1" = "" ]]; then
pwd
return $?
fi
mydir=$(indir `dirname $1` pwd)
myfn=$(basename $1)
echo ${mydir}/${myfn}
return 0
}
##
## isscreen - check if shell is inside a screen session
##
alias isscreen="([[ \"$STY\" = \"\" ]] && echo \"NO SCREEN SESSION\" ) || echo \"YES SCREEN SESSION\""
##
## is_int - check for integer inside string
##
function is_int() {
( [[ "$1" = "$(echo $1 | awk '{print strtonum($0)}')" ]] && \
return 0 ) || return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment