Write a shell script with options

Hi All

I am little bit confused to write a script. This script needs the options like unix commands
i.e.

�S to start process.
�C to check process.
-u : user
-p : password

like.

script should run like this

./script.sh -u username -p ***** -S processname

there may be all the options or not. So I am confused how to write this script ??

My Idea :
I want to read all the argument pass and match with the respected details
like if there is -u used so next should be a username or -S then next should be a process name. But this thing needs lot of checks on the arguments only.
and after that I have to call the "case" in the script if there is any specific option in command line.

Please suggest your idea or help me with mine.

thanks
Atul Singh

What will it be doing if no argument sent?

Since you are the one writing the script dont accept no agument...and add -h for help...
Can S and C be used together?
how many processes can it "start"?

thanks vbe your suggestions.

No, S and C is not use together. Here no option is used like other

ls -lrt

.

In my case if a option is there , its respective value should be there.

Thanks
Atul Singh

Use a loop with a case statement:

MODE=""

# Loop until we run out of arguments.
while [ "$#" -gt 0 ]
do
        case "$1" in
        -p)
                shift
                MYPASS="$1"
                ;;
        -u)
                shift
                MYUSER="$1"
                ;;

        -S)
                MODE="start"
                ;;

        -C)
                MODE="check"
                ;;
        *)
                echo "Unknown argument '$1'" >&2
                exit 1
                ;;
        esac

        # Delete $1 argument, set $1=$2, $2=$3, ...
        shift
done

if [ -z "$MODE" ]
then
        echo "Must give -S or -C" >&2
        exit 1
fi

if [ "$MODE" = "check" ]
then
        echo "doing check"
fi

...

Read about getopts. There're lot of posts in this forum on how to use getopts.

thanks guys for your suggestions and ideas.
will read about getopts

thanks
Atul Singh