Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # extsort.bash
- # do some operations with lists in bash such as lists of file extensions or
- # required binaries. demo for loop and regexp techniques
- # extension lists
- VIDEXTS="mkv mp4 avi flv webm wmv mpg ogv VOB wmv ts"
- AUDEXTS="flac mp3 pcm mp2 mpa mpc ogg oga ape m4a aac aiff au wma wav wv"
- IMGEXTS="gif png jpg tiff xpm pcx svg"
- DOCEXTS="doc txt ps pdf dvi asc conf html"
- # requirement binaries list (example)
- REQUIRES="bash du date tput diff ls" # these should be standard and present
- #REQUIRES="joe dos2unix clickedit nopass" # these might fail (testing)
- ########################################################################
- # FOR LOOP TECHNIQUE
- # a function to see if we have all the binaries
- check_requirements()
- {
- local OUT= # uninitialized
- L=("${1}") # a local copy of a list turned into an array
- for R in ${L[@]} # iterate through array
- do
- P=$(which "${R}") #check for each binary in PATH
- if [[ ! "${P}" ]] ; then # OOPS we are missing one
- OUT="${R}" # which one
- break # bail
- fi
- done
- if [[ ! ${OUT} ]]; then # nothing missing?
- echo "ALL" # good
- else
- echo "${OUT}" # bad. echo which binary was not found
- fi
- } #check_req
- REQS=$(check_requirements "${REQUIRES}")
- if [ "X${REQS}" != "XALL" ] ; then
- (>&2 echo "${REQS} was not found in the the following paths:")
- (>&2 echo "${PATH}")
- exit 1
- fi
- #################################################################
- # REGEXP TECHNIQUE
- # operate on files we are interested in
- let vcounter=0 # some integers for counting
- let acounter=0
- let icounter=0
- let counter=0
- let n=0
- declare -a FileList # define some string arrays
- declare -a vext
- declare -a aext
- declare -a iext
- declare -a vfiles
- declare -a afiles
- declare -a ifiles
- FileList=( "$@" ) # make an array from the lists for iteration
- vext=("${VIDEXTS}")
- aext=("${AUDEXTS}")
- iext=("${IMGEXTS}")
- n=${#FileList[@]} # number of elements in array
- while (( counter < n )) # arithmetic evaluation
- do
- F=${FileList[counter]} # array indexing
- ext="${F##*.}" # regexp ( VAR##*. = "everything after the last '.' ")
- # video or audio or image file?
- if [[ "${VIDEXTS}" =~ "${ext}" ]] ; then # (" does ext exist in VIDEXTS?" )
- vfiles+=( ${F} )
- vcounter=$((vcounter + 1))
- fi
- if [[ "${AUDEXTS}" =~ "${ext}" ]] ; then # (" does ext exist in AUDEXTS?" )
- afiles+=( ${F} )
- acounter=$((acounter + 1))
- fi
- if [[ "${IMGEXTS}" =~ "${ext}" ]] ; then # (" does ext exist in IMGEXTS?" )
- ifiles+=( ${F} )
- icounter=$((icounter + 1))
- fi
- counter=$((counter + 1))
- done
- # so we emerge with
- # video files sorted into vfiles array
- echo "${vcounter} video files: ${vfiles[@]}"
- # audio files sorted into afiles array
- echo "${acounter} audio files: ${afiles[@]}"
- # image files sorted into ifiles array
- echo "${icounter} image files: ${ifiles[@]}"
- #END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement