Advertisement
spikeysnack

extsort.bash

Oct 21st, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.05 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # extsort.bash
  4.  
  5. # do some operations with lists in bash such as lists of file extensions or
  6. # required binaries. demo for loop and regexp techniques
  7.  
  8.  
  9. # extension lists
  10. VIDEXTS="mkv mp4 avi flv webm wmv mpg ogv VOB wmv ts"
  11. AUDEXTS="flac mp3 pcm mp2 mpa mpc ogg oga ape  m4a aac aiff au wma wav wv"
  12. IMGEXTS="gif png jpg tiff xpm pcx svg"
  13. DOCEXTS="doc txt ps pdf dvi asc conf html"
  14.  
  15. # requirement binaries list (example)
  16. REQUIRES="bash du date tput diff ls" # these should be standard and present
  17. #REQUIRES="joe dos2unix clickedit nopass" # these might fail (testing)
  18.  
  19. ########################################################################
  20. # FOR LOOP TECHNIQUE
  21. # a function to see if we have all the binaries
  22.  
  23. check_requirements()
  24. {
  25.     local OUT=      # uninitialized
  26.     L=("${1}")      # a local copy of a list turned into an array
  27.  
  28.     for R in ${L[@]}    # iterate through array
  29.     do
  30.     P=$(which "${R}")  #check for each binary in PATH
  31.  
  32.     if [[ ! "${P}" ]] ; then    # OOPS we are missing one
  33.         OUT="${R}"              # which one
  34.         break                   # bail
  35.     fi
  36.     done
  37.  
  38.     if [[ ! ${OUT} ]]; then   # nothing missing?
  39.     echo "ALL"            # good
  40.     else
  41.     echo "${OUT}"         # bad. echo which binary was not found
  42.     fi
  43. } #check_req
  44.  
  45.  
  46. REQS=$(check_requirements "${REQUIRES}")
  47.  
  48. if [ "X${REQS}" != "XALL" ] ; then
  49.     (>&2 echo "${REQS} was not found in the the following paths:")
  50.     (>&2 echo "${PATH}")
  51.     exit 1
  52. fi
  53.  
  54. #################################################################
  55. # REGEXP TECHNIQUE
  56. # operate on files we are interested in
  57.  
  58. let vcounter=0  # some integers for counting
  59. let acounter=0
  60. let icounter=0
  61. let counter=0
  62. let n=0
  63.  
  64. declare -a FileList  # define some string arrays
  65. declare -a vext
  66. declare -a aext
  67. declare -a iext
  68.  
  69. declare -a vfiles
  70. declare -a afiles
  71. declare -a ifiles
  72.  
  73. FileList=( "$@" )      # make an array from the lists for iteration
  74. vext=("${VIDEXTS}")
  75. aext=("${AUDEXTS}")
  76. iext=("${IMGEXTS}")
  77.  
  78. n=${#FileList[@]}           # number of elements in array
  79.  
  80. while (( counter < n ))     # arithmetic evaluation
  81. do
  82.     F=${FileList[counter]}  # array indexing
  83.  
  84.     ext="${F##*.}"          # regexp ( VAR##*. =  "everything after the last '.' ")
  85.  
  86.     # video or audio or image file?
  87.     if [[ "${VIDEXTS}" =~ "${ext}" ]] ; then # (" does ext exist in VIDEXTS?" )
  88.     vfiles+=( ${F} )
  89.         vcounter=$((vcounter + 1))
  90.     fi
  91.    
  92.     if [[ "${AUDEXTS}" =~ "${ext}" ]] ; then # (" does ext exist in AUDEXTS?" )
  93.     afiles+=( ${F} )
  94.         acounter=$((acounter + 1))
  95.     fi
  96.  
  97.  
  98.     if [[ "${IMGEXTS}" =~ "${ext}" ]] ; then # (" does ext exist in IMGEXTS?" )
  99.     ifiles+=( ${F} )
  100.         icounter=$((icounter + 1))
  101.     fi
  102.    
  103.     counter=$((counter + 1))
  104.  
  105. done
  106.  
  107.  
  108. # so we emerge with
  109. #    video files sorted into vfiles array
  110. echo "${vcounter} video files:  ${vfiles[@]}"
  111.  
  112. #    audio files sorted into afiles array
  113. echo "${acounter} audio files:  ${afiles[@]}"
  114.  
  115. #    image files sorted into ifiles array
  116. echo "${icounter} image files:  ${ifiles[@]}"
  117.  
  118. #END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement