![폴더에 사용할 명령을 알 수 없습니다](https://linux55.com/image/124435/%ED%8F%B4%EB%8D%94%EC%97%90%20%EC%82%AC%EC%9A%A9%ED%95%A0%20%EB%AA%85%EB%A0%B9%EC%9D%84%20%EC%95%8C%20%EC%88%98%20%EC%97%86%EC%8A%B5%EB%8B%88%EB%8B%A4.png)
그래서 이미지(png, jpg)를 변환하고 크기(픽셀 및 백분율)를 조정하는 배치 스크립트를 만들고 있습니다. 나는 대화 상자와 imagemagick을 사용하고 있습니다. 내 문제는 사용자가 특정 폴더를 선택할 때 선택한 폴더에서 작업을 수행하는 명령/코드를 찾을 수 없다는 것입니다. (이 분야에서는 매우 새로운)
#!/bin/bash
dialog --menu "Batch Image Converter/Resizer" 15 25 2 1 /convert 2 /resize 2>temp
if [ "$?" = "0" ]
then
_return=$(cat temp)
if [ "$_return" = "1" ]
then
let i=0
W=()
while read -r line;do
let i=$i+1
W+=($i "$line")
done < <(ls -Rp | grep "/$")
dialog --title "List" --menu "Choose" 24 80 17 "${W[@]}" 3>&1 1>&2 2>&1 2>choice
foldercount=$(ls -Rp | grep "/$" | wc -l)
for i in 1 2 3 4 5 6 7 8 9
do
dirs=($(find * -type d))
for DIR in "${dirs[@]}"; do
folderchoice=$(cat choice)
if [ "$" = "$i" ]
then
dialog --menu "Convert: " 20 30 10 1 png to jpg 2 jpg to png 2>optionformat
keuze=$(cat optionformat)
if [ "$choice" = "1" ]
then
cd $DIR ; mogrify -format png *.jpg
fi
if [ "$choice" = "2" ]
then
cd $DIR ; mogrify -format jpg *.png
fi
fi
done
done
fi
if [ "$_return" = "2" ]
then
let i= 0
W=()
while read -r line;do
let i=$i+1
W+=($i "$line")
done < <(ls -Rp | grep "/$")
FILE=$(dialog --title "List" --menu "Choose" 24 80 17 "${W[@]}" 3>&2 2>&1 1>&3)
clear
foldercount=$(ls -Rp | grep "/$" | wc -l)
choice=($cat 1 )
if [ "$choice" = "1" ]
then
dialog --menu "Resize in percentage or pixels: " 20 30 5 1 percentage 2 pixels 2>optionResizePixel
choice=$(cat optionResizePixel)
if [ $keuze = "1" ]
then
dialog --inputbox "Pixels (1 value for 4 sides (ex. 40): " 8 60 2>inputvalue
input=$(cat inputvalue)
cd /home/student/Pictures ; mogrify *.png -resize $input *.png
cd /home/student/Pictures ; mogrify *.jpg -resize $input *.jpg
fi
if [ $choice = "2" ]
then
dialog --inputbox "Percentage (input: ex. 50%): " 8 60
cd /home/student/Pictures ; mogrify *.jpg -resize $input *.jpg
cd /home/student/Pictures ; mogrify *.png -resize $input *.png
fi
fi
fi
# Cancel is pressed
else
echo "Cancel is pressed"
fi
답변1
인수가 파일인지 디렉터리인지 확인한 다음 파일 목록을 작성하여 처리하고 반복할 수 있습니다.
#!/bin/bash
# The script takes one arguments that must be a file or a directory
# Treating a file or all files in a directory
export FILES
if [[ -f "$1" ]]
then
echo "First argument is a file"
FILES=$1
elif [[ -d "$1" ]]
then
echo "First argument is a directory"
FILES=$(find "$1" -type f)
fi
for file in $FILES
do
echo "Treating file $file"
# do stuff here
done
산출:
[user@host test]# ls ~/test
tmp.1bmMi7hOsF tmp.7VFvw4nHNz tmp.ieLecqRusH tmp.o6Rfc2QQdb tmp.SQqRwtdro2 tmp.YahRzYY5mV tmp.ZPGgJPTMZJ
tmp.2KCyJr7lLr tmp.GNvgwYbkRO tmp.MsESiT8n1Q tmp.Q81MtVS97H tmp.WSlNZ4G6Uc tmp.yLwBItk2qX
[user@host test]# ~/test.sh ~/test/tmp.YahRzYY5mV
First argument is a file
Treating file /home/user/test/tmp.YahRzYY5mV
[user@host test]# ~/test.sh ~/test/
First argument is a directory
Treating file /home/user/test/tmp.7VFvw4nHNz
Treating file /home/user/test/tmp.MsESiT8n1Q
Treating file /home/user/test/tmp.WSlNZ4G6Uc
Treating file /home/user/test/tmp.o6Rfc2QQdb
Treating file /home/user/test/tmp.GNvgwYbkRO
Treating file /home/user/test/tmp.YahRzYY5mV
Treating file /home/user/test/tmp.1bmMi7hOsF
Treating file /home/user/test/tmp.ZPGgJPTMZJ
Treating file /home/user/test/tmp.Q81MtVS97H
Treating file /home/user/test/tmp.2KCyJr7lLr
Treating file /home/user/test/tmp.yLwBItk2qX
Treating file /home/user/test/tmp.ieLecqRusH
Treating file /home/user/test/tmp.SQqRwtdro2
[user@host test]#