Hi, I'm providing you with a script I wrote, that can ease installing the ports packages:
The script is written in bash, and I would love to have some input feedbacks... so you can freely test it and let me know if you like it or not.
The script is written in bash, and I would love to have some input feedbacks... so you can freely test it and let me know if you like it or not.
Code:
#!/bin/bash
# This bash script is used to search for packages on a freebsd system
# - output: directories that match the input (if only one, automatically cd into it)
# - input: <package_name> [arguments]
# - input args:
# -l: insert left '*' to match package name
# -r: insert right '*' to match package name
# -b: insert left and right '*' to match package name
# Prerequisite commands: bash, find, cd, echo, wc
#
# Global variables
#
required_args=1
options=("-l" "-r" "-b")
options_desc=(
'match against *<package_name>' \
'match against <package_name>*' \
'match against *<package_name>*' \
)
flagl=1
flagr=1
#
# Display help for this script
#
function print_help {
echo "************************************************"
echo "Ports FreeBSD script for searching packets"
echo "Usage: ports <package_name> [options]"
echo "Options are:"
counter=0
for item in ${options[*]}
do
echo " " $item ": " ${options_desc[counter]}
counter=$counter+1
done
echo "************************************************"
}
#
# Check if the element is in array: return true if it is, otherwise false
# input:
# - first arg: element
# - second arg: array
# output:
# - 0: element is in array
# - 1: element is not in array
#
function element_in_array {
local element=$1
local item
shift
for item in "$@"
do
if [[ "$item" = "$element" ]]
then
return 0
fi
done
return 1
}
# ************************************************************** #
# Main function #
# ************************************************************** #
# check for input arguments
# - there should be at least one argument: <package_name>
# - other arguments are optional
if [ $# -lt $required_args ]
then
print_help
else
# first argument can only contain letters, numbers and underscore
if [[ "$1" != *[[:alnum:]_]* ]]
then
echo "Package name: $1 is not valid. It can only contain letters, numbers and underscore."
exit
fi
package_name="$1"
# other arguments must be available in the $options variable
shift
for arg in "$@"
do
element_in_array "$arg" "${options[@]}"
status=$?
if [[ $status -eq 1 ]]
then
echo "Specified argument: $arg is not valid. Please correct the command."
exit
fi
# mark used options
if [[ "$arg" = "-r" ]]
then
flagr=0
elif [[ "$arg" = "-l" ]]
then
flagl=0
elif [[ "$arg" = "-b" ]]
then
flagr=0
flagl=0
fi
done
fi
# Construct the right regular expression to match packages
if [[ "$flagl" -eq 0 ]]
then
package_name="*$package_name"
fi
if [[ "$flagr" -eq 0 ]]
then
pacakge_name="$package_name*"
fi
# list only packages that are available (nor subdirectories, ...)
packages=$(find /usr/ports -mindepth 2 -maxdepth 2 -type d -name "$package_name" -print)
num_pack=$(echo "$packages" | wc -l)
if [[ "$num_pack" -eq 1 ]]
then
cd "$packages"
echo "Are you sure you want to install package: $package_name [y/n]"
read answer
if [[ "$answer" = "y" ]] || [[ "$answer" = "yes" ]] || [[ "$answer" = "Y" ]] || [[ "$answer" = "YES" ]]
then
make install
fi
else
echo "$packages"
fi