Ksh

在 ksh 中檢查文件名時出現文件權限錯誤

  • April 17, 2019

我正在嘗試創建一個校驗和文件。該文件將包含工作目錄中的文件列表,為每個文件創建 cksum,並刪除文件列表及其 cksum 為 MD5_<>.txt 以及文件。

我的程式碼如下:

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

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

"Permission Denied"出於某種原因,儘管創建文件的使用者是同一使用者,但if 行卻給了我錯誤。我懷疑這$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_&lt;&lt;Process Date&gt;&gt;.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&gt;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&gt;&-

引用自:https://unix.stackexchange.com/questions/475600