1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
<!--startcut ======================================================= -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<META NAME="generator" CONTENT="lgazmail v1.3E.d">
<TITLE>The Answer Gang 57: shell script</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
LINK="#3366FF" VLINK="#A000A0">
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<P> <hr>
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<P> <hr> <P>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<center>
<H1><A NAME="answer">
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
border="0" align="middle">
<font color="#B03060">The Answer Gang</font>
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
border="0" align="middle">
</A></H1>
<BR>
<H4>By James T. Dennis,
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a><BR>
LinuxCare,
<A HREF="http://www.linuxcare.com/">http://www.linuxcare.com/</A>
</H4>
</center>
<p><hr><p>
<!-- endcut ======================================================= -->
<!-- begin 4 -->
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
height="50" width="60" alt="(?) " border="0"
>shell script</H3>
<p><strong>From Peter Truong on Mon, 28 Aug 2000
</strong></p>
<p align="right">Answered By: Ben Okopnik</p>
<P><STRONG><IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
I have a directory consisting of:
</STRONG></P>
<pre><strong>
test01.in test05.in test99.in <-- in files
test01.out test05.out test99.out <-- out files
</strong></pre>
<P><STRONG>
this is my code:
</STRONG></P>
<Pre><STRONG>
infile=`test[0-9][0-9]*.in`
outfile=`test[0-9][0-9]*.out`
</STRONG></Pre>
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
This is at least one of the reasons it doesn't work. What you seem to
be trying to do here is create a list of files under the "$infile" and
"$outfile" labels <TT>-</TT> but by putting the right side of the equation in
backquotes, you're asking the shell to _execute_ it. That won't work;
in fact, you should get an error message when you run this.
</BLOCKQUOTE>
<Pre><STRONG><IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
for input in $infile
do
for output in $outfile
a.out < $input > $output
</STRONG></Pre>
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
What this will do is execute "a.out" and use the current value of "$input"
as the file for it to process, then output the result into the filename
that is the current value of "$output" (overwriting whatever was in there
originally). You didn't mention this part of the script in your explanation
of what you want the script to do, but this _will_ wreck your "*.out"
files. This "double" loop will also repeat the above process as many times
as there are output files (if the original "list of files" equation worked)
for <EM>each</EM> input file, i.e., if you have 50 "*.in/*.out" pairs, the inside
loop will execute 2500 times <TT>-</TT> and the end result will be the "processed"
value of the last file in the "input" list written to every file in the
"output" list.
</BLOCKQUOTE>
<Pre><STRONG><IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
cmp $input $output
</STRONG></Pre>
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
This part, of course, becomes useless after the above procedure: either
"a.out" changes "$input", in which case it will always be different, or
it does not change it, in which case it will always be identical.
</BLOCKQUOTE>
<Pre><STRONG><IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
echo $?
done
</STRONG></Pre>
<P><STRONG>
but this however, doesn't work.
what I want it to do is:
</STRONG></P>
<P><STRONG>
<ul>
<li> get each of the individual pairs of files (ie. test01.in & test02.out)
<li> and compare each pair until there is no more to compare.
</ul></STRONG></P>
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
All right; try this -
</BLOCKQUOTE>
<BLOCKQUOTE><pre>
--------------------------------------------------------------------
#!/bin/bash
#
# "in_out" - compares all <fname>.in to <fname>.out files in the
# current directory
for n in *.in
do
cmp $n $(basename $n in)out
done
--------------------------------------------------------------------
</pre></BLOCKQUOTE>
<BLOCKQUOTE>
It's basic, but worth repeating: the "hash-bang" line comes first in any
shell script and must be contiguous (no spaces). If the script requires
input, write an error-checking routine and print usage instructions on an
error; otherwise, as in this one, comments will help you remember what it
does (you may remember it today, but not 3 years and 1,000 scripts down the
road.) Next, the loop <TT>-</TT> for each "in" file, we generate the "out" filenames
via "basename", by chopping off the "in" extension and tacking on an "out".
Then we perform the comparison and print a message for every pair that
fails: "cmp" exits silently on a "good" comparison, and prints a message on
a bad one. If you want numeric output, you can use "cmp <TT>-s</TT>" (for "silent"
output, i.e., it only sets the status flag) and "$?" to print the status
value.
</BLOCKQUOTE>
<BLOCKQUOTE>
Good luck with your shell-scripting,
<br>Ben Okopnik
</BLOCKQUOTE>
<!-- sig -->
<!-- end 4 -->
<!--startcut ======================================================= -->
<P> <hr> </p>
<H5 align="center"><a href="http://www.linuxgazette.com/copying.html"
>Copyright ©</a> 2000, James T. Dennis
<BR>Published in the <I>Linux Gazette</I> Issue 57 September 2000</H5>
<H6 ALIGN="center">HTML transformation by
<A HREF="mailto:star@tuxtops.com">Heather Stern</a> of
Tuxtops, Inc.,
<A HREF="http://www.tuxtops.com/">http://www.tuxtops.com/</A>
</H6>
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0">
<A HREF="../lg_answer57.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer57.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A>
<A HREF="1.html">1</A>
<A HREF="2.html">2</A>
<A HREF="3.html">3</A>
<A HREF="4.html">4</A>
<A HREF="5.html">5</A>
<A HREF="6.html">6</A>
<A HREF="7.html">7</A></td>
<td align="left" valign="center"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A>
<IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<P> <hr>
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
</BODY></HTML>
<!--endcut ========================================================= -->
|