<?xml version="1.0" encoding="ISO-8859-1"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:axle="http://www.cs.odu.edu/~zeil/axle" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ns0="http://www.cs.odu.edu/~zeil/axle" mode="ignore" id="shellscripts" ns0:xlinkuri="open/scripts.dbk" ns0:xpath="//*[@id='shellscripts']" ns0:page="scripts" foo="0 " ns0:last="/cocoon/~cs252/open/scripts/tracing.html"><head><title>Scripts</title><link type="text/css" href="_axle/docbookstyle.css" rel="stylesheet"/><link type="text/css" href="_axle/cppformat.css" rel="stylesheet"/><link type="text/css" href="_axle/overrides.css" rel="stylesheet"/><script src="_axle/docbook.js"/><link href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/open/scripts/section-1.html" title="Next" rel="next"/><link href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/open/scripts/tracing.html" title="Last" rel="last"/><script>function gotoNext() {top.location.href="/cocoon/~cs252/open/scripts/section-1.html";}
function gotoPrev() {}
function gotoUp() {}
</script><xhtml:link xmlns:xhtml="http://www.w3.org/1999/xhtml" rel="stylesheet" href="_axle/navigation.css" type="text/css"/><xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml" src="_axle/navigation.js"/></head><body><table width="100%" class="navigationBar"><tr class="navigationBar"><td width="5%" class="navigationBar"><a class="navigationBarArrow" title="Next" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/open/scripts/section-1.html"><img class="navigationBar" src="_axle/next.gif" alt="Next"/></a></td><td width="5%" class="navigationBar"><a class="navigationBarArrow" title="Last" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/open/scripts/tracing.html"><img class="navigationBar" src="_axle/last.gif" alt="Last"/></a></td><td class="navigationBar"/><td width="5%" class="navigationBar"><a class="navigationBar" title="CS252 Home" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/directory/announcements.html"><img class="navigationBar" src="_axle/home.gif" alt="CS252 Home"/></a></td><td width="5%" class="navigationBar"><a class="navigationBar" title="discuss this page" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/forum/getXPath/open/scripts/scripts.html"><img class="navigationBar" src="_axle/forum.gif" alt="discuss this page"/></a></td></tr></table><br/><div class="info_article"><div class="title_article">Scripts</div><div class="author_article"><span class="firstname">Steven</span> <span class="othername" role="mi">J</span> <span class="surname">Zeil</span></div><div class="affiliation">
        <span class="orgname">Old Dominion University</span>
        <span class="orgdiv">Dept. of Computer Science</span>
      </div><table width="100%"><tr><td align="left"><span class="copyright">©<span class="year">2000-2006</span> <span class="holder">Steven J. Zeil, Old Dominion University</span></span></td><td/><td align="right"><span class="date">Apr. 9, 2006</span></td></tr></table></div><div class="TOC">Contents:<div class="TOC-section0"><a href="section-1.html#section-1">1 Parameters</a></div><div class="TOC-section0"><a href="controlflow.html#controlflow">2 Control Flow</a></div><div class="TOC-section0"><a href="section-3.html#section-3">3 The <span class="code">test</span> and <span class="code">expr</span> Programs</a></div><div class="TOC-section0"><a href="section-4.html#section-4">4 Scripting Example: The Student Test Manager</a></div><div class="TOC-section1"><a href="section-4.1.html#section-4.1">4.1 Sample Scenario</a></div><div class="TOC-section1"><a href="section-4.2.html#section-4.2">4.2 The Script</a></div><div class="TOC-section0"><a href="scriptDebugging.html#scriptDebugging">5 Debugging Your Scripts</a></div><div class="TOC-section1"><a href="section-5.1.html#section-5.1">5.1 Isolate the Problem</a></div><div class="TOC-section1"><a href="section-5.2.html#section-5.2">5.2 Add Debugging Output</a></div><div class="TOC-section1"><a href="tracing.html#tracing">5.3 Trace the Execution</a></div></div>
  
  <div class="epigraph">
    <p class="para" align="center">
              (A printable version of this document is available 
              <a url="allPages.html" href="allPages.html">here</a>.)
            </p>
  </div>
  <p class="para">You can put any sequence of Unix commands into a file and turn that
