Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

1 total

List the names & version numbers of applications

# lsregister
/usr/bin/sudo /bin/ln -is $(/usr/bin/locate lsregister | /usr/bin/head -n 1) /bin/lsregister 


# first rebuild the Launch Services database
lsregister -kill -r -f -all system,local,user     # Mac OS X 10.5
lsregister -kill -r -f -domain local -domain system -domain user


# print the lines between :path ... version: ...
time -p lsregister -dump | sed -E -n -e '/^.+path: .+\.app$/{N;N;N;p;}' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/{N;N;N;p;}' | nl

time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/p' | nl

time -p lsregister -dump | egrep -A 3 -i "path: +\/.+\\.app$" | nl


# list application paths with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/s/^.+path: +(.+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/s/^.+path: +(.+\.app)$/\1/p' | nl

# list application names with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/s/^.+path: +\/?.*\/([^\/]+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/s/^.+path: +\/?.*\/([^\/]+\.app)$/\1/p' | nl

# list version numbers of applications with lsregister
time -p lsregister -dump | sed -E -n -e '/path: +.+\.app$/,/version/s/^.+version: +(.*)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version/s/^.+version: +(.*)$/\1/p' | nl


# list the names & version numbers of applications based on lsregister

function lsvers() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\n' file; do

         path="${file% -*}"
         lsregister_version="${file##*- }"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"
         #mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
         #   /usr/bin/awk -F '"' 'END {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${path}   --   ${mdls_version}"
         else
            printf "%s\n" "${path}   --   ${lsregister_version}"
         fi

done < <(
/bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n"
)

   else

      while read -d $'\n' file; do

         path="${file% -*}"
         bname="$(/usr/bin/basename "${path}")"
         lsregister_version="${file##*- }"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\n" "${bname}   --   ${lsregister_version}"
         fi

done < <(
/bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n"
)

   fi


: <<-'COMMENT'

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

# alternatives with sed pattern matching across several lines (here matching the lines containing: path: ... :version ...)

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/path:.+\.app$/,/version:/s/^.+path: +(\/.+\.app)$|^.+version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/path:.+\.app$/,/version:/s/path: +(\/.+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/\.app$/,/version:/s/path: +(\/.+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

COMMENT


return 0

}


lsvers | nl
lsvers | egrep -i system | nl
lsvers -fp | egrep -i system | nl     # print full paths to applications
lsvers -fp | egrep -i '\/[^\/]*system[^\/]*$' | nl     # print full paths to applications


# check
app="VerifiedDownloadAgent.app"
app="Crash Reporter.app"
app="SyncServer.app"
app="SecurityAgent.app"
app="SystemUIServer.app"

lsregister -dump | egrep -A 3 "${app}$"

path="$(lsregister -dump | grep -A 3 "${app}$" | sed -E -n -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p')"
echo "$path"
mdls -name kMDItemVersion "$path"


#-------------------------------------------


# list the names and version numbers of specified applications using lsregister
function appversion() {

/bin/lsregister -dump | /usr/bin/egrep -i -A 3 "^[[:space:]]+path:[[:space:]]+\/?.*\/([^\/]*${@}[^\/]*\\.app)$" | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+\/.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s   --   %s\n"


: <<-'COMMENT'

set -- System
echo "${@}"

time -p /bin/lsregister -dump | /usr/bin/egrep -i -A 3 "^[[:space:]]+path:[[:space:]]+\/?.*\/[^\/]*${@}[^\/]*\\.app$" | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+\/.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

# alternatives
time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i "path: +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e 's/version: +(.*)$/\1/p' -e 's/path: +\/?.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i " +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e 's/version: +(.*)$/\1/p' -e 's/path: +\/?.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl


# alternative with sed pattern matching across several lines
# here: matching the lines between: path: ... :version ...

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i "path: +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e '/\.app$/,/version:/s/path: +\/?.*\/([^\/]+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

COMMENT

return 0
}


appversion mail
appversion finder
appversion safari

appversion system
appversion uiserver
appversion server
appversion window


#----------------------------------------------------------------------


# list names & mdls version numbers of applications (based on mdfind & com.apple.application-bundle)
function lsmdvers() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\000' file; do

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${file}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${file}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 'kMDItemContentTypeTree == "com.apple.application-bundle"wc')

   else

      while read -d $'\000' file; do

         bname="$(/usr/bin/basename "${file}")"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${bname}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 'kMDItemContentTypeTree == "com.apple.application-bundle"wc')

   fi

   return 0

}


lsmdvers | nl
lsmdvers -fp | nl    # print full paths to applications

lsmdvers | grep -i system | nl && echo && lsvers | grep -i system | nl



