title: How to grep for a string pattern from command output in shell script?
body: <p>I am compressing my pdf file using ghostscript which throws error on password protected case which I have to handle.</p>
  
  <p><strong>Shell script</strong></p>
  
  <pre class="lang-sh prettyprint-override"><code>GS_RES=`gs -sDEVICE=pdfwrite -sOutputFile=$gsoutputfile -dNOPAUSE -dBATCH $2 2&gt;&amp;1`
  
  if [ "$GS_RES" != "" ]
  then
      gspassmsg="This file requires a password for access"
      echo "Error message is :::::: "$GS_RES
      gspassworddoc=`awk -v a="$GS_RES" -v b="$gspassmsg" 'BEGIN{print index(a,b)}'`
      if [ $gspassworddoc -ne 0 ]
      then
          exit 3 #error code - password protected pdf
      fi
  fi
  </code></pre>
  
  <p>And my <code>GS_RES</code> value after executing the command is like the following</p>
  
  <p><strong>Error message 1:</strong></p>
  
  <pre><code>GPL Ghostscript 9.19 (2016-03-23) Copyright (C) 2016 Artifex Software, Inc. All 
  rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for d
  etails. Error: /syntaxerror in -file- Operand stack: Execution stack: %interp_ex
  it .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --n
  ostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1967 1 3 %opa
  rray_pop 1966 1 3 %oparray_pop 1950 1 3 %oparray_pop 1836 1 3 %oparray_pop --nos
  tringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringva
  l-- 2 %stopped_push Dictionary stack: --dict:1196/1684(ro)(G)-- --dict:0/20(G)--
   --dict:78/200(L)-- Current allocation mode is local Current file position is 1
  </code></pre>
  
  <p><strong>Error message 2:</strong></p>
  
  <pre><code>GPL Ghostscript 9.19 (2016-03-23) Copyright (C) 2016 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Error: Cannot find a 'startxref' anywhere in the file. Output may be incorrect. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Error: An error occurred while reading an XREF table. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html The file has been damaged. This may have been caused gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html by a problem while converting or transfering the file. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Ghostscript will attempt to recover the data. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html However, the output may be incorrect. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Error: Trailer dictionary not found. Output may be incorrect. No pages will be processed (FirstPage &gt; LastPage). gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html This file had errors that were repaired or ignored. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Please notify the author of the software that produced this gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html file that it does not conform to Adobe's published PDF gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html specification. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html The rendered output from this file may be incorrect.
  </code></pre>
  
  <p>On running awk on Error message 2 </p>
  
  <pre><code>gspassmsg="This file requires a password for access"
  gspassworddoc=`awk -v a="$GS_RES" -v b="$gspassmsg" 'BEGIN{print index(a,b)}'`
  </code></pre>
  
  <p>It throws me the following error </p>
  
  <p>Error : <code>awk: newline in string GPL Ghostscript 9.19... at source line 1</code></p>
  
  <p><strong>Error message 3</strong></p>
  
  <pre><code>   **** Error: Cannot find a 'startxref' anywhere in the file.
     **** Warning:  An error occurred while reading an XREF table.
     **** The file has been damaged.  This may have been caused
     **** by a problem while converting or transfering the file.
     **** Ghostscript will attempt to recover the data.
     **** Error:  Trailer is not found.
  
     **** This file had errors that were repaired or ignored.
     **** Please notify the author of the software that produced this
     **** file that it does not conform to Adobe's published PDF
     **** specification.
  </code></pre>
  
  <p>I couldn't capture this error with the snippet from the below answer</p>
  
  <pre><code>if ! gs_res=$(gs -sDEVICE=pdfwrite -sOutputFile="$gsoutputfile" -dNOPAUSE -dBATCH "$2" 2&gt;&amp;1 1&gt;/dev/null); then
    echo "Error message is :::::: $gs_res" &gt;&amp;2
    gspassmsg='This file requires a password for access'
    [[ $gs_res == *"$gspassmsg"* ]] &amp;&amp; exit 3 # password protected pdf
    echo "Some other error !"
  fi
  </code></pre>
  
  <p><strong>Please clarify me the following</strong></p>
  
  <ol>
  <li>Why <code>awk</code> behaves weird here? What I'm missing?</li>
  <li>How to grep for a pattern in a string which contains special characters?</li>
  <li>Does Ghostscript has any predefined error messages like that? If possible please suggest some documentation to refer..</li>
  <li>Is it possible to compress password protected pdf with ghostscript?</li>
  <li>How can i ensure for gs compression success in the above case? Since I may not know about different possible error which Ghostscript may throw so that i could cross check with my executed command result.</li>
  </ol>
  
  <p>I am quite new to this shell script. Someone please help me on this.</p>
  
  <p>PS: I have edited my question with additional details. Please look into it. If something has to be added i'll add it.</p>