Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- DEF_SOURCE=/media/H6_SD/FOLDER01/
- DEF_TARGET=/media/ramdisk/
- DEF_FTYPE=.WAV
- DEF_IFS=$IFS
- SOURCE=$DEF_SOURCE
- TARGET=$DEF_TARGET
- FTYPE=$DEF_FTYPE
- TMP_FLIST=
- LIST=empty
- STAT=ok
- typeset -i TMP_COUNT=0
- typeset -i FAIL=0
- function ERROR() # displays error message and explains how things work out
- {
- printf "\033[0;31mError! $1\033[0;32m\n\n"
- cat <<EOF
- unzoom can be used to extract files from the folder structure created by the ZOOM H6 recorder. It seeks the files inside these
- folders, selects them by timestamp entries (year, month, day, hour) and/or file numbers and - if desired - copies them to a
- custom target folder.
- Usage: unzoom.bash [-estymdhn]
- Options: -e <extension> ..... Set file type (default is $DEF_FTYPE)
- -s <path> .......... Set source directory (default is $DEF_SOURCE)
- -t <path> .......... Set target directory (default is $DEF_TARGET)
- -y <n1,n2,nX-nY> ... Filter by year (must be written exactly as in the file list)
- -m <n1,n2,nX-nY> ... Filter by month
- -d <n1,n2,nX-nY> ... Filter by day
- -h <n1,n2,nX-nY> ... Filter by hour
- -n <n1,n2,nX-nY> ... Filter by file number
- When setting a file extension, consider that everything is case sensitive. Each filter option must be followed by either a single
- number, several numbers separated by comma or a range of numbers specified by the first and the last number divided by a "-". And
- remember: If you intend to filter by year, the value must be written exactly as given in the file list (i.e. if year is 2010 then
- write 2010 and not just 10). You can combine all the filter options at discretion. Using no filter will of course bring up the
- entire file list.
- Beware: When copying files, all existing files with same names in the target folder will be overwritten without request!
- EOF
- printf "\033[0m\n"
- exit 1
- }
- function GET_PATH() # retrieves absolute path to given folder if -s or -t option was called
- {
- NEW_PATH=$1
- if [ -d "$NEW_PATH" ]; then
- cd "$NEW_PATH"
- NEW_PATH=$(pwd)"/"
- else
- if [ -z $2 ]; then
- ERROR "Directory \"$NEW_PATH\" does not exist."
- else
- echo -e "\n\033[0;31m\"$NEW_PATH\" not found - nothing changed.\033[0m"
- PRINT_LIST
- fi
- fi
- }
- function CHECK_MATCH() # checks if current file specs match with filter settings and calls BUILD_LIST() function
- {
- typeset -i NUM=$(echo "obase=10;$1" | bc)
- if [ $NUM_LO -eq -1 ] || [ $NUM -ge $NUM_LO ]; then
- if [ $NUM_HI -eq -1 ] || [ $NUM -le $NUM_HI ]; then BUILD_LIST; fi
- fi
- }
- function BUILD_LIST() # builds file lists, skips existing lines and counts gathered entries
- {
- NEW_LIST=$(printf "$NEW_LIST")"\n"$LINE
- TMP_LINE=$(printf "/$FILE\t time stamp: $YEAR $MONTH $DAY $HOUR (year, month, day, hour)")
- PREV_IFS=$IFS
- IFS=$'\n'
- for SEG in $(printf "$TMP_FLIST"); do
- if [ "$TMP_LINE" = "$SEG" ]; then TMP_LINE=; fi
- done
- IFS=$PREV_IFS
- if [ -n "$TMP_LINE" ]; then
- TMP_FLIST=$(printf "$TMP_FLIST")"\n"$TMP_LINE
- TMP_COUNT=$TMP_COUNT+1
- fi
- }
- function CHECK_ARG() # parses argument, performs integrity check and calls FILTER() function
- { # $STAT must be "ok" to proceed to FILTER() function; otherwise only check is performed
- IFS=","
- for VAL in $1; do
- NUM_LO=${VAL%-*}
- if [ -z $NUM_LO ]; then NUM_LO=-1; fi
- NUM_HI=${VAL#*-}
- if [ -z $NUM_HI ]; then NUM_HI=-1; fi
- if [[ ! $NUM_LO =~ ^-?[0-9]+$ ]] || [[ ! $NUM_HI =~ ^-?[0-9]+$ ]]; then
- if [ -z "$NEW_OPT" ]; then
- ERROR "Missing or wrong argument for \"$2\""
- else
- FILTER_OPT=$OLD_FILTER
- echo -ne "\n\033[0;31mNumerical value expected for \"$2\" - skipping filter treatment.\033[0m\n"
- PRINT_LIST
- fi
- fi
- done
- IFS=$DEF_IFS
- if [ $STAT = ok ]; then FILTER $1 $2; fi
- }
- function FILTER() # reads and filters the files according to the filter options set by the user
- {
- if [ "$LIST" = empty ]; then
- IFS=$DEF_IFS
- for PROJECT in $(ls "$SOURCE"); do
- if [ -d "$SOURCE$PROJECT" ]; then
- if ls "$SOURCE$PROJECT"/*$FTYPE >/dev/null 2>/dev/null; then
- if [ "$LIST" = empty ]; then LIST=; fi
- LIST=$(printf "$LIST")"\n"$(ls -gn --time-style=long-iso "$SOURCE$PROJECT"/*$FTYPE)
- fi
- fi
- done
- fi
- if [ ! "$LIST" = empty ]; then
- IFS=$'\n'
- for LINE in $(printf %b "$LIST"); do
- if [ -n "$LINE" ]; then
- DATE=$(echo -e $(echo $LINE | sed 's/ /\\n/g') | sed -n '5p')
- YEAR=$(echo -e $(echo $DATE | sed 's/-/\\n/') | sed -n '1p')
- MONTH=$(echo -e $(echo $DATE | sed 's/-/\\n/g') | sed -n '2p')
- DAY=$(echo -e $(echo $DATE | sed 's/-/\\n/g') | sed -n '3p')
- TIME=$(echo -e $(echo $LINE | sed 's/ /\\n/g') | sed -n '6p')
- HOUR=${TIME%:*}
- FILE=${LINE#*\ /}
- PNAME=${FILE%/*}
- FRONT=${FILE#$PNAME/ZOOM}
- REAR=${FRONT#????}
- if [ ! $1 = nothing ]; then
- if [ $2 = h ]; then CHECK_MATCH $HOUR; fi
- if [ $2 = d ]; then CHECK_MATCH $DAY; fi
- if [ $2 = m ]; then CHECK_MATCH $MONTH; fi
- if [ $2 = y ]; then CHECK_MATCH $YEAR; fi
- if [ $2 = n ]; then CHECK_MATCH ${FRONT%$REAR}; fi
- else
- BUILD_LIST
- fi
- fi
- done
- IFS=","
- fi
- IFS=$DEF_IFS
- LIST=$(printf "$NEW_LIST")
- NEW_LIST=
- FLIST=$TMP_FLIST
- TMP_FLIST=
- COUNT=$TMP_COUNT
- TMP_COUNT=0
- }
- function BACKUP() # saves variable for PRINT_LIST() function and writes them back if error occurs
- {
- if [ -z $1 ]; then
- FILTER_OPT=$OLD_FILTER
- FLIST=$OLD_FLIST
- FTYPE=$OLD_FTYPE
- SOURCE=$OLD_SOURCE
- LIST=$OLD_LIST
- else
- OLD_FILTER=$FILTER_OPT
- OLD_FLIST=$FLIST
- OLD_FTYPE=$FTYPE
- OLD_SOURCE=$SOURCE
- OLD_LIST=$LIST
- fi
- }
- function PRINT_LIST() # checks if file list contains something, prints it out and asks for next steps
- {
- if [ -z $1 ]; then
- if [ -z "$FLIST" ]; then
- if [ -z $MENU_TRG ]; then
- ERROR $ERROR_MSG.
- else
- echo -e "\n\033[0;31m"$ERROR_MSG" - nothing changed.\033[0m"
- BACKUP
- PRINT_LIST
- fi
- fi
- printf "$FLIST\n"
- fi
- MENU_TRG=on
- echo -e "\n$COUNT files selected. Source is: "\"$SOURCE\""; target is: "\"$TARGET\"". You may now press..."
- echo -e "\033[0;32m(C)\033[0m to copy these files to target, \033[0;32m(E)\033[0m to change file type & reread, \
- \033[0;32m(S)\033[0m to change source & reread, \033[0;32m(T)\033[0m to change target, "
- echo -ne "\033[0;32m(F)\033[0m to apply some (more) filter options, \033[0;32m(U)\033[0m to undo last filter option & reread, \
- \033[0;32m(R)\033[0m to reset filter & reread, \033[0;32mENTER\033[0m to cancel & quit: "
- read -n 1 OPT
- echo
- case $OPT in
- c|C)
- echo
- IFS=$'\n'
- typeset -i COUNT=0
- for PROCESS in $(printf "$FLIST"); do
- FILE_PATH=${PROCESS%$'\t'" time stamp:"*}
- if cp -v --preserve=timestamps $FILE_PATH $TARGET; then
- COUNT=$COUNT+1
- else
- FAIL=$FAIL+1
- fi
- done
- IFS=$DEF_IFS
- echo -e "\nDone - "$COUNT" file(s) copied, "$FAIL" failed."
- PRINT_LIST 1
- ;;
- f|F)
- echo -ne "Possible options are \033[0;32my m d h\033[0m and \033[0;32mn\033[0m plus corresponding value. Enter new filter options ("
- if [ -z "$FILTER_OPT" ]; then
- read -p "none used so far): " NEW_OPT
- else
- read -p "current are \"${FILTER_OPT#\ *}\"): " NEW_OPT
- fi
- if [ -z "$NEW_OPT" ]; then PRINT_LIST 1; fi
- ERROR_MSG="New filter setting would result in an empty list"
- BACKUP save
- FILTER_OPT=$NEW_OPT
- STAT=check
- PARSE_OPT_MENU
- FILTER_OPT=$OLD_FILTER" "$NEW_OPT
- STAT=ok
- PARSE_OPT_MENU
- PRINT_LIST
- ;;
- u|U)
- if [ -z "$FILTER_OPT" ]; then
- echo -e "\n\033[0;31mNo filter used - nothing to undo.\033[0m"
- PRINT_LIST 1
- fi
- LIST=empty
- FILTER_OPT=${FILTER_OPT%\ *\ *}
- PARSE_OPT_CLI
- PRINT_LIST
- ;;
- e|E)
- read -p "Enter new file extension (current is \"$FTYPE\"): " FTYPE
- if [ -z "$FTYPE" ]; then PRINT_LIST 1; fi
- ERROR_MSG="No files with the new extension \"$FTYPE\" found"
- BACKUP save
- LIST=empty
- PARSE_OPT_CLI
- PRINT_LIST
- ;;
- s|S)
- read -p "Enter new source path (current is \"$SOURCE\"): " NEW_PATH
- if [ -z "$NEW_PATH" ]; then PRINT_LIST 1; fi
- ERROR_MSG="New source \"$NEW_PATH\" doesn't contain any useful data"
- GET_PATH "$NEW_PATH" 1
- BACKUP save
- LIST=empty
- SOURCE=$NEW_PATH
- PARSE_OPT_CLI
- PRINT_LIST
- ;;
- t|T)
- read -p "Enter new target path (current is \"$TARGET\"): " NEW_PATH
- if [ -z "$NEW_PATH" ]; then PRINT_LIST 1; fi
- GET_PATH "$NEW_PATH" 1
- TARGET=$NEW_PATH
- PRINT_LIST
- ;;
- r|R)
- LIST=empty
- FILTER_OPT=
- FILTER nothing
- PRINT_LIST
- ;;
- "")
- exit
- ;;
- ?)
- echo -e "\n\033[0;31mUnknown option: \"$OPT\" - try again!\033[0m"
- PRINT_LIST
- ;;
- esac
- }
- function PARSE_OPT_MENU() # parses new filter options set from the menu and calls CHECK_ARG() function
- {
- for PART in $FILTER_OPT; do
- if [ ! "$ARG" = set ]; then
- OPTION=${PART#-*}
- case $OPTION in
- h|d|m|y|n)
- ARG=set
- ;;
- ?)
- echo -ne "\n\033[0;31mUnknown option: \"$OPTION\" - skipping filter treatment.\033[0m\n"
- FILTER_OPT=$OLD_FILTER
- PRINT_LIST
- ;;
- esac
- else
- ARG=$PART
- fi
- if [ ! "$ARG" = set ]; then CHECK_ARG $ARG $OPTION; fi
- done
- }
- function PARSE_OPT_CLI() # parses filter options from the command line and calls CHECK_ARG() function
- {
- if [ -z "$FILTER_OPT" ]; then
- FILTER nothing
- else
- for PART in $FILTER_OPT; do
- if [ ! "$ARG" = set ]; then
- OPTION=${PART#-*}
- ARG=set
- else
- ARG=$PART
- fi
- if [ ! "$ARG" = set ]; then CHECK_ARG $ARG $OPTION; fi
- done
- fi
- }
- while getopts :e:s:t:h:d:m:y:n: OPT; do
- case $OPT in
- e)
- FTYPE=$OPTARG
- if [[ ! $FTYPE = .* ]]; then FTYPE=.$FTYPE; fi
- ;;
- s)
- GET_PATH "$OPTARG"
- SOURCE=$NEW_PATH
- ;;
- t)
- GET_PATH "$OPTARG"
- TARGET=$NEW_PATH
- ;;
- h|d|m|y|n)
- FILTER_OPT=$FILTER_OPT" -"$OPT" "$OPTARG
- ;;
- :)
- ERROR $OPTARG" requires an argument."
- ;;
- ?)
- ERROR "Unknown option: "\"$OPTARG\"
- ;;
- esac
- done
- STAT=check
- PARSE_OPT_CLI
- STAT=ok
- PARSE_OPT_CLI
- PRINT_LIST
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement