The cksum Problem

Hi,

I have a working script, well it works on my machine but when I try it on others the cksum section does not work properly (I know the scripting is not of a high quality but I'm just trying to get it working)

Heres the script:

#!/bin/sh

case $# in
0) echo "usage: enshar filename filename2 ...";;

*) for file
do

if [ -d $file ]
then echo "usage: cannot shar directory"

elif [ ! -f $file ]
then echo "usage: file does not exist"

elif [ ! -r $file ]
then echo "usage: file is not readable"

else
echo "cat > $file <<\!EnShAr!"
cat $file
echo "!EnShAr!"
echo "set 'cksum $file'"
cksum $file > temp1
awk '/^/ {print $1}' temp1 > temp2
echo "test \$1 =" > temp1
more temp2 >> temp1
echo "|| echo \$0: BAD checksum in $file >&2" >> temp1
tr "\n" " " < temp1 > temp2
more temp2
echo
fi

done ;;

esac

My problem is: I need to get cksum to function properly, I need it to check the file before transfer, then after and display the number ( in some cases in just shows ::::::::: ), any help is very appreciated.

You still don't like using code tags.

#!/bin/sh

case $# in
    0) echo "usage: enshar filename filename2 ...";;

    *) for file
        do

            if [ -d $file ]
            then echo "usage: cannot shar directory"

            elif [ ! -f $file ]
            then echo "usage: file does not exist"

            elif [ ! -r $file ]
            then echo "usage: file is not readable"

            else
                echo "cat > $file <<\!EnShAr!"
                cat $file
                echo "!EnShAr!"
                echo "set 'cksum $file'"
                cksum $file > temp1
                awk '/^/ {print $1}' temp1 > temp2
                echo "test \$1 =" > temp1
                more temp2 >> temp1
                echo "|| echo \$0: BAD checksum in $file >&2" >> temp1
                tr "\n" " " < temp1 > temp2
                more temp2
                echo
            fi

    done ;;

esac

Adding the tags is very simple and is very much appreciated by everyone helping you debug your code.

I tried your code and I can't get any file to show ::::::::: . Can you show an example of this happening?

By the and for what it's worth, your code looks to me like it could be greatly simplified if you used variables instead of temporary files:

#! /bin/sh

case $# in
    0) echo "usage: enshar filename filename2 ...";;

    *) for file
       do
            if [ -d $file ]
            then continue ; echo "usage: cannot shar directory"

            elif [ ! -f $file ]
            then continue ; echo "usage: file does not exist"

            elif [ ! -r $file ]
            then continue ; echo "usage: file is not readable"

            else
...
                CHKSUM_1=$(cksum $file | awk '{print $1}')
                echo "test $file = ${CHKSUM_1}"
...
# Do your copy/transfer/whatever
...
                CHKSUM_2=$(cksum /new/location/$file | awk '{print $1}')
                echo "test $file = ${CHKSUM_2}"
                if [[ ${CHKSUM_1} != ${CHKSUM_2} ]]
                then
                     print "not the same"
                fi
            fi

       done ;;

esac

Replace your code :

                echo "cat > $file <<\!EnShAr!"
                cat $file
                echo "!EnShAr!"
                echo "set 'cksum $file'"
                cksum $file > temp1
                awk '/^/ {print $1}' temp1 > temp2
                echo "test \$1 =" > temp1
                more temp2 >> temp1
                echo "|| echo \$0: BAD checksum in $file >&2" >> temp1
                tr "\n" " " < temp1 > temp2
                more temp2
                echo

by (not tested) :

                CHKSUM=$(cksum $file | awk '{print $1}')
                echo "cat > $file <<\!EnShAr!"
                cat $file
                echo "!EnShAr!"
                echo "chksum=`cksum $file | awk '{print \$1}'`
                echo "test \$chksum = $CHKSUM || echo \$0: BAD checksum in $file >&2" 
                echo

Jean-Pierre.

I have tried this code and I am told there is asyntax error from the very first line, it says it is unexpected, could any one give me some help on this?

The fifth line in aigle's code is missing a ". Also, make sure you're using ksh. (#!/usr/bin/ksh).

can someone please give me the syntax for getting the $file to have a cksum run on it during a script (it is followed by text) please, i understand i can't use more so i need to use another method