Register
It is currently Sun Sep 21, 2014 12:07 pm

Ask user for a "yes" or "no"


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Oct 02, 2007 9:39 pm   
User avatar

Joined: Tue Oct 02, 2007 9:22 pm
Posts: 11
Location: Argentina
I'm needing to ask the user if he wants to perform an operation I've put togheter in a script.
I know I have to use conditional statements, but I'm just learning to use them, and neither know how to read what is entered at prompt to use as input in the script.
Is there any script in BashScripts that can exemplify me how to do this?


Top
 Profile  
 PostPosted: Thu Oct 04, 2007 5:21 am   

Joined: Wed Jul 25, 2007 11:45 am
Posts: 11
This is very read in a very basic "script":

Code:
while read a; do
  echo $a
done


Hope this helps.


Top
 Profile  
 PostPosted: Thu Oct 04, 2007 8:07 pm   
User avatar

Joined: Tue Oct 02, 2007 9:22 pm
Posts: 11
Location: Argentina
Thanks slacker. I've been doing my homework and by now I have my firsth not-so-begginer script functional, even when there are still things to do. I'd like to show it so you can help me to do it better, but since I didn't find any other place, I'll do it here.

Code:
#!/bin/bash

# To do:
#       - Accept only y;Y/n;N as input for the questions
#       - Simplify conditions
#       - Learn about printf and replace echo with it
#       - Use some lighter and more universal encoder instead of soundconverter

# Start of the script: verify that a route has been given as argument.

if [ -z "$1" ]
  then
    echo "No directory as been specified"
    echo "Usage: all2mp3 [directory]"
    exit 1
  else
    echo
    echo "All the '.flac', '.wav' and '.wma' files in the directory will be encoded to '.ogg'"
   echo
   sleep 3s
fi

# Establish the NUMFILES variable, wich equals the number of files that can be encoded on the given route. If NUMFILES equals 0, then exit.

NUMFILES=`find $1 \( -iname "*.flac" -o -iname "*.wav" -o -iname "*.wma" \) -printf '%f\n\v' | grep -c ^`

    if [ "$NUMFILES" = 0 ]
      then
        echo "No suitable files for encoding have been found"
        exit 2
      else   

# Here find displays the files on the music directory and then the question of whether the action should be performed or not is asked.

        find $1 \( -iname "*.flac" -o -iname "*.wav" -o -iname "*.wma" \) -printf '%f\n\v'
       echo
        echo "There are $NUMFILES files to be encoded"
        echo
        printf '\nDo you wish to procced? Y/N\n\v'
        read -s -n 1 aval

# If yes, then finds all .wav, .wma and .flac files on the music directory and encodes them as .ogg, else, exit.

      if [ $aval = "y" -a "Y" ]
        then
          find $1 \( -iname "*.flac" -o -iname "*.wav" -o -iname "*.wma" \) -execdir soundconverter -b -m audio/x-vorbis -s .ogg {} \;
          echo "Encoding finished"
          echo
          sleep 3s
          printf '\nDo you wish to remove the '.flac', '.wav' and '.wma' files? Y/N\n\v'
          read -s -n 1 aval2

# If yes, then remove the original files, else leave them.

        if [ $aval2 = "y" -a "Y" ]
          then
            find $1 \( -iname "*.flac" -o -iname "*.wav" -o -iname "*.wma" \) -delete
          else
            echo "No file has been deleted"
            echo "Finished"
            sleep 3s
        fi
        else
          echo "No file has been encoded"
          echo "Finished"
          sleep 3s
          exit 3
      fi
    fi


Any advice?


Top
 Profile  
 PostPosted: Fri Feb 08, 2008 8:22 am   

Joined: Tue Feb 05, 2008 8:24 am
Posts: 14
JuanManuel wrote:
I'm needing to ask the user if he wants to perform an operation I've put togheter in a script.

If you are looking to add some interactivity to your script, consider dialog or whiptail utilities. They both serve to draw dialog boxes.

For example, yes-or-no is the standard box:
Code:
dialog --yesno 'Are you really absolutely 100% sure?' 7 50 && echo "It's OK" || echo 'He is not sure yet'

or
Code:
whiptail --yesno 'Are you really absolutely 100% sure?' 7 50 && echo "It's OK" || echo 'He is not sure yet'

will draw a nice dialog box with yes and no options,
Quote:
Image

and print one phrase or another depending on the user choice.

This is an article in Linux Journal about dialog. whiptail is practically the same, but smaller.

--
UseFreeTools — my blog about GNU/Linux and Free Software


Top
 Profile WWW  
 PostPosted: Fri Feb 08, 2008 2:20 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 667
Location: Des Moines, Iowa
You can also use case/esac to ask a yes or no question ;)

Instead of if/then, case/esac makes for a cleaner look.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 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:  


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