Register
It is currently Sun Feb 26, 2017 6:16 am

Bash script with options and resizing images


All times are UTC - 6 hours


Post new topic Reply to topic  [ 13 posts ] 
Author Message
 PostPosted: Mon Jan 09, 2017 3:34 am   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
Alright I'm trying to create a bash script that has options for users to choose from when it comes to image resizing. The script can be edited by the user to fit their needs. But it's not working & I can't figure out why.

Here's what I've got for my script:

Code:
#!/bin/bash

  DIR=/Yosemite/Users/Technologx/Downloads/New\ Carved\ Walls/previews/

   while [ “answer” != e ]; do #menu
      clear
   echo -e '[Hello $USER] what can I do for  you?\n'
      echo " 
          1   Resize jpg
          2   Resize png
          3   Resize gif
          4   Resize jpeg
         5   Exit”
   echo -e "\n--------------------------------------------------------------\n"
      read -p  "Sir ?  " answer
      case $answer in
   done

      1|rem|rm|RM|REM)
      echo -e “\n.. Resizing your jpg’s!\n”
      find $DIR -iname \*.jpg -exec sips -Z 150 250 {} \;
   done

      2|rem|rm|RM|REM)
      echo -e “\n.. Resizing your png’s!\n”
      find $DIR -iname \*.png -exec sips -Z 150 250 {} \;
   done

      3|rem|rm|RM|REM)
      echo -e “\n.. Resizing your gif’s!\n”
      find $DIR -iname \*.gif -exec sips -Z 150 250 {} \;
   done

      4|rem|rm|RM|REM)
      echo -e “\n.. Resizing your jpeg’s!\n”
      find $DIR -iname \*.jpeg -exec sips -Z 150 250 {} \;
   done

      5|rem|rm|RM|REM)
      echo -e “\n.. GoodBye\n”
      killall Terminal
   done

   echo -e “\n..Thank you for using my image resizer don’t forget to visit https://technologx.com and signup\n”

   sleep 5

   done


Last edited by Technologx on Tue Jan 10, 2017 4:34 am, edited 1 time in total.

Top
 Profile WWW  
 PostPosted: Mon Jan 09, 2017 11:30 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 524
use proper indenting.
This is unreadable.
Press "Full Editor" to use Code tags

Code:
while true; do
    read -p "gor it ? " answer
    case $answer in
          what|sorry   ) 
                  echo "case constructs must be written in proper syntax"
                  ;;
           explain  )
                  echo "each case must end with double \";;\""
                  echo "read \"help case\" to learn about this."
                  ;;
            gotit|yes      )
                  echo "and use code tags if posting code!"
                  break
                  ;;
            *     )
                  echo "can't get you. What do you mean? "
                  ;;   
    esac
done


Top
 Profile  
 PostPosted: Tue Jan 10, 2017 4:39 am   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
Alright I indented not sure if I did it correctly or not. It's been awhile since I made a script and I've never done a script that had a menu. What I'm wanting to do is have a menu of options for users to choose from. Each option will resize only 1 image extension type and no other file in the listed directory. I'm going to use this for Android Icon Pack devs like myself to use to be able to quickly resize their icons for their icon pack before submitting it to the play store. I'm not sure if it's possible but I'd like to have the option in the script for the user to enter the dimensions they want the images in the directory to be.


Top
 Profile WWW  
 PostPosted: Tue Jan 10, 2017 1:34 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 524
1 while ... do
and
thousands done?

And running a loop forever usually needs a break.
Or it will be running forever.
Not, what you want.

And you really want to read help case
And if you don't want do, read at least my previous post carefully.
Everything already said with code.

You still do not indent properly.
Still hard to read.

Code:
while true; do
    read -p "gor it ? " answer
    case $answer in
          what|sorry   )
                  echo "case constructs must be written in proper syntax"
                  ;;
           explain  )
                  echo "each case must end with double \";;\""
                  echo "read \"help case\" to learn about this."
                  read -p "some more? Y|n ""  explain_deeper
                  if [[ $explain_deeper == "Y" ]]; then
                      echo "the two \";;\" tell case, that one case is finished"
                  else
                      echo " then let's go on..."
                  fi
                  ;;
            gotit|yes      )
                  echo "and use code tags if posting code!"
                  break
                  ;;
            *     )
                  echo "can't get you. What do you mean? "
                  ;;   
    esac
done


Top
 Profile  
 PostPosted: Thu Jan 12, 2017 3:49 pm   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
Here's what I got now:
Code:
#!/bin/bash

   echo “Please make sure your in the directory you want to have images resized in!”
   echo “EX. cd ~/Pictures”
   sleep 2
   echo “If you want a different image size then please edit the script.”

PS3='Please enter your choice: '
options=(“PNG” “JPG” “GIF” "Quit")
select opt in "${options[@]}"
do
    case $opt in
        “PNG”)
       echo -e “Resizing your png’s!”
            sips -Z 192 * .PNG
            ;;
        “JPG”)
            echo -e “Resizing your jpg’s!”
            sips -Z 192 * .JPG
            ;;
        GIF”)
            echo -e “Resizing your gif’s!”
            sips -Z 192 * .GIF
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

   echo “Thank you for using this image resizing script don’t forget to visit and signup on https://technologx.com”


Top
 Profile WWW  
 PostPosted: Thu Jan 12, 2017 11:06 pm   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
Now with my new script is there a way where I can replace the 192 with something to were the user can enter in the size they desire?


Top
 Profile WWW  
 PostPosted: Thu Jan 12, 2017 11:41 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 524
Code:
while true; do
    read -P "give file type" ftype
    case $ftype in
         p|P|png  )  dosomething
                ;;
    esac
    read -p "give x" x-pix
    resize ........
done


Top
 Profile  
 PostPosted: Fri Jan 13, 2017 12:47 am   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
uhelp wrote:
Code:
while true; do
    read -P "give file type" ftype
    case $ftype in
         p|P|png  )  dosomething
                ;;
    esac
    read -p "give x" x-pix
    resize ........
done


Alright I'm confused on that so how exactly would I do that? I'm still learning this so I'm thankful for you being patient with me I have a learning disability and am slow at times with learning new things. Also will that work for x & y?


Top
 Profile WWW  
 PostPosted: Fri Jan 13, 2017 3:25 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 524
Code:
while true; do
    read -P "give file type" ftype
    case $ftype in
         p|P|png  )  dosomething
                ;;
    esac
    read -p "give x" x-pix
    read -p "and now give y" y-pix
    some_resize_app  --width=$x --height=$y somePic
    echo you can define any var you want with \"read -p some_prompt anyvar\"
    echo and \"anyvar\" will hold whatever user entered.
done


Top
 Profile  
 PostPosted: Fri Jan 13, 2017 2:22 pm   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
So how would I add that to my options sorry I'm trying to understand here?


Top
 Profile WWW  
 PostPosted: Fri Jan 13, 2017 4:37 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 524
Read help select

I did not use the select statement.
It is somewhat old-fashioned.
If you want to use it anyway, then you do not need the while loop.
select syntax already is a loop.


Top
 Profile  
 PostPosted: Fri Jan 13, 2017 5:03 pm   

Joined: Mon Jan 09, 2017 3:29 am
Posts: 7
So would it be better if I just left my script the way it is and the user can change it manually?


Top
 Profile WWW  
 PostPosted: Fri Jan 13, 2017 11:03 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 524
If a user has to edit a script to adjust it to his needs, it is absolutely worthless.

There are basically two solution right here.
One using the very outdated select
and one that don't.

You just need to understand both ways.
Read 'em and try 'em.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP