title: git pull check if a file changed bash
body: <p>I run following bash script in cronjob to update my web site</p>
  
  <pre><code>#!/bin/bash
  
  cd /home/username/public_html
  git pull origin master
  </code></pre>
  
  <p>This cronjob is run every 5 minutes, so if i commit changed to git repo, it get pulled to live web site.</p>
  
  <p>What i want to do is, each time this script run, check if a particular file is changed, run a curl/wget command if the file is changed.</p>
  
  <p>The file i need to track for changes are style.css and script.js</p>
  
  <p>What i am doing is, if these file is changed, update revision number of the script in my web site template, so cache is cleared and latest changed css and js file is loaded.</p>
  
  <pre><code>&lt;link href="/css/style.css?REVISION_NUMBER_HERE" rel="stylesheet" type="text/css"&gt;
  </code></pre>
  
  <p>I can increase JS/CSS revision number easily with a curl request as it is stored in mysql DB.</p>
  
  <p>Here is a sample git pull result, so we can grep for that style.css/script.js in the result</p>
  
  <pre><code> * branch            master     -&gt; FETCH_HEAD
  Updating cbd0d46..36aeed6
  Fast-forward
   admin/cf.php         |   16 ++++++++++++++++
   css/style.css        |   47 +++++++++++++++++++++++++++--------------------
   include/common.php   |    1 +
   templates/header.tpl |    4 ++--
   templates/index.tpl  |   13 ++++++-------
   5 files changed, 52 insertions(+), 29 deletions(-)
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks @torek for the reply.</p>
  
  <p>I need some more help with BASH scripting, now here is my test git command.</p>
  
  <pre><code>[root@server70 public_html]# git diff --name-only 8613bd3ed718b27ca680bdf33e0a4b32b0f58b8c HEAD | egrep '.css|.js|.tpl'
  css/style.css
  templates/header.tpl
  templates/index.tpl
  templates/user/delete.tpl
  [root@server70 public_html]#
  </code></pre>
  
  <p>I put hard coded git rev number for testing. My bash script</p>
  
  <pre><code>[root@server70 public_html]# sh 1.sh
  File changed css/style.css
  File changed templates/header.tpl
  File changed templates/index.tpl
  File changed templates/user/delete.tpl
  [root@server70 public_html]# cat 1.sh
  #!/bin/bash
  
  
  CSS_JS_CHANGED=0
  
  git diff --name-only 8613bd3ed718b27ca680bdf33e0a4b32b0f58b8c HEAD | egrep '.css|.js|.tpl' |
  while read name; do
     CSS_JS_CHANGED=1
     echo "File changed $name"
  done
  
  if [ "$CSS_JS_CHANGED" = "1" ]; then
      echo expression evaluated as true
  fi
  
  [root@server70 public_html]#
  </code></pre>
  
  <p>If i run my script inside while, it get executed multiple times if multiple files changed. I only need to run my script once per change.</p>
  
  <p>So i Need to set a variable CSS_JS_CHANGED, set its value to 0. Then if a file change is detected, set its value to 1. Then run my script if value of CSS_JS_CHANGED is 1.</p>
  
  <p>But for some reason, i can't set value of CSS_JS_CHANGED to 1 inside the while loop. Anything to do with variable scope ?</p>
  
  <p>** EDIT 2 **</p>
  
  <p>I find why i can't set variable from inside while loop <a href="https://stackoverflow.com/questions/4667509/problem-accessing-a-global-variable-from-within-a-while-loop">here</a></p>
  
  <p>Being a bash noob, can someone get my script working please :)</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Got it working with out CSS_JS_CHANGED variable. </p>
  
  <pre><code>[root@server70 public_html]# cat update
  #!/bin/bash
  # */5 * * * * /home/USERNAME/public_html/update &gt;/dev/null 2&gt;&amp;1
  
  
  cd /home/USERNAME/public_html
  OLD_HEAD=$(git rev-parse HEAD)
  git pull origin master
  NEW_HEAD=$(git rev-parse HEAD)
  
  [ $OLD_HEAD = $NEW_HEAD ] &amp;&amp; exit 0
  
  git diff --name-only $OLD_HEAD $NEW_HEAD | egrep '.css|.js' |
  while read name; do
     touch /tmp/css_js_update
  done
  
  if [ -f "/tmp/css_js_update" ]
  then
      /usr/bin/curl 'http://mywebsite.com/admin/cf.php?js_version_update=mywebsite'
      rm -f /tmp/css_js_update
  fi
  [root@server70 public_html]#
  </code></pre>
  
  <p>Now waiting for a .css/.js file commit :)</p>
  
  <p>Thanks again @torek</p>