2 get_path_hierarchy_level
() { 
   3     # Desc: Outputs hierarchy level of input paths 
   4     # Example: $ cat lines.txt | get_path_hierarchy_level 
   5     # Input: stdin    str  lines with /-delimited paths 
   6     # Output: stdout  int  hierarchy level of each path 
  14     while read -r line
; do 
  15         # Check for mixed absolute/relative paths. 
  16         if [[ $n -le 0 ]] && [[ "$line" =~ ^
/ ]]; then 
  21         if { [[ "$flag_root" == "true" ]] && [[ ! "$line" =~ ^
/ ]]; } || \
 
  22            { [[ "$flag_root" == "false" ]] && [[ "$line" =~ ^
/ ]]; } then 
  23             echo "FATAL:Mixed relative and absolute paths not supported." 1>&2; return 1; 
  26         # Squeeze multiple slashes and remove trailing slashes 
  27         line
="$(echo "$line" | tr -s '/' | sed 's:/*$::' )"; 
  29         # Count the number of slashes to determine hierarchy level 
  30         level
="$(echo "$line" | awk -F'/' '{print NF-1}' )"; 
  31         if [[ "$flag_root" == "true" ]]; then ((level--
)); fi; 
  35         #declare -p flag_root level; # debug 
  39     printf "%s\n" "${output[@]}"; 
  40 }; # return hierarchy level of lines as integers 
  42 # Test the function with the provided lines 
  43 printf "\n\n========Test 1========\n" 
  56 printf "%s\n" "${input_lines[@]}"; 
  57 printf "========Test 1 results:========\n"; 
  58 # Convert array to newline-separated string and pass to function 
  59 printf "%s\n" "${input_lines[@]}" | get_path_hierarchy_level
 
  61 printf "\n\n========Test 2========\n" 
  74 printf "%s\n" "${input_lines[@]}"; 
  75 printf "========Test 2 results:========\n"; 
  76 # Convert array to newline-separated string and pass to function 
  77 printf "%s\n" "${input_lines[@]}" | get_path_hierarchy_level