# ports script for installing packages



## eleanor (Jul 16, 2010)

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.


```
#!/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
```


----------



## Alt (Jul 16, 2010)

No need bash for this functionality..
Or maybe... "No need bash ever"


----------



## DutchDaemon (Jul 16, 2010)

Other than 'no real need for bash':

1) you should script in sh - FreeBSD's 'native' scripting environment; the syntax is almost identical, so porting it to 'sh' should be easy
2) bash doesn't live in /bin on FreeBSD; it's a third-party application from ports which lives in /usr/local/bin
3) I see no added value compared to the ports management tools there already are


----------



## eleanor (Jul 16, 2010)

Can you tell me which are the freebsd ports management tools that already exists, maybe I just don't know them?


----------



## DutchDaemon (Jul 16, 2010)

They're all in /usr/ports/ports-mgmt/ (or http://www.freshports.org/ports-mgmt). There are some ports-related scripts in the Userland Programming & Scripting forum and the Useful scripts thread.


----------



## phoenix (Jul 16, 2010)

ports-mgmt/psearch
ports-mgmt/portmaster
The former searches the ports tree, the latter installs/upgrades/manages ports.  Can't really get any better than that combination.


----------

