views:

9

answers:

1

I am trying to write a shell script that compiles a latex source. I wish to grab the name of the bibliography file which is contained in a command like:

\bibliography{filename}

I need to save "filename" to a shell variable. My solution to this (in tcsh) is dreadfully embarrassing:

set bioliography=grep -v -E "[[:blank:]]*%[[:blank:]]*" poltheory.tex | grep -E "\\bibliography{[A-Za-z0-9_\.]*}" | tail -1 | sed 's/\\bibliography//' | tr -d { | tr -d } | awk '{print $1}'

This breaks down as:

  1. do not show commented lines in the latex source
  2. grab those lines containing a valid bibliography tag
  3. use only the last one (in case for some reason multiple ones are defined)
  4. get rid of the curly braces
  5. set what is left to the shell variable.

Surely there's an elegant way of doing this that I'm overlooking. Can you wow me? I already have a working command, so this is merely in the name of beauty and shell wizardry.

+1  A: 

Rule 1: always write shell scripts using Bourne family shells, not csh family shells.

It's easier to pull the info from the .aux file. Here I'll use an extra feature of bash to chop the .tex off the end of the filename:

#!/bin/sh
texfile="$1"
auxfile="${texfile%.tex}.aux"

grep '^.bibdata{' "$auxfile" | sed 's/.*{//;s/}.*//'
Norman Ramsey
Interesting. I'm about to leave my office so I won't try it now but soon. Good idea. In years, I've never even examined the aux file. It's been several years since I've read "Csh Programming Considered Harmful" but I also remember reading a few things saying that tcsh programming is not as bad as the article suggests.
Dr. Person Person II