Question

To process a bunch of data and get it ready to be inserted into our database, we generate a bunch of shell scripts. Each of them has about 15 lines, one for each table that the data is going. One a recent import batch, some of the import files failed going into one particular table. So, I have a bunch of shell scripts (about 600) where I need to comment out the first 7 lines, then rerun the file. There are about 6000 shell scripts in this folder, and nothing about a particular file can tell me if it needs the edit. I've got a list of which files that I pulled from the database output.

So how do I write a bash script (or anything else that would work better) to take this list of file names and for each of them, comment out the first 7 lines, and run the script?

EDIT:

#!/usr/bin/env sh

cmd1
cmd2
cmd3
cmd4
cmd5
cmd6
cmd7
cmd8

Not sure how readable that is. Basically, the first 7 lines (not counting the first line) need to have a # added to the beginning of them. Note: the files have been edited to make each line shorter and partially cut off copying out of VIM. But in the main part of each file, there is a line starting with echo, then a line starting with sqlldr

Was it helpful?

Solution

Using sed, you can specify a line number range in the file to be changed.

#!/bin/bash

while read line
do
    # add a comment to beginning of lines 1 - 7 and rename the script
    sed '3,9 s/^/#/' $line > $line.new
    exec $line.new
done < "filelist.txt"

You may wish to test this before running it on all of those scripts...

EDIT: changed the lines numbers to reflect comments.

OTHER TIPS

Roughly speaking:

#!/bin/sh
for file in "$@"
do
    out=/tmp/$file.$$
    sed '2,8s/^/#/' < $file > $out
    $SHELL $out
    rm -f $out
done

Assuming you don't care about checking for race conditions etc.

ex seems made for what you want to do.

For instance, for editing one file, with a here document:

#!/bin/sh

ex test.txt << END
1,12s/^/#/
wq
END

That'll comment out the first 12 lines in "test.txt". For your example you could try "$FILE" or similar (including quotes!).

Then run them the usual way, i.e. ./"$FILE"

edit: $SHELL "$FILE" is probably a better approach to run them (from one of the above commenters).

Ultimately you're going to want to use the linux command sed. Whatever logic you need to place in the script, you know. But your script will ultimately call sed. http://lowfatlinux.com/linux-sed.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top