无法弄清楚对文件夹使用什么命令

无法弄清楚对文件夹使用什么命令

所以我正在制作一个批处理脚本来转换图像(png,jpg)并调整它们的大小(以像素和百分比为单位)。我正在使用dialog 和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]#

相关内容