Anybody good with bash scripting?

TyphoidHippo

1Trillion K Platinum User
I figure with all these members, there's gotta be at least one person who is good with bash scripting - whether or not I'll actually get their attention with this thread...I'm kinda doubtful - but it sure would be swell, since people here are generally much more friendly than the ones I could ask for help on *nix forums. :p

Basically, all I want to do is make a script that will read some variable definitions from one file and then go replace those variable instances in other files with said definitions from the first file.

Example:

***Definitions file***
definitions.src

%{APPNAME} SomeName
%{APPDESC} Some program that does something
%{AUTHOR} John Doe
%{DATE} 09-24-08

***File to be altered (before running the script)***
SomeName.cpp

//%{APPNAME}.cpp
//%{APPDESC}
//Created/revised by %{AUTHOR} on %{DATE}

***File to be altered (after running the script)***
SomeName.cpp

//SomeName.cpp
//Some program that does something
//Created/revised by John Doe on 09-24-08

Thanks in advance if anybody can help.
 
I don't know that I would solve this with bash - what you want is sed/awk, or perl.

Hey, thanks, man. I actually ended up just switching to Windows on my desktop because I have to use Windows-only apps (Visual Studio:rolleyes:, 3DS MAX) with their own proprietary formats for my classes now :mad:....and, as we all know, the built-in scripting in Windows is just awesome, lol. although... it is nice to be able to script with Auto-it again, as there is, sadly, nothing quite like it for Linux (and I looked, really hard, for something similar for years).

Anyway, I already scripted up a nice little Auto-it script that does it for me, but thanks anyway! :D
 
I wish you'd posted in the computer forum where I would have seen it. This would be about a 15 second script. I recommend that you not use all those weird symbols in the definition file, though. Just make the file be:

TERM value to insert
NAME John Doe
DATE June 9, 1969

I probably wouldn't do this with awk. It could probably do it, but I think you'd be working around its design quite a bit. A shell script with a lot of sed expressions would probably be easier, e.g. something like this:

#!/bin/sh

SOURCEFILE="$1"
DESTFILE="$2"
DEFSFILE="$3"

# Capture the definitions file and the source file into variables
DEFS="$(cat "$DEFSFILE")"
DATA="$(cat "$SOURCEFILE")"

# Process each definition in the definitions file.
while [ "x$DEFS" != "x" ] ; do

# First, split the content out one line at a time.
LINE="$(echo "$DEFS" | head -n 1)"
DEFS="$(echo "$DEFS" | tail -n "+2")"

# Grab the first word, splitting on the first space.
TOKEN="$(echo "$LINE" | sed 's/ .*//g')"

# Look for one or more nonspace characters followed by a space,
# anchored to the start of the line. Delete everything up to and including
# this space. In effect, this is the inverse of the previous line.
VALUE="$(echo "$LINE" | sed 's/^[^ ][^ ]* //')"

# If both the token and the value are nonempty, hack up the data
# with them.
if [ "x$TOKEN" != "x" -a "x$VALUE" ] ; then
DATA="$(echo "$DATA" | sed "s/$TOKEN/$VALUE/g")"
fi

# End of the while loop.
done

# Write the data out to the destination file.
echo "$DATA" > "$DESTFILE"


Note that this is completely untested and probably contains a fair number of typos, but you get the basic idea, anyway. I know it's too late to help you, but hopefully this post will at least help somebody trying to figure something out in the future.
 
Dang, nice, man! "I must spread some reputation around before giving it to Dgatwood again" :D

P.S. We have a computer forum?! General computing or the computer recording and soundcards forum? If the former, I must be pretty dense, because I can't find it. :(
 
Back
Top