2

So I'm making a batch script to convert images (png, jpg) and resize (in pixels and percentage) them. I'm working with dialog and imagemagick. My problem is that I can't find any command/code to when a user chooses a specific folder, the action on that chosen folder will be executed. (Very new at this)

#!/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
2

You could check if the argument is a file or a directory, and then build a list of files to treat and loop over them.

#!/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

Outputs:

[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]#
  • Thank you very much. Your comment was very insightful! :) – Mizugorou Dec 28 '17 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.