| 1 |
\documentclass{ltxdoc} % Process with xelatex -shell-escape |
| 2 |
\usepackage[verbose,unique]{bashful} |
| 3 |
|
| 4 |
\usepackage[colorlinks=true]{hyperref} |
| 5 |
\usepackage{gensymb} |
| 6 |
\usepackage{graphicx} |
| 7 |
\usepackage{metalogo} |
| 8 |
\usepackage{xkvview} |
| 9 |
\usepackage{xspace} |
| 10 |
\usepackage{amsmath} |
| 11 |
\usepackage{multicol} |
| 12 |
|
| 13 |
\newcommand\me{bashful} |
| 14 |
\newcommand\bashful{\textsf{\me}\xspace} |
| 15 |
\lstdefinestyle{input}{basicstyle=\ttfamily\footnotesize, |
| 16 |
keywords={},upquote=true,extendedchars=false, |
| 17 |
showstringspaces=false,aboveskip=0pt,belowskip=0pt} |
| 18 |
\lstdefinestyle{scriptsize}{style=input,basicstyle=\ttfamily\scriptsize} |
| 19 |
|
| 20 |
% listings style for the script, standard output file, and standard error file. |
| 21 |
\lstdefinestyle{bashfulScript}{style=input} |
| 22 |
\lstdefinestyle{bashfulStdout}{style=input} |
| 23 |
\lstdefinestyle{bashfulStderr}{style=input, |
| 24 |
basicstyle=\ttfamily\footnotesize\color{red}} |
| 25 |
\newcommand\listFile[1]{% |
| 26 |
\vspace{0.8em plus 0.3em minus 0.3em}% |
| 27 |
\lstinputlisting[style=input,frameround=ftttt,frame=trBL]{#1}% |
| 28 |
\vspace{0.8em plus 0.3em minus 0.3em}} |
| 29 |
|
| 30 |
\title{The \bashful Package\thanks{ |
| 31 |
Copyright \copyright{} 2011, 2012 by Yossi Gil |
| 32 |
\url{mailto:[email protected]}. |
| 33 |
This work may be distributed and/or modified under the conditions of the |
| 34 |
\emph{\LaTeX{} Project Public License} (LPPL), either version 1.3 of this |
| 35 |
license or (at your option) any later version. |
| 36 |
The latest version of this license is in |
| 37 |
\url{http://www.latex-project.org/lppl.txt} and version 1.3 or later |
| 38 |
is part of all distributions of \LaTeX{} version 2005/12/01 or later. |
| 39 |
This work has the LPPL maintenance status `maintained'. |
| 40 |
The Current Maintainer of this work is Yossi Gil. |
| 41 |
This work consists of the files \texttt{\me.tex} and \texttt{\me.sty} |
| 42 |
and the derived file |
| 43 |
\texttt{\me.pdf} |
| 44 |
}} |
| 45 |
|
| 46 |
\author{Yossi Gil\thanks{\url{mailto:[email protected]}}\\ |
| 47 |
\normalsize Department of Computer Science\\ |
| 48 |
\normalsize The Technion---Israel Institute of Technology\\ |
| 49 |
\normalsize Technion City, Haifa 32000, Israel |
| 50 |
} |
| 51 |
|
| 52 |
\makeatletter |
| 53 |
\date{\date@bashful\thanks{ |
| 54 |
This document describes \bashful \version@bashful.}} |
| 55 |
\makeatother |
| 56 |
|
| 57 |
\begin{document} |
| 58 |
\bash |
| 59 |
cat << EOF > README |
| 60 |
The bashful package, v 0.93 |
| 61 |
|
| 62 |
This package makes it possible to execute bash scripts from within LaTeX. The |
| 63 |
main application is in writing computer-science texts, in which you want to |
| 64 |
make sure the programs listed in the document are executed directly from the |
| 65 |
input. |
| 66 |
|
| 67 |
This package may be distributed and/or modified under the LaTeX Project Public |
| 68 |
License, version 1.3 or higher (your choice). The latest version of this |
| 69 |
license is at: http://www.latex-project.org/lppl.txt |
| 70 |
|
| 71 |
This work is author-maintained (as per LPPL maintenance status) |
| 72 |
by Yossi Gil, <[email protected]> |
| 73 |
EOF |
| 74 |
\END |
| 75 |
|
| 76 |
\bash[verbose,stdoutFile=bashful.date] |
| 77 |
stat -c %y bashful.sty | sed -e s+-+/+g -e 's/ .*//g' > date |
| 78 |
\END |
| 79 |
|
| 80 |
\maketitle |
| 81 |
|
| 82 |
\begin{abstract} |
| 83 |
\parindent 1.5ex |
| 84 |
\parskip 0.5em |
| 85 |
|
| 86 |
\sl |
| 87 |
It is sometimes useful to ``\emph{escape-to-shell}'' from within |
| 88 |
\LaTeX{}. |
| 89 |
The most obvious application is when the document |
| 90 |
explains something about the working of a computer program. |
| 91 |
Your text would be more robust to changes, and easier to write, |
| 92 |
if all the examples it gives, are run directly from |
| 93 |
within \LaTeX{}. |
| 94 |
|
| 95 |
To facilitate this and other applications, |
| 96 |
package \bashful{} provides a convenient interface to \TeX's |
| 97 |
primitive \verb+\write18+---the execution of shell commands from within |
| 98 |
your input files, also known as \emph{shell escape}. |
| 99 |
Text between \verb+\bash+ and \verb+\END+ is executed by |
| 100 |
\href |
| 101 |
{http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29} |
| 102 |
{\texttt{bash}}, |
| 103 |
a popular Unix command line interpreter. |
| 104 |
Various flags control whether the executed commands and their output |
| 105 |
show up in the printed document, and whether they are saved |
| 106 |
to files. |
| 107 |
|
| 108 |
Although provisions are made for using shells other |
| 109 |
than \texttt{bash}, this package may \emph{not} operate without |
| 110 |
modifications on Microsoft's operating systems. |
| 111 |
\end{abstract} |
| 112 |
|
| 113 |
\begin{multicols}{2} |
| 114 |
\footnotesize |
| 115 |
\tableofcontents |
| 116 |
\end{multicols} |
| 117 |
|
| 118 |
\parindent 1.5ex |
| 119 |
\parskip 0.5em |
| 120 |
|
| 121 |
\section{Introduction} |
| 122 |
|
| 123 |
\bash[verbose,scriptFile=temperature.sh,stdoutFile=temperature.tex] |
| 124 |
location=Jerusalem,Israel |
| 125 |
server="http://www.Google.com/ig/api" |
| 126 |
request="$server?weather=$location" |
| 127 |
wget -q -O - $request |\ |
| 128 |
tr "<>" "\012\012" |\ |
| 129 |
grep temp_c |\ |
| 130 |
sed 's/[^0-9]//g' |
| 131 |
\END |
| 132 |
|
| 133 |
\bash[verbose,scriptFile=condition.sh,stdoutFile=condition.tex] |
| 134 |
location=Jerusalem,Israel |
| 135 |
server="http://www.Google.com/ig/api" |
| 136 |
request="$server?weather=$location" |
| 137 |
wget -q -O - $request |\ |
| 138 |
tr "<>" "\012\012" |\ |
| 139 |
grep "condition data" |\ |
| 140 |
head -n 1 |\ |
| 141 |
sed -e 's/^.*="//' -e 's/"\/*//' |\ |
| 142 |
tr 'A-Z' 'a-z' |
| 143 |
\END |
| 144 |
|
| 145 |
At the time I run this document through \LaTeX{}, |
| 146 |
the temperature in Jerusalem, Israel, |
| 147 |
was~\emph{\input{temperature}\unskip\celsius}, |
| 148 |
while the weather condition was |
| 149 |
\emph{\input{condition}}\unskip. |
| 150 |
|
| 151 |
You may not care so much about these bits of truly |
| 152 |
ephemeral information, |
| 153 |
but you may be surprised that they were produced |
| 154 |
by the very process of \LaTeX{}ing the input. |
| 155 |
|
| 156 |
\bash |
| 157 |
cat << EOF > ls.tex |
| 158 |
\documentclass{article} |
| 159 |
\usepackage[a6paper]{geometry} |
| 160 |
\usepackage{bashful} |
| 161 |
\pagestyle{empty} |
| 162 |
\begin{document} |
| 163 |
The directories in my \texttt{/usr} directory are: |
| 164 |
\bash[stdout] |
| 165 |
ls -F /usr |
| 166 |
EOF |
| 167 |
echo "\\END" >> ls.tex |
| 168 |
cat << EOF >> ls.tex |
| 169 |
That's it! |
| 170 |
\end{document} |
| 171 |
EOF |
| 172 |
xelatex -shell-escape ls.tex |
| 173 |
\END |
| 174 |
|
| 175 |
Before I tell you how I generated this information, |
| 176 |
let me demonstrate the use of the \bashful package for the purpose of |
| 177 |
incorporating the list of files in a folder into your output. |
| 178 |
|
| 179 |
This simple \LaTeX{} file generates a listing of all files |
| 180 |
in the \texttt{/usr} directory, using the UNIX \texttt{ls} |
| 181 |
command: |
| 182 |
|
| 183 |
\begin{minipage}{\textwidth} |
| 184 |
\listFile{ls.tex} |
| 185 |
\end{minipage} |
| 186 |
|
| 187 |
The printed output of this file is then |
| 188 |
|
| 189 |
\begin{center} |
| 190 |
\fbox{\includegraphics[scale=0.8,trim=20 200 40 50]{ls.pdf}} |
| 191 |
\end{center} |
| 192 |
|
| 193 |
To generate the weather information, I wrote |
| 194 |
a series of shell commands that retrieve the current temperature, |
| 195 |
and another such series to obtain the current |
| 196 |
weather conditions. |
| 197 |
This task required connection to |
| 198 |
\href{http://www.Google.com/support/forum/p/% |
| 199 |
apps-apis/thread?tid=0c95e45bd80def1a&hl=en}% |
| 200 |
{Google's weather service} and |
| 201 |
minimal dexterity with Unix pipes and filters to process the output. |
| 202 |
|
| 203 |
My command series to obtain the current temperature was: |
| 204 |
|
| 205 |
\begin{minipage}{\textwidth} |
| 206 |
\begin{quote} |
| 207 |
\lstinputlisting[style=input]{temperature.sh} |
| 208 |
\end{quote} |
| 209 |
\end{minipage} |
| 210 |
|
| 211 |
while the weather condition was obtained by |
| 212 |
|
| 213 |
\begin{minipage}{\textwidth} |
| 214 |
\begin{quote} |
| 215 |
\lstinputlisting[style=input]{condition.sh} |
| 216 |
\end{quote} |
| 217 |
\end{minipage} |
| 218 |
|
| 219 |
The second step was coercing \LaTeX{} to run these commands |
| 220 |
while processing my document. |
| 221 |
To do that, I used package \bashful, |
| 222 |
\begin{verbatim} |
| 223 |
\usepackage{bashful} |
| 224 |
\end{verbatim} |
| 225 |
And, then, I wrapped each of these two series within |
| 226 |
a \verb+\bash+\ldots\verb+\END+ pair. |
| 227 |
|
| 228 |
The \verb+\bash+ command, offered by this package, |
| 229 |
takes all subsequent lines, stopping at the closing \verb+\END+, |
| 230 |
places these in a file, and then |
| 231 |
lets the \texttt{bash} shell interpreter execute this file. |
| 232 |
|
| 233 |
Allowing \LaTeX{} to run arbitrary shell commands can be |
| 234 |
dangerous---you never know whether that nice looking \texttt{.tex} |
| 235 |
file you received by email was prepared by a friend or |
| 236 |
a foe. |
| 237 |
This is the reason that you have to tell \LaTeX{} |
| 238 |
explicitly that shell escapes |
| 239 |
are allowed. |
| 240 |
The \texttt{-shell-esc} flag does that. |
| 241 |
To process my document, I typed, at the command line, |
| 242 |
\begin{quote} |
| 243 |
\tt |
| 244 |
\% latex -shell-escape \jobname.tex |
| 245 |
\end{quote} |
| 246 |
|
| 247 |
What I actually wrote in the input |
| 248 |
to produce the temperature in |
| 249 |
Jerusalem, Israel was: |
| 250 |
|
| 251 |
\begin{minipage}{\textwidth} |
| 252 |
\begin{quote} |
| 253 |
\noindent\verb+\bash[verbose,scriptFile=temperature.sh,stdoutFile=temperature.tex]+ |
| 254 |
\lstinputlisting[style=input,belowskip=0pt]{temperature.sh} |
| 255 |
\verb+\END+\\ |
| 256 |
\end{quote} |
| 257 |
\end{minipage} |
| 258 |
|
| 259 |
The flags passed to the \verb+bash+ control sequence above instructed it: |
| 260 |
\begin{enumerate} |
| 261 |
\item to be verbose, typing out a detailed log of everything it did; |
| 262 |
\item to save the shell commands in a script file named |
| 263 |
\texttt{temperature.sh}; and, |
| 264 |
\item to store the standard output of the script in a file named |
| 265 |
\texttt{temperature.tex}. |
| 266 |
\end{enumerate} |
| 267 |
|
| 268 |
To obtain the current weather condition in the capital I wrote: |
| 269 |
|
| 270 |
\begin{minipage}{\textwidth} |
| 271 |
\begin{quote} |
| 272 |
\noindent\verb+\bash[verbose,scriptFile=condition.sh,stdoutFile=condition.tex]+ |
| 273 |
\lstinputlisting[style=input]{condition.sh} |
| 274 |
\verb+\END+ |
| 275 |
\end{quote} |
| 276 |
\end{minipage} |
| 277 |
|
| 278 |
I wrote these two just after my \verb+\begin{document}+. |
| 279 |
When \LaTeX{} encountered these, it executed the bash commands and |
| 280 |
created two files \texttt{temperature.tex} and \texttt{condition.tex}. |
| 281 |
|
| 282 |
Subsequently, I could use the content of these files by writing: |
| 283 |
|
| 284 |
\begin{quote} |
| 285 |
\bash |
| 286 |
sed -n "/^At the time/,/^You may not/ p" bashful.tex > init.tex |
| 287 |
\END |
| 288 |
|
| 289 |
\lstinputlisting[style=input,belowskip=0pt]{init.tex}\ldots |
| 290 |
\end{quote} |
| 291 |
|
| 292 |
\section{Application for Teaching Programming} |
| 293 |
\bashful primary application is for writing documents which describe |
| 294 |
computer programming. |
| 295 |
You can include the programs in your text, and have them compiled |
| 296 |
and executed as part of the \LaTeX{} processing. |
| 297 |
To demonstrated I will first tell a simple story |
| 298 |
of writing, compiling and executing and |
| 299 |
a short program. |
| 300 |
Then, I will explain how I used the \verb+\bash+ |
| 301 |
command to not only tell the story, but |
| 302 |
also to play it live: that is, authoring |
| 303 |
a simple~C program, compiling it and executing |
| 304 |
it, all from within \LaTeX{}. |
| 305 |
|
| 306 |
\subsection{A ``Hello, World'' Program} |
| 307 |
|
| 308 |
\subsubsection{Authoring} |
| 309 |
Let's first write a simple |
| 310 |
\href{http://en.wikipedia.org/wiki/Hello_world_program} |
| 311 |
{Hello, World!} program in the |
| 312 |
\href{http://en.wikipedia.org/wiki/C_(programming_language)} |
| 313 |
{C programming language}: |
| 314 |
|
| 315 |
|
| 316 |
\bash[verbose,environment=quote,script] |
| 317 |
rm -f hello.c; cat << EOF > hello.c |
| 318 |
/* |
| 319 |
** hello.c: My first C program; it prints |
| 320 |
** "Hello, World!", and dies. |
| 321 |
*/ |
| 322 |
|
| 323 |
#include <stdio.h> |
| 324 |
|
| 325 |
int main() |
| 326 |
{ |
| 327 |
printf("Hello, World!\n"); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
EOF |
| 331 |
\END |
| 332 |
|
| 333 |
\subsubsection{Compiling} |
| 334 |
Now, let's compile this program: |
| 335 |
\bash[environment=quote,script,stdout] |
| 336 |
cc hello.c |
| 337 |
\END |
| 338 |
|
| 339 |
\subsubsection{Executing} |
| 340 |
Finally, we can execute this program, |
| 341 |
and see that indeed, it prints the ``Hello, World!'' |
| 342 |
string. |
| 343 |
\bash[environment=quote,script,stdout] |
| 344 |
./a.out |
| 345 |
\END |
| 346 |
|
| 347 |
|
| 348 |
\subsection{Behind the Scenes} |
| 349 |
\subsubsection{Authoring} |
| 350 |
What I wrote in the input to produce the |
| 351 |
\texttt{hello.c} program was: |
| 352 |
|
| 353 |
\begin{minipage}{\textwidth} |
| 354 |
\begin{quote} |
| 355 |
\begin{verbatim} |
| 356 |
\bash[script] |
| 357 |
rm -f hello.c; cat << EOF > hello.c |
| 358 |
/* |
| 359 |
** hello.c: My first C program; it prints |
| 360 |
** "Hello, World!", and dies. |
| 361 |
*/ |
| 362 |
|
| 363 |
#include <stdio.h> |
| 364 |
|
| 365 |
int main() |
| 366 |
{ |
| 367 |
printf("Hello, World!\n"); |
| 368 |
return 0; |
| 369 |
} |
| 370 |
EOF |
| 371 |
\END |
| 372 |
\end{verbatim} |
| 373 |
\end{quote} |
| 374 |
\end{minipage} |
| 375 |
|
| 376 |
In doing so, all the text between the \verb+\bash+ |
| 377 |
and \verb+\END+ was sent to a temporary file, |
| 378 |
which was then sent for execution. |
| 379 |
The \texttt{script} flag instructed \verb+\bash+ |
| 380 |
to list this file in the main document. |
| 381 |
This listing was prefixed with \verb*+% + |
| 382 |
to make it clear that it was input to \texttt{bash}. |
| 383 |
|
| 384 |
\subsubsection{Compiling} |
| 385 |
Next, I wrote |
| 386 |
\begin{quote} |
| 387 |
\begin{verbatim} |
| 388 |
\bash[script,stdout] |
| 389 |
cc hello.c |
| 390 |
\END |
| 391 |
\end{verbatim} |
| 392 |
\end{quote} |
| 393 |
|
| 394 |
As before, in doing that, I achieved two objectives: |
| 395 |
first, when \LaTeX{} processed |
| 396 |
the input, it also invokes the~C compiler to compile |
| 397 |
file \texttt{hello.c}, the file which I just created. |
| 398 |
|
| 399 |
Second, thanks to the \texttt{script} flag, |
| 400 |
the command for compiling this program |
| 401 |
was included in the printed version of |
| 402 |
this document. |
| 403 |
The \texttt{stdout} option instructed \verb+\bash+ |
| 404 |
to include plain messages, i.e., not error messages, |
| 405 |
produced by the compiler in |
| 406 |
the printed version of this document. |
| 407 |
In this case, no such messages were produced. |
| 408 |
|
| 409 |
|
| 410 |
\subsubsection{Executing} |
| 411 |
|
| 412 |
Finally, I wrote |
| 413 |
\begin{quote} |
| 414 |
\begin{verbatim} |
| 415 |
\bash[script,stdout] |
| 416 |
./a.out |
| 417 |
\END |
| 418 |
\end{verbatim} |
| 419 |
\end{quote} |
| 420 |
to run the program I just wrote. |
| 421 |
The \texttt{stdout} adds to my listing |
| 422 |
the output that this execution produces, i.e., |
| 423 |
the string \texttt{Hello, World!} that this |
| 424 |
execution produces to the standard output. |
| 425 |
|
| 426 |
\section{Dealing With Errors} |
| 427 |
Using \bashful{} to demonstrate |
| 428 |
my \emph{Hello, World!} program, made |
| 429 |
sure that the story I told is accurate: |
| 430 |
I really did everything I said I did. |
| 431 |
More accurately, the \verb+\bash+ command |
| 432 |
acted as my proxy, and did it for me. |
| 433 |
|
| 434 |
Luckily, my \texttt{hello.c} program was |
| 435 |
correct. |
| 436 |
But, if it was not, the \verb+\bash+ command would have detected |
| 437 |
the error, and would have stopped the \LaTeX{} process, |
| 438 |
indicating that the compilation did not succeed. |
| 439 |
More specifically, the \verb+\bash+ command |
| 440 |
\begin{enumerate} |
| 441 |
\item collects all commands up to \verb+\END+; |
| 442 |
\item places these commands in a script file; |
| 443 |
\item change directory to a designated directory if the \texttt{hide} |
| 444 |
option is set (the \texttt{dir} option sets the directory name); |
| 445 |
\item executes this script file, redirecting its standard output |
| 446 |
and its standard error streams to distinct files; |
| 447 |
\item checks whether the exit code of the execution indicates an error |
| 448 |
(i.e., exit code which is different from~$0$), and if so, |
| 449 |
place this exit code in a distinct file; |
| 450 |
\item checks whether the file containing the standard error is empty, |
| 451 |
and if not, pauses execution after displaying an error message; |
| 452 |
\item checks whether the file containing the exit code is empty, |
| 453 |
and if not, pauses execution after displaying an error message; |
| 454 |
\item lists, if requested to, the script file; |
| 455 |
\item lists, if requested to, the file containing the standard output; and, |
| 456 |
\item lists, if requested to, the file containing the standard error; |
| 457 |
\end{enumerate} |
| 458 |
|
| 459 |
Let me demonstrate a situation in which the execution of |
| 460 |
the script generates an error. |
| 461 |
To do that, I will write a short \LaTeX{} file, named \texttt{minimal.tex} |
| 462 |
which tries to use \verb+\bash+ to compile an incorrect~C program. |
| 463 |
Since \texttt{minimal.tex} contains \verb+\END+, |
| 464 |
I will have to author this file in three steps: |
| 465 |
\begin{enumerate} |
| 466 |
\item Creating the header of \texttt{minimal.tex}: |
| 467 |
\bash[script] |
| 468 |
cat << EOF > minimal.tex |
| 469 |
\documentclass{article} |
| 470 |
\usepackage[a6paper]{geometry} |
| 471 |
\usepackage{bashful} |
| 472 |
\pagestyle{empty} |
| 473 |
\begin{document} |
| 474 |
This document creates a simple erroneous C program |
| 475 |
and then compiles it: |
| 476 |
\bash[script,stdout] |
| 477 |
echo "main(){return int;}" > error.c |
| 478 |
cc error.c |
| 479 |
EOF |
| 480 |
\END |
| 481 |
\item Adding \verb+\END+ to \texttt{minimal.tex} |
| 482 |
\bash[script] |
| 483 |
echo "\\END" >> minimal.tex |
| 484 |
\END |
| 485 |
\item Finalizing \texttt{minimal.tex} |
| 486 |
\bash[script] |
| 487 |
echo "\\end{document}" >> minimal.tex |
| 488 |
\END |
| 489 |
\end{enumerate} |
| 490 |
|
| 491 |
Let me now make sure \texttt{minimal.tex} was what I expect it to be: |
| 492 |
|
| 493 |
\begin{minipage}{\textwidth} |
| 494 |
\bash[script,stdout] |
| 495 |
cat minimal.tex |
| 496 |
\END |
| 497 |
\end{minipage} |
| 498 |
|
| 499 |
I am now ready to run \texttt{minimal.tex} through \LaTeX{}, |
| 500 |
but since I will not run the \texttt{latex} command myself, |
| 501 |
I will send a ``\texttt{q}'' character |
| 502 |
to it to abort execution when the anticipated error occurs. |
| 503 |
|
| 504 |
\lstdefinestyle{bashfulScript}{style=scriptsize} |
| 505 |
\lstdefinestyle{bashfulStdout}{style=scriptsize} |
| 506 |
\bash[script,stdout] |
| 507 |
yes q | xelatex -shell-esc minimal.tex | sed /texmf-dist/d |
| 508 |
\END |
| 509 |
\lstdefinestyle{bashfulScript}{style=input} |
| 510 |
\lstdefinestyle{bashfulStdout}{style=input} |
| 511 |
|
| 512 |
You can see that when \LaTeX{} tried to process \texttt{minimal.tex}, |
| 513 |
it stopped execution while indicating that file |
| 514 |
\texttt{minimal.stderr} was not |
| 515 |
empty after the compilation. The first line of \texttt{minimal.stderr} |
| 516 |
was displayed, and I was advised to examine this file myself. |
| 517 |
Inspecting \texttt{minimal.stderr}, we see the C compiler error messages: |
| 518 |
|
| 519 |
\begin{minipage}{\textwidth} |
| 520 |
\bash[script,stdout] |
| 521 |
cat minimal.stderr |
| 522 |
\END |
| 523 |
\end{minipage} |
| 524 |
|
| 525 |
Note that the failure to compile \texttt{hello.c}, |
| 526 |
did not stop \verb+\bash+ from including |
| 527 |
this file in the source. |
| 528 |
|
| 529 |
Here is what \texttt{minimal.pdf} looks like: |
| 530 |
|
| 531 |
\begin{center} |
| 532 |
\fbox{\includegraphics[scale=0.8,trim=30 300 10 40]{minimal.pdf}} |
| 533 |
\end{center} |
| 534 |
|
| 535 |
\section{Other Commands} |
| 536 |
\begin{description} |
| 537 |
\item[\texttt{\textbackslash{}bashStdout}] |
| 538 |
After each execution of \verb+\bash+, the macro \verb+\bashStdout+ |
| 539 |
is defined to entire contents of |
| 540 |
the standard output of the executed script. |
| 541 |
|
| 542 |
For example, I can write |
| 543 |
\begin{quote} |
| 544 |
\begin{verbatim} |
| 545 |
To obtain the following sentence: |
| 546 |
\bash |
| 547 |
uname -o |
| 548 |
\END |
| 549 |
\begin{quote} |
| 550 |
``This document was prepared on \emph{\bashStdout}'' |
| 551 |
\end{quote} |
| 552 |
\end{verbatim} |
| 553 |
\end{quote} |
| 554 |
To obtain the following sentence: |
| 555 |
\bash |
| 556 |
uname -o |
| 557 |
\END |
| 558 |
\begin{quote} |
| 559 |
``This document was prepared on \emph{\bashStdout}'' |
| 560 |
\end{quote} |
| 561 |
|
| 562 |
\item[\texttt{\textbackslash{}bashStderr}] |
| 563 |
Similar to \verb+\bashStderr+, except that it |
| 564 |
is defined is defined to the standard error of the executed script. |
| 565 |
(Be ware that you must apply error tolerance flags |
| 566 |
to use this command, since normally, |
| 567 |
if the script generates anything to the standard error stream, |
| 568 |
\LaTeX{} processing will halt, asking for your attention.) |
| 569 |
|
| 570 |
\item[\texttt{\textbackslash{}splice}] |
| 571 |
Shell commands passed to the \verb+\splice+ |
| 572 |
macro are executed in a similar fashion to |
| 573 |
commands enclosed between \verb+\bash+ |
| 574 |
and \verb+\END+, but, in addition to this execution, |
| 575 |
\bashful incorporates the standard output into the main file. |
| 576 |
For example, I can write |
| 577 |
\begin{quote} |
| 578 |
\begin{verbatim} |
| 579 |
Here is a nice quote for you to remember. |
| 580 |
\begin{quote} |
| 581 |
\emph{\splice{fortune}} |
| 582 |
\end{quote} |
| 583 |
\end{verbatim} |
| 584 |
\end{quote} |
| 585 |
To obtain |
| 586 |
\begin{quote} |
| 587 |
Here is a nice quote for you to remember. |
| 588 |
\begin{quote} |
| 589 |
\emph{\splice{fortune}} |
| 590 |
\end{quote} |
| 591 |
\end{quote} |
| 592 |
|
| 593 |
Unlike the \verb+\bash+\ldots\verb+\END+, |
| 594 |
\verb+\splice+ does not treat its argument |
| 595 |
as if it was \texttt{verbatim}. |
| 596 |
Using special characters can therefore be |
| 597 |
tricky with \verb+\splice+. |
| 598 |
On the positive side, macro expansion within |
| 599 |
this argument can be handy. |
| 600 |
\end{description} |
| 601 |
|
| 602 |
\bash |
| 603 |
cat 00.tex |\ |
| 604 |
tr -c "a-zA-Z\\\\" "\012" |\ |
| 605 |
tr "\\\\" "@" | |
| 606 |
sed "s/@/ @/g" |\ |
| 607 |
tr " " "\012" |\ |
| 608 |
sed "/^@$/d" |\ |
| 609 |
grep @ | sort |\ |
| 610 |
uniq -c |\ |
| 611 |
sort -n |\ |
| 612 |
awk '{print$1}' | uniq -c |
| 613 |
\END |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
\section{Customization} |
| 621 |
|
| 622 |
\newcommand\option[3]{% |
| 623 |
\noindent\( |
| 624 |
\text{\bfseries\texttt{#1}} |
| 625 |
= |
| 626 |
\langle\text{{#2}}\rangle |
| 627 |
\) |
| 628 |
\hfill\texttt{#3}\\} |
| 629 |
\subsection{Package Options} |
| 630 |
Options to the \verb+\bashful+ package passed using the \textsf{xkeyval} syntax: |
| 631 |
|
| 632 |
\option{hide}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 633 |
If \texttt{true}, scripts are |
| 634 |
executed in a designated directory; |
| 635 |
if \texttt{false}, scrips are executed |
| 636 |
in the current working directory. |
| 637 |
|
| 638 |
\option{dir}{\sl directoryName}{} |
| 639 |
If \texttt{hide} option is \texttt{true}, then |
| 640 |
scripts are executed in this directory. |
| 641 |
Initial value of this options is \verb+_00+. |
| 642 |
Note that if you use \TeX{}live 2010, you have to configure certain |
| 643 |
security flags to make it possible to write to directories |
| 644 |
whose name start with a dot, or to directories |
| 645 |
which are not below the current working directory. |
| 646 |
|
| 647 |
\option{verbose}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 648 |
If \texttt{true}, be chatty. |
| 649 |
|
| 650 |
\option{unique}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 651 |
If \texttt{true}, then \bashful uses |
| 652 |
unique names for the files it generates in each |
| 653 |
invocation of the \verb+\bash+ command: \textsf{XX}\texttt{.sh}, |
| 654 |
\textsf{XX}\texttt{.stdout}, \textsf{XX}\texttt{.stderr} and |
| 655 |
\textsf{XX}\texttt{.exitCode}. |
| 656 |
These names then follow the pattern |
| 657 |
\textsf{JOB}\texttt{@}\textsf{LINE}\texttt{.}\textsf{EXTENSION}, |
| 658 |
where \textsf{JOB} is the job's name (i.e., \verb+\jobname+), |
| 659 |
\textsf{LINE} is the number of the line in the input file in |
| 660 |
which the \verb+\bash+ command was invoked, and |
| 661 |
\textsf{EXTENSION} is one of ``\texttt{sh}'', |
| 662 |
``\texttt{stdout}", ``\texttt{stderr}'' and |
| 663 |
``\texttt{exitCode}. |
| 664 |
|
| 665 |
If \texttt{false}, then these files follow the pattern |
| 666 |
\textsf{JOB}\texttt{.}\textsf{EXTENSION}. |
| 667 |
|
| 668 |
You should use this option your input invokes |
| 669 |
\verb+\bash+ more than once. |
| 670 |
|
| 671 |
\option{dir}{\sl directoryName}{} |
| 672 |
If \texttt{hide} option is \texttt{true}, then |
| 673 |
scripts are executed in this directory. |
| 674 |
Initial value of this options is \verb+_00+. |
| 675 |
Note that if you use \TeX{}live, you have to configure certain |
| 676 |
security flags to make it possible to write to directories |
| 677 |
whose name start with a dot, or to directories |
| 678 |
which are not below the current working directory. |
| 679 |
|
| 680 |
\subsection{Command Options} |
| 681 |
|
| 682 |
Options to \verb+\bash+ command |
| 683 |
are passed using the \textsf{xkeyval} syntax: |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
\subsubsection{File names} |
| 689 |
\option{scriptFile}{\sl fileName}{\textbackslash jobname.sh} |
| 690 |
Name of file into which the script instructions are spilled prior |
| 691 |
to execution. |
| 692 |
The default is \verb+\jobname.sh+; this file |
| 693 |
will be reused by all \verb+\bash+ commands in your documents. |
| 694 |
This is rarely a problem, since these scripts |
| 695 |
execute sequentially. |
| 696 |
|
| 697 |
\option{stdoutFile}{\sl fileName}{\textbackslash jobname.stdout} |
| 698 |
Name of file into which the shell standard output stream is |
| 699 |
redirected. |
| 700 |
|
| 701 |
|
| 702 |
\option{stderrFile}{\sl fileName}{\textbackslash jobname.stderr} |
| 703 |
Name of file into which the shell standard error stream is |
| 704 |
redirected. |
| 705 |
|
| 706 |
\option{exitCodeFile}{\sl fileName}{\textbackslash jobname.stderr} |
| 707 |
Name of file into which the shell standard error stream is |
| 708 |
redirected. |
| 709 |
|
| 710 |
\subsubsection{Listing Structure} |
| 711 |
\option{script}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 712 |
If \texttt{true}, the content of \texttt{scriptFile} |
| 713 |
is listed in the main document. |
| 714 |
|
| 715 |
\option{stdout}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 716 |
If \texttt{true}, the content of \texttt{stdoutFile} |
| 717 |
is listed in the main document. |
| 718 |
If both \texttt{script} and \texttt{stdout} are |
| 719 |
\texttt{true}, then \texttt{scriptFile} is listed |
| 720 |
first, and leaving no vertical space, |
| 721 |
\texttt{stdoutFile} is listed next. |
| 722 |
|
| 723 |
\option{stderr}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 724 |
If \texttt{true}, the content of \texttt{stderrFile} |
| 725 |
is listed in the main document, following |
| 726 |
\texttt{scriptFile} (if \texttt{script} is |
| 727 |
\texttt{true}) |
| 728 |
and |
| 729 |
\texttt{stdoutFile} (if \texttt{stdout} is |
| 730 |
\texttt{true}). |
| 731 |
|
| 732 |
\subsubsection{Tolerance to Errors} |
| 733 |
\option{ignoreExitCode}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 734 |
When |
| 735 |
\texttt{true} \verb+\bash+ will consider |
| 736 |
an execution correct even if its exit code |
| 737 |
is not 0. |
| 738 |
|
| 739 |
\option{ignoreStderr}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 740 |
When \texttt{true} \verb+\bash+ will consider |
| 741 |
an execution correct even if produces |
| 742 |
output to the standard error stream. |
| 743 |
|
| 744 |
\subsubsection{Appearance} |
| 745 |
|
| 746 |
\option{prefix}{tokens}{\percentchar\textvisiblespace} |
| 747 |
String that prefixes the listing of \texttt{scriptFile}. |
| 748 |
|
| 749 |
\option{environment}{enrionmentName}{none} |
| 750 |
Name of \LaTeX{} environment (e.g., \texttt{quote}) |
| 751 |
in which the listing is wrapped. |
| 752 |
|
| 753 |
\subsubsection{Miscellaneous} |
| 754 |
\option{verbose}{\texttt{true}/\texttt{false}}{\texttt{false}} |
| 755 |
If \texttt{true}, the package logs every step it takes. |
| 756 |
|
| 757 |
\subsection{Listings Styles} |
| 758 |
Package |
| 759 |
\href |
| 760 |
{ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf} |
| 761 |
{\textsf{listing}} |
| 762 |
is used for all listing both the executed shell |
| 763 |
commands and their output. |
| 764 |
\subsubsection{Listings Style for Script File} |
| 765 |
|
| 766 |
Style \verb+bashfulScript+ is used for displaying the executed shell |
| 767 |
commands (when option \texttt{script} is used). |
| 768 |
The current definition of this style is: |
| 769 |
\begin{verbatim} |
| 770 |
\lstdefinestyle{bashfulScript}{ |
| 771 |
basicstyle=\ttfamily, |
| 772 |
keywords={}, |
| 773 |
showstringspaces=false} |
| 774 |
|
| 775 |
|
| 776 |
\end{verbatim} |
| 777 |
Redefine this style to match your needs. |
| 778 |
|
| 779 |
\subsubsection{Listings Style for Standard Output} |
| 780 |
|
| 781 |
Style \verb+bashfulStdout+ is used for displaying the output of the |
| 782 |
executed shell |
| 783 |
commands (when option \texttt{stdout} is used). |
| 784 |
The current definition is: |
| 785 |
\begin{verbatim} |
| 786 |
% listings style for the stdoutFile, can be redefined by client |
| 787 |
\lstdefinestyle{bashfulStdout}{ |
| 788 |
basicstyle=\sl\ttfamily, |
| 789 |
keywords={}, |
| 790 |
showstringspaces=false |
| 791 |
}% |
| 792 |
|
| 793 |
\end{verbatim} |
| 794 |
Redefine this style to match your needs. |
| 795 |
|
| 796 |
|
| 797 |
Style \verb+bashfulStderr+ is used for displaying the output of the |
| 798 |
executed shell |
| 799 |
commands (when option \texttt{stderr} is used). |
| 800 |
\begin{verbatim} |
| 801 |
\lstdefinestyle{bashfulStderr}{ |
| 802 |
basicstyle=\sl\ttfamily\color{red}, |
| 803 |
keywords={}, |
| 804 |
showstringspaces=false |
| 805 |
} |
| 806 |
\end{verbatim} |
| 807 |
Redefine this style to match your needs. |
| 808 |
|
| 809 |
\section{Interaction with Other Packages} |
| 810 |
This packages tries to work around a bug in \texttt{polyglossia} |
| 811 |
by which \verb+\texttt+ is garbled upon |
| 812 |
switching to languages which do not use the Latin alphabet. |
| 813 |
Also, in case bidirectional \TeX{}ing is in effect, |
| 814 |
\bashful forces the listing to be left-to-right. |
| 815 |
|
| 816 |
|
| 817 |
\section{History} |
| 818 |
\begin{description} |
| 819 |
\item[Version 0.91] Initial release. |
| 820 |
\item[Version 0.92] |
| 821 |
\begin{itemize} |
| 822 |
\item Added \texttt{ignoreExitCode}, |
| 823 |
\texttt{ignoreStderr}, \texttt{stderr}, |
| 824 |
\texttt{exitCodeFile} command options. |
| 825 |
|
| 826 |
\item |
| 827 |
Renamed \texttt{list} to \texttt{script}. |
| 828 |
\item |
| 829 |
Added \texttt{hide} and \texttt{dir} package options. |
| 830 |
\end{itemize} |
| 831 |
\item[Version 0.93] |
| 832 |
\begin{itemize} |
| 833 |
\item Added the \texttt{unique} package flag. |
| 834 |
\item Added the \verb+\splice+, \verb+\bashStdout+ and \verb+\bashStderr+ |
| 835 |
commands. |
| 836 |
\item Enclosed in the packaging the Prac\TeX{} article |
| 837 |
source and \texttt{.pdf} file. |
| 838 |
\end{itemize} |
| 839 |
\end{description} |
| 840 |
|
| 841 |
\section{Future} |
| 842 |
The following may get implemented some day. |
| 843 |
|
| 844 |
\begin{enumerate} |
| 845 |
\item \emph{A \texttt{clean} option.} This option |
| 846 |
will automatically erase files |
| 847 |
generated for storing the script, and its standard |
| 848 |
output and standard error streams. |
| 849 |
|
| 850 |
\item \emph{A \texttt{noclobber} option.} This option |
| 851 |
will make this package safer, by reducing the risk |
| 852 |
of accidentally erasing existing files. |
| 853 |
\end{enumerate} |
| 854 |
|
| 855 |
\section{Acknowledgments} |
| 856 |
The manner by which \verb+\bash+ |
| 857 |
collects its arguments is based on that of |
| 858 |
\href |
| 859 |
{http://www.tn-home.de/Tobias/Soft/TeX/tobiShell.pdf} |
| 860 |
{\textsf{tobiShell}}. |
| 861 |
Martin Scharrer tips on \TeX{} internals |
| 862 |
were invaluable. |
| 863 |
I pay tribute to the insight and encouragement offered by Francisco Reinaldo |
| 864 |
which lead to the Prac\TeX{} journal publication entitled |
| 865 |
\emph{Bashful Writing and Active Documents} that describes |
| 866 |
sophisticated applications of this package. |
| 867 |
|
| 868 |
|
| 869 |
\appendix |
| 870 |
\section{Source of \texttt{\jobname.sty}} |
| 871 |
\lstinputlisting |
| 872 |
[ style=input, |
| 873 |
basicstyle=\scriptsize\ttfamily, |
| 874 |
numbers=left, |
| 875 |
stepnumber=10, |
| 876 |
firstnumber=1, |
| 877 |
numberfirstline=true, |
| 878 |
numberstyle=\scriptsize\rmfamily\bfseries |
| 879 |
] |
| 880 |
{\jobname.sty} |
| 881 |
|
| 882 |
\section{Source of \texttt{\jobname.tex}} |
| 883 |
\lstinputlisting |
| 884 |
[ style=input, |
| 885 |
basicstyle=\scriptsize\ttfamily, |
| 886 |
numbers=left, |
| 887 |
stepnumber=10, |
| 888 |
firstnumber=1, |
| 889 |
numberfirstline=true, |
| 890 |
numberstyle=\scriptsize\rmfamily\bfseries |
| 891 |
] |
| 892 |
{\jobname.tex} |
| 893 |
\end{document} |