It is currently Tue, 17 May 2022 03:22:13 GMT



 
Author Message
 sh newb: case menus w/scripts

Building case menus and including entire scripts in case options.

Suppose you wanted to write a script where you gave the user the option
of choosing between two seperate scripts to run (that were too small to merit
a seperate file).

The first script alone, could read:
----------------------------------------
#!/bin/sh
## echo script

echo kkkkkkkkkkkk
echo kkkkkkkkkkkk
echo jjjjjjjjjjjj
---------------------------

and the second could look like:
-------------------------------------
#!/bin/sh
## head script

for file in `ls`
   do  head -2 $file
done
-----------------------------

The basic template is simple:

"option" )\
......................; \
......................; \
......................; \

;;

with the "..............."  representing each line in the script you
want to include.

When putting them in a case script, you need a ";"  wherever a newline
would be, and a "\" wherever a literal line ends in the case rendition.

--------------------------------------------------------------------------

#!/bin/sh

clear  

# clears the screen first

echo
echo
echo
echo "[a]  Choose this execute echo script"
echo
echo "[b]  Choose this to execute head script "
echo
echo
echo

# the above part displays the choices on the screen with the lone "echo"s
# giving blank lines for formatting purposes.

read mm           # offers a prompt and takes input

# could be "abc" or "snort" instead of "mm"
# there must be no spaces after the "\"  below

case "$mm" in

"a" )\
echo kkkkkkkkkkkkk ; \
echo kkkkkkkkhhhhh ; \
echo jjjjjjjjjjjjj ; \

;;

"b" )\
for file in `ls` ;  \
 do   head -2  $file ; \
    done ; \

;;

*) echo "Not a valid option. Exiting." ; exit 1 \

;;

esac

exit 0

---------------------------------------------------------------------

# the *) just means "any other choices"

Alan C

--

   take control of your mailbox ----- elrav1 ----- http://www.**-**.com/



 Mon, 13 Feb 2006 04:25:40 GMT   
 sh newb: case menus w/scripts

Why not just

"option") /bin/sh <<- EOF
        ...
        ...
        ...
        EOF
;;

--
Kevin Rodgers



 Mon, 13 Feb 2006 06:45:00 GMT   
 sh newb: case menus w/scripts

If that's the case, why not make them functions? You can create
function libraries which would provide the same functionality for many
different scripts, and which would be much easier to maintain than
coding things inline in multiple scripts.

You only need the ';' if you use the '\'. You don't need the '\'.

Why not just

. function_library

a)
  a_function;;

b)
  another_function;;

*)
  emit_an_error;;

Joe



 Mon, 13 Feb 2006 07:01:08 GMT   
 
   [ 3 post ] 

Similar Threads

1. Is there switch and case in sh script?

2. Script to Convert Upper Case Filenames to Lower Case

3. script to change lower case to upper case

4. Execute a sh script under perl and sh: sh script; perl script?

5. make sh-script compatible between SUN-sh and bash

6. Newb request: scripts

7. Newb Help with a Basic Bash Script...

8. newb Q, script files

9. case command which returns menu after select

10. sh - case query


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software