我正在编写一个 Bash 脚本,该脚本将获取一组目录,遍历它,在每个目录中创建一个名为“已处理”的目录,并对目录中的每个文件运行一个命令。这是我的代码(阅读代码中的注释以了解我坚持的内容)。有任何想法吗?
#!/bin/bash
command -v convert >/dev/null 2>&1 || {
echo >&2 "Convert is not installed. Aborting.";
exit 1;
}
declare -a directories_to_process=(
"$HOME/Desktop/Album 1"
"$HOME/Desktop/Album 2"
"$HOME/Desktop/Album 3"
);
for directory in "${directories_to_process[@]}"
do
if [ -d "$directory" ]; then
if [ ! -d "$directory/processed" ]; then
mkdir "$directory/processed"
fi
# Insert code to run the following command on each file in $directory:
#
# convert $directory/$filename -resize 108x108^ -gravity center -extent 108x108 $directory/processed/$filename
fi
done
更新:
这是工作脚本:
#!/bin/bash
command -v convert >/dev/null 2>&1 || {
echo >&2 "Convert is not installed. Aborting.";
exit 1;
}
directories_to_process=(
"$HOME/Desktop/Album 1"
"$HOME/Desktop/Album 2"
"$HOME/Desktop/Album 3"
);
for directory in "${directories_to_process[@]}"
do
[[ -d $directory ]] || continue
mkdir -p "$directory/processed"
for infile in "$directory"/*.jpg
do
outfile="$directory/processed/${infile##*/}"
convert "$infile" \
-resize '108x108^' \
-gravity center \
-extent 108x108 \
"$outfile"
done
done