したがって、ディレクトリ内のすべてのファイルを調べて、これらのファイルに同じディレクトリ内の他のファイルと同じ内容が含まれていることを確認するスクリプトがあります。
Check()
{
if [ -e "new.txt" ]
then
rm new.txt
fi
for item in "$2"/*
do
if [ ! -d "$item" ]
then
diff "$1" "$item">new.txt
if [ -s "new.txt" ]
then
echo "$1 $item"
echo "Yes"
fi
fi
done
}
Iterate()
{
for item in "$2"/*
do
if [ ! -d "$item" ]
then
Check $item $2
fi
done
}
Iterate $1 $2
そしてカーニバル
bash script.sh asd /home/ljuben
ただし、スクリプトの実行時にファイルに同じ内容が含まれていない場合は、常に「yes」と表示されます。
そしてアイデア?
ベストアンサー1
あなたのスクリプトが最初の引数を使用していないようです。Iterate
関数に渡された後、再び表示されません。
しかし、実際の質問は、diff
両方のファイルのすべての組み合わせを実行し、違いのサイズを確認することです。他のファイルの場合、差のサイズはゼロではありません。したがって、スクリプトはYes
各ファイルの組み合わせを報告します。その他、ない同じです。
A
また、ファイルを不必要に実行し、B
その違いを2回実行しています(A
vs. そして then vs. B
later )。ファイルの一覧を一度だけ作成してから繰り返して、この問題を解決できます。B
A
代替スクリプト:
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directory\n' "$0" >&2
exit 1
fi
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# skip over non-regular files
if [ ! -f "$1" ]; then
shift
continue
fi
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same content\n' "$item" "$name"
fi
done
done
必要に応じて両方の機能を使用できます。
#!/bin/sh
if [ ! -d "$1" ]; then
printf 'Usage: %s directory\n' "$0" >&2
exit 1
fi
check () {
# now process the first item in the list against the other ones
item=$1
# shift off the first item from the list
shift
# loop over the remaining items...
for name do
# we're still not interested in non-regular files
[ ! -f "$name" ] && continue
# if they are the same, report this
if cmp -s "$item" "$name"; then
printf '%s and %s has same content\n' "$item" "$name"
fi
done
}
iterate () {
# set positional parameters to the list of files in the given directory
set -- "$1"/*
# while there's still files to process...
while [ "$#" -gt 0 ]; do
# only process regular files
if [ -f "$1" ]; then
check "$@" # checks the first item against the rest
fi
shift # get rid of the first item
done
}
iterate "$1"
check
関数が独自のファイルリストを生成しないようにする方法に注意してください。代わりにファイルのリストを渡します。
怠惰な人のために:
fdupes -1 /some/directory