up

Code

As a coder, when working independently I prefer to use the vim text-editor (Don't worry, I'm not here to start any tribal wars).

In order to facilitate calling various compilers and whatnot from vim I also tend to rely on bash scripts. I thought I'd share a few of them here. Keep in mind they're nothing spectacular, and if they existed for any other reason than personal use I'd make them more robust, but they might still be of interest to newer bash coders.

  1. pdflatex
  2. cpp-compile
  3. copyright update

pdflatex

January 12, 2017

I'm frequently writing LaTeX articles. When editing, I'm compiling and recompiling the source often to see what it looks like visually. I prefer to build pdf documents. Anyway, as it gets tedious to call each command directly, I wrote a script instead:

Image of pdflatex script
  1. This script is only meant to work with one ".tex" file in a directory.
  2. If the user offers a name, it's accepted, otherwise the single file from the directory is deduced.
  3. It is verified that exactly one latex file exists. If there's zero or more than one the script halts.
  4. I run pdflatex, then as I frequently use the asymptote programming language in conjunction (as a latex package), I check to see if any ".asy" files were generated. If they were, I compile them.
  5. I run pdflatex again. If I have hyperlink references embedded, or I need to update bibliographic info, or if I was in fact using asymptote, I would need to run pdflatex a second time anyway. If not, then I have run it a second time for nothing, which isn't great, but not the worst offense either. If it really mattered, it would be easy enough to modify the script to add the option to prevent double compiling.
  6. If everything compiled properly, a ".pdf" file will exist. If not, it won't. I can use this property of the pdflatex compiler to test whether or not I should run evince (the supported pdf viewer on my operating system) to view the output.

cpp-compile

January 12, 2017

As my main personal project is a C++ library, I'm frequently compiling and testing code. It gets tedious to call each command directly, so this is my shortform:

Image of cpp-compile script
  1. I get the working directory by removing the longest pathname prefix ending in "/".
  2. I then get the name the user wants for the compiled (executable) file. If the user does not supply a name, I use the working directory name as default.
  3. I call gcc or g++ depending on the source "main" file, printing the compiler output both to the screen and to an error file.
  4. Assuming the file compiled, I execute its code as well as return its basic runtime statistics.

copyright update

January 2, 2017

Every coder has their own copyright update script I'm sure, this is mine.

Image of copyright update script
  1. I call find with a regular expression matching C++ filename extensions returning the names (and paths) of C++ files.
  2. I then feed this list into the while loop construct where I modify the copyrights.
  3. I use a mawk script for the text substitutions themselves.
  4. The modified file is saved as tmp.txt, and then renamed to replace the original. As such, this script assumes none of the directories you're working in have a file called tmp.txt.

The nice thing about the commandline is it literally takes 2-3 seconds to update hundreds of text files :)