-1

I'm trying to create a checksum file. The file will have a list of files from the working directory, create cksums for each file, and drop the list of files with their cksums as MD5_<>.txt along with the files.

The code I have is as follows:

EXT=`date +%Y%m%d`
p_FP=${gp_LOAD_DIR}Client/

cd ${p_FP}
###################################################
# Create MD5_<<Process Date>>.txt File
###################################################
echo "MD5 CHECKSUM Roster for " ${EXT} > MD5_${EXT}.txt
echo "MD5        File Size     FileName" >> MD5_${EXT}.txt
for file in $p_FP*; do
     if $file != MD5_${EXT}.txt]; then
        echo cksum file >> MD5_${EXT}.txt
     fi
done

for some reason the if line is giving me "Permission Denied" errors in spite of the fact it is the same user who created the file. I suspect that $file is an object, not a text name of a file. how do I rectify this?

  • Actually, your if test should be between [[...]] as to not try to execute the $file. On the other hand, you should not "echo" to the file since you will end up with the literal "cksum file" in your file (by the way it should be $file). – Andre Gelinas Nov 1 '18 at 16:50
2

Just for fun, that's what I would do including some formatting to ease the reading of the result file.

    #!/bin/ksh

    # This is just for formatting

    typeset -L15 Col1
    typeset -L15 Col2
    typeset -L50 Col3

    # Shouldn't use backtick, it's more or less deprecated by now
    EXT=$(date +"%Y%m%d")
    p_FP=${gp_LOAD_DIR}Client/

    cd ${p_FP}
    ###################################################
    # Create MD5_<<Process Date>>.txt File
    ###################################################

    # Just for speed. Since you are already using cksum for each line, it could be faster to avoid reopening the result file each time also

    exec 3>MD5_${EXT}.txt

    # Using ksh, you should use "print" instead of echo since it's builtin instead of external (faster)
    print -u 3 "MD5 CHECKSUM Roster for " ${EXT}

    Col1="MD5"
    Col2="File Size"
    Col3="FileName"

    print -u 3 "$Col1$Col2$Col3"

    for file in $p_FP*; do
        # cksum won't work on directories nor on names pipe
         if [[ -f $file && ! -p $file && $file != MD5_${EXT}.txt ]]; then
            # Again, that's just for formatting
            line=( $(cksum $file) )
            Col1=${line[0]}
            Col2=${line[1]}
            Col3=${line[2]}
            print -u 3 "$Col1$Col2$Col3"
         fi
    done

    # Closing the file

    exec 3>&-
-1

You are calling the program $filewith the arguments !=and MD5_${EXT}.txt]

So the most probable reason is that $file is not executable (the x bit was not set).

If you like to test something, you should prepend $file with test and remove that probably stray ] before the semicolon.

  • the intent of that if statement is to just make sure I don't try to chksum the MD5 file itself. how do I ensure that? – arcee123 Oct 15 '18 at 15:37
  • OK, I edited my test to make the word "test" bold – schily Oct 15 '18 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.