フォルダに使用するコマンドが不明

フォルダに使用するコマンドが不明

そのため、画像(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]#

おすすめ記事