file into a command. Such a file is called a <span class="emphasis">script</span>. For
example, suppose that you are working on a program <span class="code">myprog</span>
and have several files of test data that you run through it each time
you make a change.  Create a file <span class="code">dotest1</span> with the following
lines:
<div class="programlisting">
   ./myprog &lt; test1.dat &gt; test1.dat.out
   ./myprog &lt; test2.dat &gt; test2.dat.out
   ./myprog &lt; test3.dat &gt; test3.dat.out
   ./myprog &lt; test4.dat &gt; test4.dat.out
   ./myprog &lt; test5.dat &gt; test5.dat.out 
</div>
You can't execute <span class="code">dotest1</span> yet, because you don't have execute
permission. (Do <span class="code">ls -l dotest1</span> to see this.)  So use the
<span class="code">chmod</span> command to add execute permission:
<div class="screen"><span class="userinput">
chmod u+x dotest1
</span></div>
Now you can execute <span class="code">dotest1</span> by simply typing
<div class="screen"><span class="userinput">
./dotest1
</span></div></p>
  <p class="para">Most shells provide special facilities for use in scripts. Since these
differ from one shell to another, it's a good idea to tell Unix which
shell to use when running the script. You do this by placing the
command <span class="code">#!/bin/csh</span> in the first line of the
script.</p>
  <p class="para">In fact, you can list any program there, not just
<span class="code">/bin/csh</span>, and Unix will use that program to process the
remainder of the lines in the script.
<div class="itemizedlist"><ul><li>Any text file with execute permission can be invoked as a
program.</li><li>The first line must identify the program to be run (after a
starting <span class="code">#!</span>).</li><li>The remaining lines are fed to the standard input of that
program.</li></ul></div>
So another possibility for executing your test code would be to 
put the line <span class="code">#!./myprog</span> at the front of each of the test data
input files <span class="code">test1.dat</span>, <span class="code">test2.dat</span>, &#8230;, and then execute
those data files!</p>
  <script type="text/javascript">
checkForAnchorOnOtherPage('section-1','section-1');
</script>
  <script type="text/javascript">
checkForAnchorOnOtherPage('controlflow','controlflow');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('fn_fref1','controlflow');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('fn_fref1_live','controlflow');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('fn_fnote1','controlflow');
</script>
  <script type="text/javascript">
checkForAnchorOnOtherPage('section-3','section-3');
</script>
  <script type="text/javascript">
checkForAnchorOnOtherPage('section-4','section-4');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('section-4.1','section-4.1');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('section-4.2','section-4.2');
</script>


  <script type="text/javascript">
checkForAnchorOnOtherPage('scriptDebugging','scriptDebugging');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('section-5.1','section-5.1');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('section-5.2','section-5.2');
</script><script type="text/javascript">
checkForAnchorOnOtherPage('tracing','tracing');
</script>
<br/><table width="100%" class="navigationBar"><tr class="navigationBar"><td width="5%" class="navigationBar"><a class="navigationBarArrow" title="Next" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/open/scripts/section-1.html"><img class="navigationBar" src="_axle/next.gif" alt="Next"/></a></td><td width="5%" class="navigationBar"><a class="navigationBarArrow" title="Last" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/open/scripts/tracing.html"><img class="navigationBar" src="_axle/last.gif" alt="Last"/></a></td><td class="navigationBar"/><td width="5%" class="navigationBar"><a class="navigationBar" title="CS252 Home" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/directory/announcements.html"><img class="navigationBar" src="_axle/home.gif" alt="CS252 Home"/></a></td><td width="5%" class="navigationBar"><a class="navigationBar" title="discuss this page" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/forum/getXPath/open/scripts/scripts.html"><img class="navigationBar" src="_axle/forum.gif" alt="discuss this page"/></a></td></tr></table></body></html>