Script "print-sharedlib-deps.sh"
#!/bin/sh

# ------------------------------
# print-sharedlib-deps.sh
# version 1.2.5.7
# ------------------------------

# check for required binaries

REQD_BINS=""
which grep >/dev/null 2>&1 || type grep >/dev/null 2>&1 || REQD_BINS="grep,${REQD_BINS}"
which tr >/dev/null 2>&1 || type tr >/dev/null 2>&1 || REQD_BINS="tr,${REQD_BINS}"
which cut >/dev/null 2>&1 || type cut >/dev/null 2>&1 || REQD_BINS="cut,${REQD_BINS}"
which sed >/dev/null 2>&1 || type sed >/dev/null 2>&1 || REQD_BINS="sed,${REQD_BINS}"
which readelf >/dev/null 2>&1 || type readelf >/dev/null 2>&1 || REQD_BINS="readelf,${REQD_BINS}"
if [ ! -z "${REQD_BINS}" ]; then
  echo -e `basename ${0}`: "Required binaries (${REQD_BINS}\010) are not present."
  exit 1
fi

# check for parameters

_usage_str="Usage: ${0} [ --short ] /path/to/elf-binary /path/to/another-elf-binary ..."

if [ $# -eq 0 ] || [ ${1} = "--help" ] || [ ${1} = "--usage" ]; then
  printf "%s\n" "${_usage_str}"
  exit 0
fi

SHRT_MODE=""
PATHS_TO_ELFS=$@

for arg in $@
do
  is_option=`echo ${arg} | grep '\-\-'`
  if [ ! -z "${is_option}" ]; then
    if [ $arg = "--short" ]; then
      SHRT_MODE="yes"
      PATHS_TO_ELFS=`echo "${PATHS_TO_ELFS}" | sed "s/${arg}//g"`
    else
      echo "${0}: unknown option '${arg}'"
    fi
  fi
done

if [ -z "${PATHS_TO_ELFS}" ]; then
  printf "%s\n" "${_usage_str}"
  exit 0
fi

print_libdeps_for_one_binary()
# requires 1 parameter: full name of ELF binary
{
  if [ ! $# -eq 1 ]
  then
    echo "*INTERNAL ERROR*"
  else
    if [ -f "${1}" ] || [ -L "${1}" ]
    then
      LIST_OF_DEPS=`readelf -d "${1}" 2>/dev/null | grep NEEDED | tr -d " \t]" | tr "[" "\t" | cut -f2`
      ERR_OF_LSDEPS=`readelf -d "${1}" 2>&1 | tail -1`

      printf "%s:" "${1}"
      [ -z "${SHRT_MODE}" ] && echo ""
      if [ ! -z "${LIST_OF_DEPS}" ]; then
        echo "${LIST_OF_DEPS}" | while read LINE
        do
          [ -z "${SHRT_MODE}" ] && printf "\t"
          [ -z "${SHRT_MODE}" ] || printf " "
          printf "%s" "${LINE}"
          [ -z "${SHRT_MODE}" ] && echo ""
        done
        [ -z "${SHRT_MODE}" ] || echo ""
      else
        [ -z "${SHRT_MODE}" ] && printf "\t"
        [ -z "${SHRT_MODE}" ] || printf " "
        echo "${ERR_OF_LSDEPS}"
      fi
    else
      echo File \'${1}\' doesn\'t exist.
    fi
  fi
}

PATHS_TO_ELFS=`echo "${PATHS_TO_ELFS}" | sed 's/ /\n/g'`
echo "${PATHS_TO_ELFS}" | while read elf_name
do
  [ -z "${elf_name}" ] || print_libdeps_for_one_binary "${elf_name}"
done
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.