|
|
| Portable Shell Programming (ISBN: 0134514947) |
 |
List Price: $49.93
Our Price: $49.93
Used Price: $29.89
Release Date: 19 October, 1995
Manufacturer: Prentice Hall PTR (Paperback)
Sales Rank: 19,475
Author: Bruce Blinn
|
More Info
|
|
|
Switch
|
2001-11-25 03:49:52
|
| |
sh
|
|
|
|
|
Category: source:sh:runtime
|
|
Description: Filter the arguments given to a shell script
|
|
Platform: unix
|
|
Author: mind
|
|
Viewed: 2280
|
|
Rating: 4/5 (4 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
# Switch example by mindsport
# Contact: mind@infract.net
#
# $0 = program name: ./switch.sh
# $1 = first switch after ./switch.sh <switch1>
# $2 = second switch after ./switch.sh <switch1> <switch2>
# etc..
if [ -z $1 ]; then # Nothing was put after ./switch.sh
echo "Usage: $0 <-switch>"
echo
echo "Switches:"
echo " -a <prints a>"
echo " -b <prints b>"
exit # ends script at this point
fi
if [ $1 = "-a" ]; then
echo "You picked switch -a"
# You can also add 'exit' here to finish the script without going on..
fi
if [ $1 = "-b" ]; then
echo "You picked switch -b"
# also you can add 'exit' here to finish the script
fi
# This is an example of a second switch like ./switch -a -b
if [ -z $2 ]; then # nothing was entered for switch2 so we ..
exit # exit....
else
if [ $2 = "-a" ]; then
echo "You picked switch -a"
# Could finish the script with 'exit'
fi
if [ $2 = "-b" ]; then
echo "You picked switch -b"
# Also could finish with 'exit'
fi
fi
|
|
|