doc_to_docx.sh 872 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. usage() {
  4. echo "Usage: $(basename "$0") <file.doc> [output_directory]"
  5. echo "Convert .doc to .docx using LibreOffice."
  6. exit 1
  7. }
  8. if [ $# -lt 1 ]; then
  9. usage
  10. fi
  11. INPUT="$1"
  12. OUTDIR="${2:-.}"
  13. if [ ! -f "$INPUT" ]; then
  14. echo "Error: File not found: $INPUT"
  15. exit 1
  16. fi
  17. if ! command -v soffice &>/dev/null; then
  18. echo "Error: soffice (LibreOffice) is required for .doc conversion but not found."
  19. echo "Install LibreOffice: brew install --cask libreoffice"
  20. exit 1
  21. fi
  22. BASENAME=$(basename "$INPUT" .doc)
  23. mkdir -p "$OUTDIR"
  24. echo "Converting: $INPUT -> $OUTDIR/$BASENAME.docx"
  25. soffice --headless --convert-to docx --outdir "$OUTDIR" "$INPUT" >/dev/null 2>&1
  26. OUTPUT="$OUTDIR/$BASENAME.docx"
  27. if [ ! -f "$OUTPUT" ]; then
  28. echo "Error: Conversion failed. Output file not created: $OUTPUT"
  29. exit 1
  30. fi
  31. echo "Success: $OUTPUT"