Bash-confirm-binaries: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
(Created page with "=== Simple function to confirm binaries exist before running scripts === <pre> #=== FUNCTION ================================================================ # NAME: verify_deps # DESCRIPTION: Check that all binary files are available to be used # PARAMETERS: None. This is standalone. Changes occur on case by case # RETURNS: none #=============================================================================== verify_deps() { # needed="xmllint...")
 
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
=== Simple function to confirm binaries exist before running scripts ===
=== Simple function to confirm binaries exist before running scripts ===
If you are testing for files not in PATH, then you must give an absolute path to them.  For example if a file lives in your home directory, you must use /home/user/path/to/file.  This will allow the type check to work as expected.  Otherwise use the stat command and an absolute path with that.
<pre>
<pre>
#===  FUNCTION  ================================================================
#===  FUNCTION  ================================================================
Line 15: Line 17:
   if [ $? -eq 1 ]; then
   if [ $? -eq 1 ]; then
     echo "Status WARNING - missing manditory component: $i"; exit 1
     echo "Status WARNING - missing manditory component: $i"; exit 1
  fi
done
echo "Confirmed: all binary files necessary have been found"
}
</pre>
=== If used with abnormal binaries not in your path ===
<pre>
#===  FUNCTION  ================================================================
#          NAME:  verify_deps
#  DESCRIPTION:  Check that all binary files are available to be used
#    PARAMETERS:  None.  This is standalone.  Changes occur on case by case
#      RETURNS:  none
#===============================================================================
verify_deps() {
# needed="xmllint curl w3m snmptrap cut egrep expr"
needed="/foo/bar/baz /barf/damn/gasp"
for i in `echo $needed`
do
  stat $i | grep -q "File:"
  if [ $? -eq 1 ]; then
    echo "Status WARNING - missing mandatory component: $i"; exit 1
   fi
   fi
done
done

Latest revision as of 16:36, 23 March 2023

Simple function to confirm binaries exist before running scripts

If you are testing for files not in PATH, then you must give an absolute path to them. For example if a file lives in your home directory, you must use /home/user/path/to/file. This will allow the type check to work as expected. Otherwise use the stat command and an absolute path with that.

#===  FUNCTION  ================================================================
#          NAME:  verify_deps
#   DESCRIPTION:  Check that all binary files are available to be used
#    PARAMETERS:  None.  This is standalone.  Changes occur on case by case
#       RETURNS:  none
#===============================================================================
verify_deps() {
# needed="xmllint curl w3m snmptrap cut egrep expr"
needed="ansible ansible-playbook grep sed echo tee"
for i in `echo $needed`
do
  type $i >/dev/null 2>&1
  if [ $? -eq 1 ]; then
    echo "Status WARNING - missing manditory component: $i"; exit 1
  fi
done
echo "Confirmed: all binary files necessary have been found"
}

If used with abnormal binaries not in your path

#===  FUNCTION  ================================================================
#          NAME:  verify_deps
#   DESCRIPTION:  Check that all binary files are available to be used
#    PARAMETERS:  None.  This is standalone.  Changes occur on case by case
#       RETURNS:  none
#===============================================================================
verify_deps() {
# needed="xmllint curl w3m snmptrap cut egrep expr"
needed="/foo/bar/baz /barf/damn/gasp"
for i in `echo $needed`
do
  stat $i | grep -q "File:"
  if [ $? -eq 1 ]; then
    echo "Status WARNING - missing mandatory component: $i"; exit 1
  fi
done
echo "Confirmed: all binary files necessary have been found"
}