Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

how to show all parms matching a given substring?



I would like to solicit suggestions from others about a better, simpler way to 
get all parameters in the current context matching a given substring.  If 
this can be done using parameter expansion patterns, so much the better.

For right now, I use the following function.

Thanks,

scowles at earthlink dot net


showparms(){
  # find all parms with name or value matching a given fixed substring.

  availopts=hv
  usage="usage:  $0 -{${availopts}}"
  usage=$(printf "%s\n\t%s\n" $usage "h help")
  usage=$(printf "%s\n\t%s\n" $usage "v include parm values")

  v=""
  while getopts ":${availopts}" opt
  do
    case ${opt} in
      v )  v="v"
        ;;
      h )
        echo "${usage}" >&2
        return
        ;;
      \? )
        echo "invalid option usage." >&2
        echo ${OPTIND} >&2
        echo "${usage}" >&2
        return
        ;;
      \: )
        echo "missing option." >&2
        echo ${OPTIND} >&2
        echo "${usage}" >&2
        return
        ;;
    esac
  done
  shift $(( ${OPTIND} - 1 ))
  [[ -z "$1" ]] && {
    echo "must provide a pattern." >&2
    return
  }
  spatt="$1"

  [[ -z "${v}" ]] && {
    # work on parameter names, only.
    eval "
      showparm () {
        set + |
        while read line
        do
          [[ -z \"\${line}\" ]] && { continue ; }
          [[ \${line[(i)${spatt}]} -le \${#line} ]] && { echo \${line} ; }
        done
      }
    "
  }
  [[ -n "${v}" ]] && {
    # work on parameter names and values.
    eval "
      showparm () {
        set   |
        while read line
        do
          [[ -z \"\${line}\" ]] && { continue ; }

          [[ \${line[(i)${spatt}]} -le \${#line} &&
             \${line[(i)=]}        -le \${#line} ]] && { echo \${line} ; }
        done
      }
    "
  }
  showparm
}




Messages sorted by: Reverse Date, Date, Thread, Author