Я пытаюсь запустить скрипт, который будет ООН rar вещи и добавлять файл для установки флага файла, как "обработано", но это выдающий исключения как каталог, не найденный и я не уверен почему.
#!/bin/bash
function recursive {
if [ -d "$1" ]; then
for dir in "$1"; do
if [ -f "$dir"*rar ]; then
for file in $dir*.rar; do
echo $file
if [ ! -f "$dir$file.processed" ]; then
unrar e "$dir$file.rar" "$dir"
touch "$dir$file.processed"
fi
done
fi
echo $dir
subs= find $dir -maxdepth 1 -type d=
if [ "$subs" != "0" ] && [ "$subs" != "No such file or directory" ]; then
recursive "$dir*/"
fi
done
fi
}
recursive /home/user/Complete/*/
Этот скрипт основан на вашем ответе, но он использует find
вместо того, чтобы проходить через файловую систему вручную.
#!/bin/bash
unrar_if_needed()(
file="$1"
dir="$(dirname "$file")" # Extract dir name from file path.
echo "Found: $file"
if [[ -f "$file".processed ]]; then # Double brackets are better.
echo " Already unrared."
else
echo -n " Unraring... "
if unrar -inul e "$file" "$dir"; then # This if-statement is not necessary, but a nice touch.
touch "$file".processed
echo "Done."
fi
fi
)
export -f unrar_if_needed
if [[ "$1" ]]; then
dir="$1"
else
dir=.
fi
echo "Looking in: $dir"
find "$dir" -iname "*.rar" -exec bash -c 'unrar_if_needed {}' \; # find runs the custom function.
Вывод должен выглядеть следующим образом:
$ script.sh somedir
Looking in: somedir
Found: ./somedir/file1.rar
Unraring... Done.
Found: ./somedir/file2.rar
Already unrared.
#!/bin/bash
function recursive {
echo "$1"
for dir in "$1"*/; do
if [ -d "$dir" ]; then
echo "Reading Directory for rars: $dir"
for file in "$dir"*.rar; do
echo "Found Rar: $file"
if [ ! -f "$dir"processed ] && [ ! -f "$file".processed ]; then
echo "File not unrared. Unraring File."
unrar e "$file" "$dir"
touch "$dir"processed
echo "File Processed."
else
echo "File already Unrared."
fi
done
echo "......Going Deeper......"
recursive "$dir"
fi
done
}
recursive /home/user/Complete/