Damien ([info]thedigitalghost) wrote in [info]shell_scripting,
  • Mood: determined

Unix KSH help?

Anyone wanna write a KSH script for me to help my job become a lot easier?

I have this thing I have to do in Unix. It's easy - but very tedious. I have to do an 'sdiff' on two files and e-mail the results to myself for documentation purposes. But I'm not UNIX-savvy (yet) and even less KSH savvy. (I just learned what KSH stood for today!)

Here's what I need. I need a script that will do the following. This is what I have to type today:

sdiff really_long_program_name_here.ext /directory/where/other/file/is/really_long_program_name_here.ext | mailx -s "realy_long_program_name_here.ext" mye-mail@myaddress.com

Yeah - I have to type that for every file that gets changed. There are 700 programs in the current product that I have to maintain. Nice.

Here's what I'd like to type:

supc file1 file2 file3 filen -f folder

Where:

  • supc is the name of the script to be executed
  • file1 is the first set of files to be compared
  • file2 is the second set (and so on)
  • -f folder is the folder where the second file I'm comparing against lives
So this script would compare file1 in the current folder against file1 in the folder specified by -f folder and e-mail the results to me. Then it would compare file2 in the current folder against file2 in the folder specified by -f folder and e-mail the results to me and so on until it ran out of files from the command.

Make sense?

This would REALLY make my life easier.

D

  • Post a new comment

    Error

  • 5 comments

[info]trevoke

November 3 2005, 20:18:26 UTC 6 years ago

How about just ... diff ?

grep directory
--from-file=FILE1 Compare FILE1 to all operands. FILE1 can be a directory.
--to-file=FILE2 Compare all operands to FILE2. FILE2 can be a directory.


something like diff > file | mail -s .... ?

[info]trevoke

November 3 2005, 20:18:46 UTC 6 years ago

oops :)

$ diff --help | grep directory
--from-file=FILE1 Compare FILE1 to all operands. FILE1 can be a directory.
--to-file=FILE2 Compare all operands to FILE2. FILE2 can be a direct

[info]footpad

November 3 2005, 20:48:26 UTC 6 years ago

I totally agree! (Well, not necessarily with the diff—[info]thedigitalghost does want sdiff, which doesn't appear to have a recursive option.) Who in their right mind is going to read 700 automatically-generated emails?

Of course, [info]thedigitalghost, I'm only speculating about the context, but I am sure that your problem has been solved elsewhere. Any even vaguely sensible code-management system will let you browse changes in an infinitely more flexible, readable and useful way. If you really need this script then I've a shrewd suspicion that you don't need it at all; you need to read up on CVS, and two-by-four your developers until they start using it.

Having said that... here's a roughed-out version. I haven't bothered to test it (bedtime) so you'll need to play with it a bit, but it should be close to the mark. Discussions welcome, especially if people have suggestions of better ways to do things.

[info]footpad

November 3 2005, 20:53:09 UTC 6 years ago

#!/bin/ksh

die () {
    echo "$(basename $0): fatal error: $*" >&2 
    exit 1
}

#
# Standard argument-parsing rubric
#
while getopts "e:f:" opt "$@"
do
    case $opt in
        e) email="$OPTARG" ;;
        f) dir="$OPTARG" ;;
        *) die "bad option: '-$opt'" >&2 ;;
    esac
done
shift $((OPTIND - 1))

#
# Check that the options are sensible
#
[[ -d $dir ]] || die "directory argument needed: -f '${dir:-}'"
[[ -n $email ]] || die "email address needed: -e '${email:-}'"

for file1 in "$@"
do
    #
    # work out where the target file should be
    #
    file2="$dir/$(basename $file1)"
    #
    # check that the source and target file both exist
    # if not, move on to the next file
    #
    for f in "$file1" "$file2"
    do
        if [[ ! -f $f || ! -r $f ]]
        then
            echo "can't read file: '$f'" >&2
            continue
        fi
    done

    #
    # diff and mail!
    #
    # You almost certainly want something that skips
    # identical files!
    #
    sdiff "$file1" "$file2" | mail -s "sdiff result: $file1" "$email"
done

[info]beldin

November 4 2005, 01:54:11 UTC 6 years ago

It doesn't even necessarily need to start as a behavioral change for the developers; supc could be rewritten as a wrapper over CVS, with the target specified by -f referencing a CVS checkout directory. Running the script would iterate across the argument list, copying files into the CVS checkout directory, doing mkdirs and 'cvs add' of files and directories as appropriate.

There would be no need for the emailed sdiff; all that would be necessary is to install cvsweb on top of the base CVS installation.
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…