# list the names and version numbers of specified applications using mdfind & com.apple.application-bundle
function appmdversion() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\000' file; do

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${file}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${file}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 "kMDItemContentTypeTree == 'com.apple.application-bundle'wc && kMDItemDisplayName == '*${2:-*}*'wc")

   else

      while read -d $'\000' file; do

         bname="$(/usr/bin/basename "${file}")"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${bname}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 "kMDItemContentTypeTree == 'com.apple.application-bundle'wc && kMDItemDisplayName == '*${@:-*}*'wc")

   fi

   return 0

}


appmdversion play | nl
appmdversion -fp play | nl   # print full paths to applications

appversion play && echo && appmdversion play


#----------------------------------------------------


# experimental

function cmdversion() {

declare cmd last_modified vers

while [ $# -gt 0 ]; do

   cmd="$(/usr/bin/which ${1})"   # get the full cmd path

   # cmd was not found in $PATH
   if [[ -n "$(echo "$cmd" | /usr/bin/egrep "^no +${1} +in ")" ]]; then echo; echo "${cmd}"; echo; shift; continue; fi
   #if [[ -n "$(echo "$cmd" | /usr/bin/egrep '^no +[^[:space:]]+ +in ')" ]]; then echo; echo "${cmd}"; echo; shift; continue; fi


   # handle special cases such as ls, echo, ...
   if [[ "${1##*/}" == "ls" ]] || [[ "${1##*/}" == "echo" ]] || [[ "${1##*/}" == "getopt" ]]; then
      vers="$(/usr/bin/egrep -ao '(.{0,10}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,15}|.{0,10}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,15})' "${cmd}")"
      last_modified="$(/usr/bin/stat -f $'last modified:   %Sm\n' "${cmd}")"
      #printf "\e[1m%s\e[m (guess):\n%s\n" "${cmd}" "${vers}"
      printf "\e[1m%s\e[m (guess):\n%s\n%s\n" "${cmd}" "${vers}" "${last_modified}"
      shift
      continue
   fi

   if [[ "${1##*/}" == "python" ]]; then
      vers="$(${cmd} -V 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"
      shift
      continue
   fi

   # cases that require sudo
   if [[ "${1##*/}" == "fibreconfig" ]]; then
      vers="$(/usr/bin/sudo ${cmd} --version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"
      shift
      continue
   fi


   if [[ -n "$(${cmd} --version 2>/dev/null)" ]]; then
      vers="$(${cmd} --version)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} -version 2>/dev/null)" ]]; then
      vers="$(${cmd} -version)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} --version 2>&1 | /usr/bin/egrep -ao '([Vv]ersion:? *"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)')" ]]; then
      vers="$(${cmd} --version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} -version 2>&1 | /usr/bin/egrep -ao '([Vv]ersion:? *"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)')" ]]; then
      vers="$(${cmd} -version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(/usr/bin/egrep -ao '([Vv]ersion:? +"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)' "${cmd}" 2>/dev/null) 2>/dev/null)" ]]; then
      vers="$(/usr/bin/egrep -ao '(.{0,10}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,15}|.{0,10}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,15})' "${cmd}")"
      #vers="$(/usr/bin/egrep -ao '(.{0,30}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,30}|.{0,30}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,30})' "${cmd}")"
      #vers="$(/usr/bin/egrep -ao '([Vv]ersion:? +"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)' "${cmd}")"

      last_modified="$(/usr/bin/stat -f $'last modified:   %Sm\n' "${cmd}")"

      if [[ -z "${vers}" ]]; then 
         printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${last_modified}"
         shift
         continue
      fi

      #printf "\e[1m%s\e[m (guess):\n%s\n" "${cmd}" "${vers}"
      printf "\e[1m%s\e[m (guess):\n%s\n%s\n" "${cmd}" "${vers}" "${last_modified}"

   fi

   shift

done

return 0
}


cmdversion bash
cmdversion /bin/bash

cmdversion sh java sed ls echo printf tr

cmdversion rm srm rmdir unlink kill killall

cmdversion read stat chown w tcl tk getopt getopts symlink ln locate

cmdversion chmod cp dd ed ssh

cmdversion bzcat openssl cat open alias uuidgen bc apropos man perl python ruby

cmdversion /sbin/fibreconfig


#-----------------------------------------------


# list the version numbers of dynamically loaded kernel extensions
man kextstat   
/usr/sbin/kextstat | /usr/bin/sed -E -n -e 's/^([[:space:]]+[^[:space:]]+){5}[[:space:]]+([^[:space:]]+)[[:space:]]+\(([^[:space:]]+)\).*$/\2  --  \3/p'
1 total