私はこの質問がさまざまな方法で尋ねられたことを知っています、そして私は私が今いるところにたどり着くためにここでいくつかの答えを参照しました。
書き込みが行われなくなったら、基本的にフォルダとウイルススキャンファイルを監視するスクリプトを作成しようとしています。
処理する必要のあるファイル/文字列には、スペースが含まれる場合と含まれない場合があり、特殊文字が含まれる場合もあります。現時点では、名前にスペースが含まれているファイル(実際にスキャンして移動する場合など)でのみ機能しますが、スペースが含まれていないファイルでは機能しません。また、各ファイル(スペースの有無にかかわらず)の後で、whileループが中断し、次の出力でスクリプトが停止します。
./howscan.sh: line 29: snmp.conf: syntax error: invalid arithmetic operator (error token is ".conf")
./howscan.sh: line 34: snmp.conf: syntax error: invalid arithmetic operator (error token is ".conf")
スペースなしでファイル名を処理するように機能させましたが"${files[$i]}"
、配列要素を使用する方法を導入したため、スペース付きのファイルでのみ機能し、上記のエラーを出力します。
これのsepscan部分は、他のタスクでも機能することができると確信しているので、自由に省略してください(完全に理解するために完全なスクリプトを表示したかっただけです)。
現在のスクリプト:
#!/bin/bash
set -x
workingpath='/mnt/Incoming/ToScan/'
outputpath='/mnt/Incoming/Scanned/'
logfile='/var/log/howscan.log'
faildir='/mnt/Incoming/ScanFailed/'
sepscan='/opt/Symantec/symantec_antivirus/sav manualscan -c'
# Change to working directory
cd $workingpath
# Exclude files with a given extension, in this case .aspx, and declare the remaining files as the array "files"
shopt -s extglob nullglob
# Loop the below - ie it's a watch folder
while true
do
# Exclude files with .aspx in the file name
files=( !(*.aspx) )
# If the array files is not empty then...
if [ ${#files[@]} -ne 0 ]; then
for i in ${files[*]}
# For every item in the array files process them as follows
# Declare any variables you wish to happen on files here, not globally
# Check each file to see if it's in use using fuser, do nothing and log it if its still in use, or process it and log the results
do
fileopen=`fuser "${files[$i]}" | wc -c`
# Here for 'fileopen' we are checking if the file is being writen to.
if [ $fileopen -ne 0 ]; then
echo `date` "${files[$i]}" is being used so has been ignored >> $logfile
else
echo `date` File "${files[$i]}" not being used or accessed >> $logfile
sepscan=`$sepscan "${files[$i]}" | wc -c`
if [ $sepscan = 0 ]; then
mv "${files[$i]}" $outputpath
echo `date` "${files[$i]}" has been virus scanned and moved to $outputpath >> $logfile
else
echo `date` "${files[$i]}" has been moved to $faildir as a virus or error was detected >> $logfile
fi
fi
done
fi
echo `date` 'No more files to process. Waiting 60 seconds...' >> $logfile
sleep 60
done
問題を明確にするために他に何か提供できるかどうか教えてください。
更新:
ちなみに、/ mnt / Incoming / ToScan /ディレクトリにsnmp.confというファイルがあります。
for i in ${files[*]}
する必要があります
for i in ${!files[*]}
# or
for i in ${!files[@]}
${files[*]}
配列の内容に展開され、単語分割が行われます。上記の構文は、配列のインデックスのリストに展開されます。
変数を二重引用符で囲む必要がある場合もあります。
if [ "$fileopen" -ne 0 ]; then
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加