From f9008c7e2f38697cab81972beeafb6910be18377 Mon Sep 17 00:00:00 2001 From: Gustavo Neves Date: Mon, 15 May 2017 06:26:12 -0300 Subject: [PATCH] CAT is DEADED, removed the need for CAT and HEAD. Renamed -m c, cat+grep to -g, grep. Old way is preserved for compatibility reasons; Some extra dependecy checks added, more which to work --- README.md | 5 +++-- pdfScale.sh | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cee03cf..60a79bd 100644 --- a/README.md +++ b/README.md @@ -301,10 +301,11 @@ Valid Ghostscript Paper Sizes accepted ``` ## Dependencies -The script uses `basename`, `cat`, `grep`, `bc`, `head` and `gs` (ghostscript). +The script uses `basename`, `grep`, `bc` and `gs` (ghostscript). You probably have everything installed already, except for ghostscript. Optional dependencies are `imagemagick`, `pdfinfo` and `mdls` (Mac). This app is focused in `Bash`, so it will probably not run in other shells. +The script will need to see the dependencies on your `$PATH` variable. ##### apt-get ``` @@ -321,7 +322,7 @@ brew install ghostscript ##### Optionals Page Size detection is by default in Adaptive Mode. It will try the following methods in sequence: - 1. Try to get `/MediaBox` with `cat` + `grep` + 1. Try to get `/MediaBox` with `grep` (fastest) 2. Failed AND MacOS ? Try `mdls` 3. Failed ? Try `pdfinfo` 4. Failed ? Try ImageMagick's `identify` diff --git a/pdfScale.sh b/pdfScale.sh index e3c711f..4bc1f66 100755 --- a/pdfScale.sh +++ b/pdfScale.sh @@ -221,15 +221,18 @@ pageResize() { # Loads external dependencies and checks for errors initDeps() { + GREPBIN="$(which grep 2>/dev/null)" GSBIN="$(which gs 2>/dev/null)" BCBIN="$(which bc 2>/dev/null)" IDBIN=$(which identify 2>/dev/null) MDLSBIN="$(which mdls 2>/dev/null)" PDFINFOBIN="$(which pdfinfo 2>/dev/null)" - vprint "Checking for ghostscript and bcmath" - if notIsAvailable "$GSBIN"; then printDependency 'ghostscript'; fi - if notIsAvailable "$BCBIN"; then printDependency 'bc'; fi + vprint "Checking for grep, ghostscript and bcmath" + notIsAvailable "$GSBIN" && printDependency 'ghostscript' + notIsAvailable "$BCBIN" && printDependency 'bc' + notIsAvailable "$GREPBIN" && printDependency 'grep' + return $TRUE } # Checks for dependencies errors, run after getting options @@ -248,6 +251,7 @@ checkDeps() { initError 'mdls executable was not found! Is this even MacOS?' $EXIT_MAC_MDLS_NOT_FOUND 'nobanner' fi fi + return $TRUE } @@ -338,7 +342,7 @@ parseScale() { parseMode() { local param="$(lowercase $1)" case "${param}" in - c|catgrep|'cat+grep') + c|catgrep|'cat+grep'|grep|g) ADAPTIVEMODE=$FALSE MODE="CATGREP" return $TRUE @@ -417,7 +421,7 @@ parseAutoRotationMode() { ################################################################ # Detects operation mode and also runs the adaptive mode # PAGESIZE LOGIC -# 1- Try to get Mediabox with CAT/GREP +# 1- Try to get Mediabox with GREP # 2- MacOS => try to use mdls # 3- Try to use pdfinfo # 4- Try to use identify (imagemagick) @@ -427,7 +431,7 @@ getPageSize() { if isNotAdaptiveMode; then vprint " Get Page Size: Adaptive Disabled" if [[ $MODE = "CATGREP" ]]; then - vprint " Method: Cat + Grep" + vprint " Method: Grep" getPageSizeCatGrep elif [[ $MODE = "MDLS" ]]; then vprint " Method: Mac Quartz mdls" @@ -447,7 +451,7 @@ getPageSize() { fi vprint " Get Page Size: Adaptive Enabled" - vprint " Method: Cat + Grep" + vprint " Method: Grep" getPageSizeCatGrep if pageSizeIsInvalid && [[ $OSNAME = "Darwin" ]]; then vprint " Failed" @@ -548,7 +552,7 @@ getPageSizePdfInfo() { fi # get data from image magick - local identify="$("$PDFINFOBIN" "$INFILEPDF" 2>/dev/null | grep -i 'Page size:' )" + local identify="$("$PDFINFOBIN" "$INFILEPDF" 2>/dev/null | "$GREPBIN" -i 'Page size:' )" if isEmpty "$identify" && isNotAdaptiveMode; then notAdaptiveFailed "Linux PdfInfo returned an empty string!" @@ -574,7 +578,14 @@ getPageSizeCatGrep() { # /MediaBox[ 0 0 595.28 841.89 ] # Get MediaBox data if possible - local mediaBox="$(cat "$INFILEPDF" | grep -a '/MediaBox' | head -n1)" + #local mediaBox="$(cat "$INFILEPDF" | "$GREPBIN" -a '/MediaBox' | "$HEADBIN" -n1)" + #local mediaBox="$("$GREPBIN" -a -e '/MediaBox' "$INFILEPDF" | "$HEADBIN" -n1)" + local mediaBox="$("$GREPBIN" -a -e '/MediaBox' "$INFILEPDF" 2>/dev/null)"$'\n\n' + while read l; do + mediaBox="$l" + break + done <<< "$mediaBox" + mediaBox="${mediaBox##*/MediaBox}" # No page size data available @@ -1169,7 +1180,7 @@ Output filename: Page Size Detection Modes: a, adaptive Default mode, tries all the methods below - c, cat+grep Forces the use of the cat + grep method + g, grep Forces the use of grep method m, mdls Forces the use of MacOS Quartz mdls p, pdfinfo Forces the use of PDFInfo i, identify Forces the use of ImageMagick's Identify