Useful bash functions

Get bash file absolute path

realpath(){
  path="$1"
  while [ -h "$path" ] ; do path="$(readlink "$path")"; done
  echo "$(cd "$(dirname "$path")"; echo -n "$(pwd)/$(basename "$path")")";
}

Log

log() {
  if [[ -n "$VERBOSE" ]]; then echo -e "$@"; else test 1; fi
}

error() {
  echo "$@" >&2
  exit 1
}
warning() {
  echo "$@" >&2
}

function check_status {
  if [ $? -ne 0 ];then
    error ${@:-"Encountered an error, aborting!"}
  fi
}

Loop find

find . -type f -iname "*.txt" -print0 | while IFS= read -r -d $'\0' line; do
  echo "$line"
  ls -l "$line"
done

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.