@@ 1,17 1,12 @@
#!/bin/bash
name="debom"
-version="1.0"
+version="1.1"
read -r -d '' help_message <<-EOF
Remove BOM from a target file
Usage: debom TARGET [OPTIONS]
Options:
- -b, --backup output a backup file, editing TARGET in-place
- -d, --dump print to STDOUT
- -f, --force overwrite without asking
-h, --help print this message
- -n FILE, --name FILE set output filename (Default: TARGET.new, or
- TARGET.bak in backup mode)
-q, --quiet suppress error messages and prompts
-v, --verbose show additional messages
-V, --version print version number and exit
@@ 22,42 17,14 @@ source /usr/local/lib/mylib.bash
positional=()
quiet=0
verbose=0
-backup=0
-dump=0
-force=0
-output_fn=""
while [[ $# -gt 0 ]]; do
case "$1" in
- -b|--backup)
- debug_msg "Setting backup option to 1 (was ${backup})"
- backup=1
- shift
- ;;
-
- -d|--dump)
- debug_msg "Setting dump option to 1 (was ${dump})"
- dump=1
- shift
- ;;
-
- -f|--force)
- debug_msg "Setting force option to 1 (was ${force})"
- force=1
- shift
- ;;
-
-h|--help)
help_msg
shift
;;
- -n|--name)
- debug_msg "Setting output filename to ${2} (was ${output_fn})"
- output_fn="$2"
- shift; shift
- ;;
-
-q|--quiet)
debug_msg "Setting quiet option to 1 (was ${quiet})"
quiet=1
@@ 84,67 51,18 @@ done
# error if no filenames given
if [[ ${#positional[@]} -eq 0 ]]; then
- debug_msg "No input filename was given"
usage_msg
fi
-original_fn="${positional[0]}"
-# output filename
-if [[ "$dump" -eq 1 ]]; then
- debug_msg "Output is STDOUT"
-elif [[ -z "$output_fn" ]]; then
- if [[ "$backup" -eq 1 ]]; then
- debug_msg "No output filename was given, so defaulting to 'TARGET.bak'"
- output_fn="${original_fn}.bak"
- else
- debug_msg "No output filename was given, so defaulting to 'TARGET.new'"
- output_fn="${original_fn}.new"
- fi
-else
- debug_msg "Output filename given as '${output_fn}'"
-fi
-
-# check if files exist
-debug_msg "Beginning file existence checks"
-if [[ ! -f "$original_fn" ]]; then
- debug_msg "Input file does not exist"
- error_msg "No such file '${original_fn}'"
-elif [[ "$dump" -eq 0 && "$force" -eq 0 && -f "$output_fn" ]]; then
- debug_msg "Output file exists; prompting for permission to overwrite"
- if ! prompt_overwrite "${output_fn}"; then
- debug_msg "Could not obtain permission to overwrite output file"
- exit 1
- fi
-fi
-debug_msg "Finished file existence checks"
-
-# backup routine
-if [[ "$dump" -eq 0 && "$backup" -eq 1 ]]; then
- debug_msg "Beginning backup routine"
- backup_fn="${output_fn}"
- if ! /usr/local/bin/mkbak "${original_fn}" --force --name "${backup_fn}"; then
- error_msg "Error occured while executing backup"
- exit 1
- fi
- output_fn=${original_fn}
- original_fn=${backup_fn}
- debug_msg "Finished backup routine"
-fi
-
-# main routine
-debug_msg "Beginning main routine"
code=0
-if [[ "$dump" -eq 1 ]]; then
- if ! /usr/bin/sed '1s/^\xEF\xBB\xBF//' < "${original_fn}"; then
+for filename in "${positional[@]}"; do
+ if [ ! -f "$filename" ]; then
+ nonfatal_error_msg "No such file '$filename'"
code=1
- fi
-else
- if ! /usr/bin/sed '1s/^\xEF\xBB\xBF//' < "${original_fn}" > "${output_fn}"; then
+ elif ! /usr/bin/sed '1s/^\xEF\xBB\xBF//' < "${filename}" 2>/dev/null; then
code=1
fi
-fi
-debug_msg "Finished main routine"
+done
-# return stored code
exit "$code"
@@ 1,36 0,0 @@
-#!/bin/sh
-# tests demon and rebom
-
-# move to temp directory
-cd test
-
-test_exit_code() {
- $1 >/dev/null 2>&1
- if [ $? -ne $2 ]; then
- printf "Failure in BOM tests: wrong exit code ('$1' returned '$?', should be '$2')\n"
- exit 1
- fi
-}
-test_exit_code "../debom -V" "0"
-test_exit_code "../debom -h" "0"
-test_exit_code "../debom" "1"
-test_exit_code "../debom non-existant-file" "1"
-test_exit_code "../rebom -v" "0"
-test_exit_code "../rebom -h" "0"
-test_exit_code "../rebom" "1"
-test_exit_code "../rebom non-existant-file" "1"
-
-# try de-BOM-ing a file
-../debom -d debom_target.txt > debom_result.txt
-if ! cmp rebom_target.txt debom_result.txt >/dev/null 2>&1; then
- printf "Failure in BOM tests: de-BOM-ed file is corrupt\n"
- exit 1
-fi
-
-# try re-BOM-ing a file
-../rebom rebom_target.txt > rebom_result.txt
-if ! cmp debom_target.txt rebom_result.txt >/dev/null 2>&1; then
- printf "Failure in BOM tests: re-BOM-ed file is corrupt\n"
- exit 1
-fi
-