Code: Select all
#! /bin/bash
# Licence: GNU GPL v3 https://www.gnu.org/licenses/gpl-3.0.html
# Version: 2
# Script use: Name_of_script --check /path/to/next/script
# Destiny: Script to find dependencies from *.bash and *.sh files
####################################{
## == Check Dependecies ==
# Check Dependecies
[[ -z $(which awk) ]] && DEP="$DEP"$'\n'"awk"
[[ -z $(which comm) ]] && DEP="$DEP"$'\n'"comm"
[[ -z $(which echo) ]] && DEP="$DEP"$'\n'"echo"
[[ -z $(which grep) ]] && DEP="$DEP"$'\n'"grep"
[[ -z $(which lsb_release) ]] && DEP="$DEP"$'\n'"lsb_release"
[[ -z $(which print) ]] && DEP="$DEP"$'\n'"print"
[[ -z $(which sed) ]] && DEP="$DEP"$'\n'"sed"
[[ -z $(which wc) ]] && DEP="$DEP"$'\n'"wc"
[[ -z $(which which) ]] && DEP="$DEP"$'\n'"which"
# End script if exist any error
[ -z "$DEP" ] || { echo " Error: Missing dependencies, before run script please install: $DEP" ; exit 1 ;}
## == Check Dependecies ==
####################################}
####################################{
## == Message library ==
NC='\e[0m' # Reset Color
BL='\e[0;36m' # Cyan ECHO
GN='\e[0;32m' # Green ECHO
YW='\e[0;33m' # Yellow ECHO
RD='\e[0;31m' # Red ECHO
#----------------{
MESSAGE_DEBUG() {
#DEBUG="OFF / ON"
DEBUG="OFF"
if [[ "$DEBUG" == "ON" ]] ; then
echo -e "${BL} DEBUG: $1 ${NC}"
fi
}
#----------------}
MESSAGE_INFO() {
#echo "----"
echo -e "${GN} INFO: $1 ${NC}"
#echo "----"
}
MESSAGE_WARNING() {
# Print text in red and redirect to error
echo -e "${YW} WARNING: $* ${NC}" 1>&2
#; exit 1
}
MESSAGE_ERROR() {
# Print text in red and redirect to error
echo -e "${RD} ERROR: $* ${NC}" 1>&2
#; exit 1
}
##
####################################}
CHECK_DEP() {
########################{
## == System settings ==
NAME_LINUX=$(lsb_release -is)
MESSAGE_DEBUG "--> Linux distribution name: $NAME_LINUX"
case "$NAME_LINUX" in
"UPLOS")
FIND_PACKAGE="rpm -qf" # find package name from path
FIND_SOURCE="rpm -qi" # show info about package
;;
"Linuxmint")
FIND_PACKAGE="dpkg -S" # find package name from path
FIND_SOURCE="dpkg-query -s" # show info about package
# Example
# command: dpkg-query -s ncurses-bin | grep -i source
# Source: ncurses
# command: dpkg -s ncurses-bin | grep -i source
# Source: ncurses
;;
"Ubuntu")
FIND_PACKAGE="dpkg -S" # find package name from path
FIND_SOURCE="dpkg-query -s" # show info about package
;;
*)
# Arch Linux
#FIND_PACKAGE="pacman -Qo"
MESSAGE_ERROR "Sorry, I don't have enough information about your system to develop this script."
MESSAGE_INFO "You can send me output \" lsb_release -is \" , how find package name from path ,how show info about package "
exit 1
;;
esac
## == System settings ==
########################}
MESSAGE_DEBUG "Used arguments: $*"
NAME_SCRIPT=$(awk '{ print $2 }' <<< "$*")
# Check if this is file
if [[ ! $(echo "$*" | wc -w) == "2" ]] ; then
MESSAGE_ERROR "Invalid number of arguments = $*" ; exit 1
elif [ ! -f "$NAME_SCRIPT" ] ; then
MESSAGE_ERROR "We can't find file: ${NAME_SCRIPT}" ; exit 1
elif ! grep -q "text" <(file "$NAME_SCRIPT") ; then
MESSAGE_ERROR "This is not text file: $NAME_SCRIPT" ; exit 1
elif ! grep -q "/bin/bash\|/bin/sh\|/usr/bin/env sh\|/usr/bin/env bash" "$NAME_SCRIPT" ; then
MESSAGE_ERROR "This is not shell script: $NAME_SCRIPT" ; exit 1
fi
# All available commands in the system except reserved
# dictionary: https://www.gnu.org/software/bash/manual/html_node/Reserved-Word-Index.html
# compgen https://bash.cyberciti.biz/bash-reference-manual/Programmable-Completion-Builtins.html#index-compgen-367
# From all available commands remove aliasses
LIST_1=$(comm -23 <(compgen -c | sort) <(compgen -a | sort))
# From LIST_1 remove functions
LIST_2=$(comm -23 <(echo "$LIST_1" | sort) <(compgen -A function | sort))
# From LIST_2 remove shell reserved
LIST_3=$(comm -23 <(echo "$LIST_2" | sort) <(compgen -k | sort))
# From LIST_3 remove shell builtin commands and "[" and repeat
LIST_4=$(comm -23 <(echo "$LIST_3" | sort) <(compgen -b | sort) | grep -v '\[' | uniq)
# MESSAGE_DEBUG "List commands = $LIST_4"
# Clear file = remove comments, echo coments, options, gtkdialog comments, gtkdialog if(if false disable:CHECKBOX2), html, empty lines.
CLEARED_FILE=$(sed -e 's/#[^#]*//g' -e 's/"[^"]*"//g' -e 's/-[^-]*//g' -e '/<label>/d' -e '/able:/d' -e 's/<[^>]*>//g' -e '/^\s*$/d' ${NAME_SCRIPT})
# MESSAGE_DEBUG "CLEARED_FILE = $CLEARED_FILE"
while IFS= read -r COMMAND_LINE ; do
if $(echo "$CLEARED_FILE" | grep -wq "$COMMAND_LINE") ; then
# MESSAGE_DEBUG "$COMMAND_LINE"
LIST_OF_DEPEND=$(echo -e "$LIST_OF_DEPEND\n${COMMAND_LINE}")
fi
done <<< "${LIST_4}"
# Remove first empty line
LIST_OF_DEPEND=$(grep -v -e '^$' <<< "$LIST_OF_DEPEND")
# MESSAGE_DEBUG "LIST_OF_DEPEND = $LIST_OF_DEPEND"
# Find linux distribution dependencies
while IFS= read -r LINE_DEPEND ; do
MESSAGE_DEBUG "LINE_DEPEND = $LINE_DEPEND"
PATH_DEPEND=$(which "$LINE_DEPEND")
if [[ $(echo $?) == "1" ]] ;then
MESSAGE_ERROR "We can not find path of command: $LINE_DEPEND"
continue
fi
NAME_PACK=$(${FIND_PACKAGE} "$PATH_DEPEND")
if [[ $(echo $?) == "1" ]] ;then
# Some symbolic links are badly made in packages and it will be harder to find the right package from package manager.
# We will try use deep search
if grep -q "symbolic link" <(file "$PATH_DEPEND") ;then
MESSAGE_DEBUG "This is symbolic link = $PATH_DEPEND"
MESSAGE_WARNING "Search package from path failed. We will try find file from symbolic link = $PATH_DEPEND"
# We will try get file from symbolic link
NAME_PACK=$(${FIND_PACKAGE} "$(readlink -f "$PATH_DEPEND")")
if [[ $(echo $?) == "1" ]] ;then
MESSAGE_ERROR "We can not find package = $PATH_DEPEND"
continue
fi
else
MESSAGE_ERROR "We can not find package = $PATH_DEPEND"
continue
fi
fi
MESSAGE_DEBUG "NAME_PACK = $NAME_PACK"
LIST_PACK=$(echo -e "${LIST_PACK}\n${NAME_PACK}")
done <<< "${LIST_OF_DEPEND}"
MESSAGE_DEBUG "==============="
# Remove first empty line, get first column
LIST_PACK=$(grep -v -e '^$' <<< "$LIST_PACK" | cut -d':' -f1 | sort | uniq)
MESSAGE_INFO "List of packages dependencies: \n$LIST_PACK"
echo " "
echo "==============="
echo " "
## Find source code
## Poorly described packages in Ubuntu/LinuxMint and I don't know any other method
## so is off
#while IFS= read -r NAME_PACK ; do
# SOURCE_CODE=$(${FIND_SOURCE} "$NAME_PACK" | grep -i source)
# MESSAGE_INFO "$NAME_PACK = $SOURCE_CODE"
#done <<< "${LIST_PACK}"
MESSAGE_INFO "Example of code for shell script:"
echo " "
echo '# Check Dependecies'
while IFS= read -r DEPEND ; do
echo "[[ -z \$(which "${DEPEND}") ]] && DEP=\"\$DEP\"$'\n'\"${DEPEND}\""
done <<< "${LIST_OF_DEPEND}"
echo " "
echo '# End script if exist any error'
echo '[ -z "$DEP" ] || { echo " Error: Missing dependencies, before run script please install: $DEP" ; exit 1 ;}'
echo " "
}
case $1 in
"--help"|"-h")
echo "---------------------------------------------------------"
echo "usage: $0 --option script.sh"
echo " "
echo " Options:"
echo " -h --help Show this help."
echo " --check Check dependencies of shell script."
echo " "
echo "---------------------------------------------------------"
exit 0
;;
"--check")
CHECK_DEP "$*"
;;
"--BUILD_CONFIG")
CHECK_DEP "$*"
BUILD_CONFIG
;;
*)
MESSAGE_ERROR "Unknow option. For help write: $0 --help"
;;
esac
Edit$ bash find.dep.sh --check script.sh
dpkg-query: no matching path was found /usr/bin/awk
WARNING: Search package from path failed. We will try find file from symbolic link = /usr/bin/awk
dpkg-query: no matching path was found /usr/bin/which
WARNING: Search package from path failed. We will try find file from symbolic link = /usr/bin/which
INFO: List of packages dependencies:
coreutils
debianutils
gawk
grep
lsb-release
mime-support
sed
===============
INFO: Example of code for shell script:
# Check Dependecies
[[ -z $(which awk) ]] && DEP="$DEP"$'\n'"awk"
[[ -z $(which comm) ]] && DEP="$DEP"$'\n'"comm"
[[ -z $(which echo) ]] && DEP="$DEP"$'\n'"echo"
[[ -z $(which grep) ]] && DEP="$DEP"$'\n'"grep"
[[ -z $(which lsb_release) ]] && DEP="$DEP"$'\n'"lsb_release"
[[ -z $(which print) ]] && DEP="$DEP"$'\n'"print"
[[ -z $(which sed) ]] && DEP="$DEP"$'\n'"sed"
[[ -z $(which wc) ]] && DEP="$DEP"$'\n'"wc"
[[ -z $(which which) ]] && DEP="$DEP"$'\n'"which"
# End script if exist any error
[ -z "$DEP" ] || { echo " Error: Missing dependencies, before run script please install: $DEP" ; exit 1 ;}
- The example code in the example output is automatically generated from dependencies and you can use this code.
- I didn't use the package name for generating the sample code because any Linux distribution can split a package into any number of packages and any name.