ListFCL.sh
Go to the documentation of this file.
1 #!/bin/bash
2 #
3 # Lists all the FCL files whose name matches the specified pattern.
4 #
5 # Usage: ListFCL.sh [Pattern]
6 #
7 # The pattern is a regex as interpreted by grep ("-e" option).
8 #
9 
10 SCRIPTNAME="$(basename "$0")"
11 
12 : ${FORMAT:="%f (in %h)"}
13 
14 function help() {
15  cat <<-EOH
16  Looks for FHICL files in the ART FHICL search directories.
17 
18  Usage: ${SCRIPTNAME} [options] [--] [Pattern]
19 
20  Options:
21  --format=FORMAT
22  use the specified format; the default format is "%f (in %h)";
23  the placeholder are as documented in find (1) manual
24  --name , -n
25  print the file name only
26  --path , -p
27  print the full path
28 
29  EOH
30 } # help()
31 
32 
33 function isFlagSet() {
34  local VarName="$1"
35  [[ -n "${!VarName//0}" ]]
36 } # isFlagSet()
37 
38 function STDERR() { echo "$*" >&2 ; }
39 function ERROR() { STDERR "ERROR: $@" ; }
40 function FATAL() {
41  local Code="$1"
42  shift
43  STDERR "FATAL ERROR (${Code}): $*"
44  exit $Code
45 } # FATAL()
46 function LASTFATAL() {
47  local Code="$?"
48  [[ "$Code" != 0 ]] && FATAL "$Code""$@"
49 } # LASTFATAL()
50 
51 function Filter() {
52  local Pattern="$1"
53  if [[ -n "$Pattern" ]]; then
54  grep -e "$Pattern"
55  else
56  cat
57  fi
58 } # Filter()
59 
60 function CleanSortKey() { sed -e 's/^[^ ]* \+//' ; }
61 
62 function ApplyFormat() {
63  local Format="$1"
64  local Data
65  while read Data ; do
66  local FileName="${Data%%/*}"
67  local Path="${Data#*/}"
68 
69  case "$Format" in
70  ( '' )
71  echo "${FileName} (in ${Path})"
72  ;;
73  ( * )
74  sed -e "s@\([^\]|\^\)%f@${FileName}@" -e "s@\([^\]|\^\)%h@${Path}@" <<< "$Format"
75  esac
76  done
77 }
78 
79 ################################################################################
80 
81 declare -i NoMoreOptions=0
82 declare -a Patterns
83 declare -i nPatterns=0
84 for (( iParam = 1 ; iParam <= $# ; ++iParam )); do
85  Param="${!iParam}"
86  if ! isFlagSet NoMoreOptions && [[ "${Param:0:1}" == '-' ]]; then
87  case "$Param" in
88  ( '--help' | '-h' | '-?' ) DoHelp=1 ;;
89 
90  ### format options
91  ( '--name' | '-n' ) FORMAT="%f" ;;
92  ( '--path' | '-p' ) FORMAT="%p" ;;
93  ( '--format='* ) FORMAT="${Param#--*=}" ;;
94 
95  ### other stuff
96  ( '-' | '--' )
97  NoMoreOptions=1
98  ;;
99  ( * )
100  echo "Unrecognized script option #${iParam} - '${Param}'"
101  exit 1
102  ;;
103  esac
104  else
105  NoMoreOptions=1
106  Patterns[nPatterns++]="$Param"
107  fi
108 done
109 
110 if isFlagSet DoHelp ; then
111  help
112  # set the exit code (0 for help option, 1 for missing parameters)
113  isFlagSet DoHelp
114  exit $?
115 fi
116 
117 # explanation:
118 # - start with paths in FHICL_FILE_PATH
119 # - split by ":"
120 # - find in all those directories (but not in their subdirectories),
121 # and in that order, all the FCL files
122 # - print for each its name and the string to be presented to the user as output
123 # - soft them by FCL file name, preserving the relative order of files with the
124 # same name from different directories
125 # - filter them on sort key (file name) by user's request
126 # - remove the sort key (file name) from the output
127 tr ':' "\n" <<< "$FHICL_FILE_PATH" | xargs -I SEARCHPATH find SEARCHPATH -maxdepth 1 -name "*.fcl" -printf "%f ${FORMAT}\n" 2> /dev/null | sort -s -k1,1 -u | Filter "^[^ ]*${Patterns[0]}[^ ]* " | CleanSortKey