如何在 bash 腳本中對檔案名稱進行排毒而不發布錯誤?

如何在 bash 腳本中對檔案名稱進行排毒而不發布錯誤?

作業系統:Kubuntu 22.04.4 LTS x86_64
detox 1.4.5

要顯示在上面:

neofetch --stdout |grep 'OS:'
detox -V

這是一個有毒的檔名,以 s1 開頭:

s1 Ä Ö Ü - ä ö ü Science & < > " 1 \ 2 ⁄ 3 | ? * (&&9&&&) ::::z.pdf

script1已複製並貼上到終端中。 script1輸出適用於上述有毒檔案名稱"$FILE1"

filename_before_detox='s1 Ä Ö Ü - ä ö ü Science & < > " 1 \ 2 ⁄ 3 | ? * (&&9&&&) ::::z.pdf'
filename_after__detox= s1_A_A-Aoe-A_A_pp_A_Science_and_1_2_a_3-and_and_9_and_and_and-z.pdf

script1有效,給予結果,一個解毒的檔案名稱。沒有空格和特殊字元:請參閱下方所需的轉換、重新命名、檔案名稱:

s1_A_A-Aoe-A_A_pp_A_Science_and_1_2_a_3-and_and_9_and_and_and-z.pdf

script1:

clear 
DIR1=$HOME/Downloads    # DIR1=/home/x/Downloads 
cd $DIR1
ls -alF s1*    # List all filenames starting with s1
FILE1='s1 Ä Ö Ü - ä ö ü Science & < > " 1 \ 2 ⁄ 3 | ? * (&&9&&&) ::::z.pdf'
  detox -s iso8859_1    "$FILE1" 
# detox -s iso8859_1 -v "$FILE1" # v=verbose 
ls -alF s1*    # List all filenames starting with s1

script2不管用:

錯誤=沒有這樣的檔案或目錄

script2auto 檢測到新文件,DIR1 = ~/Downloads並且script2On AccessDIR1最終將
在測試期間運行 clamscan 來查找病毒,FILE1手動貼上到其中DIR1以模擬下載。

各種報價結果:

detox -s iso8859_1  "$FILE1"    # No such file or directory
detox -s iso8859_1 '"$FILE1"'   # No such file or directory
detox -s iso8859_1 ""$FILE1""   # posting errors then ok result 

script2

clear 
DIR1=$HOME/Downloads    # DIR1=/home/x/Downloads 
inotifywait -q -m -e close_write,moved_to --format '%w%f' "$DIR1" |while read FILE1
do 
ls -alF s1*             # List all filenames starting with s1
detox -s iso8859_1 ""$FILE1"" 
ls -alF s1*             # List all filenames starting with s1
done

script2有錯誤然後好的結果:

s1_A_A-Aoe-A_A_pp_A_Science_and_1_2_a_3-and_and_9_and_and_and-z.pdf

但也有很多錯誤:

rw-rw-r-- 1 x x 1153263 Mar 13 11:36 's1 Ä Ö Ü - ä ö ü Science & < > " 1 \ 2 ⁄ 3 | ? * (&&9&&&) ::::z.pdf'
/home/x/Downloads/s1: No such file or directory
Ä: No such file or directory
Ö: No such file or directory
Ü: No such file or directory
-: No such file or directory
ä: No such file or directory
ö: No such file or directory
ü: No such file or directory
Science: No such file or directory
&: No such file or directory
<: No such file or directory
>: No such file or directory
": No such file or directory
2: No such file or directory
⁄: No such file or directory
3: No such file or directory
|: No such file or directory
(&&9&&&): No such file or directory
::::z.pdf: No such file or directory
-rw-rw-r-- 1 x x 1153263 Mar 13 11:36 s1_A_A-Aoe-A_A_pp_A_Science_and_1_2_a_3-and_and_9_and_and_and-z.pdf
-rw-rw-r-- 1 x x  1153263 Mar 13 11:36 s1_A_A-Aoe-A_A_pp_A_Science_and_1_2_a_3-and_and_9_and_and_and-z.pdf
-rw-rw-r-- 1 x x 1153263 Mar 13 11:36 s1_A_A-Aoe-A_A_pp_A_Science_and_1_2_a_3-and_and_9_and_and_and-z.pdf

這是什麼detox意思?

  • detox= 清理檔案名稱
  • detox實用程式會重新命名檔案以使它們
    更易於使用。
  • 它消除了空格和其他此類煩惱。
  • 它還會翻譯或清理以 8 位元 ASCII 編碼的 Latin-1 (ISO 8859-1) 字元、以 UTF-8 編碼的 Unicode 字元以及 CGI 轉義字元。

inotifywait= 使用 inotify 等待文件更改

  • inotifywaitinotify(7)使用 Linux 的介面有效地等待檔案的變更。
  • 它適合等待 shell 腳本對檔案的變更。
  • 它可以在事件發生後退出,也可以在事件發生時持續執行和輸出事件。

如何在 bash 腳本中對檔案名稱進行排毒而不發布錯誤?

相關內容