Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 965e33040dd61030a94f0eb89877aee8 > files > 611

howto-html-en-20080722-2mdv2010.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
 <TITLE>BASH Programming - Introduction HOW-TO: User interfaces</TITLE>
 <LINK HREF="Bash-Prog-Intro-HOWTO-10.html" REL=next>
 <LINK HREF="Bash-Prog-Intro-HOWTO-8.html" REL=previous>
 <LINK HREF="Bash-Prog-Intro-HOWTO.html#toc9" REL=contents>
</HEAD>
<BODY>
<A HREF="Bash-Prog-Intro-HOWTO-10.html">Next</A>
<A HREF="Bash-Prog-Intro-HOWTO-8.html">Previous</A>
<A HREF="Bash-Prog-Intro-HOWTO.html#toc9">Contents</A>
<HR>
<H2><A NAME="s9">9. User interfaces</A>          </H2>

<H2><A NAME="ss9.1">9.1 Using select to make simple menus</A>
           </H2>

<P>
<BLOCKQUOTE><CODE>
<PRE>
           #!/bin/bash
           OPTIONS="Hello Quit"
           select opt in $OPTIONS; do
               if [ "$opt" = "Quit" ]; then
                echo done
                exit
               elif [ "$opt" = "Hello" ]; then
                echo Hello World
               else
                clear
                echo bad option
               fi
           done
          
</PRE>
</CODE></BLOCKQUOTE>
 
<P> If you run this script you'll see that it is a 
programmer's dream for text based menus. You'll probably notice 
that it's very similar to the 'for' construction, only rather 
than looping for each 'word' in $OPTIONS, it prompts the user.
<P>
<H2><A NAME="ss9.2">9.2 Using the command line          </A>
</H2>

<P>
<BLOCKQUOTE><CODE>
<PRE>
          #!/bin/bash        
          if [ -z "$1" ]; then 
              echo usage: $0 directory
              exit
          fi
          SRCD=$1
          TGTD="/var/backups/"
          OF=home-$(date +%Y%m%d).tgz
          tar -cZf $TGTD$OF $SRCD
         
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P> What this script does should be clear to you. The expression 
in the first conditional tests if the program has received an argument
($1) and quits if it didn't, showing the user a little usage message.
The rest of the script should be clear at this point.
<HR>
<A HREF="Bash-Prog-Intro-HOWTO-10.html">Next</A>
<A HREF="Bash-Prog-Intro-HOWTO-8.html">Previous</A>
<A HREF="Bash-Prog-Intro-HOWTO.html#toc9">Contents</A>
</BODY>
</HTML>