Need help to make a shell script with usage

Hello,

I begin to write my first shell script, but I am totally lost, hope you can help me.

I'd like to write a script with some conditions and multiples usages:

./myscript.sh -i <host>

or

./myscript.sh -e <host>

Have you some suggestion, because I am really lost, I try to do something with an echo and a read answer but it is not good.

If you have a good exemple I can adapt it's a good thing for me too.

With no indication of what conditions you are talking about, no indication of what you want the -i and -e options to do, and no indication of what you want the script to do with or without options; it is hard to make any useful suggestions.

Take a look at this post, where it shows parameters being passed.

Francesco, can you briefly explain what is your requirement?

you just need to search for

CASE 

statement on google. I hope this is what you are looking for.

I must admit I am not very clear, please excuse me.

myscript.sh has differents usage

./myscript.sh -i <host>

==> will lauch an apt-get update on <$host>

./myscript.sh -e <host>

==> will launch an apt-get remove on <$host>

I have 6 hosts=

HOST='host1 host2 host3 host4 host5 host6'

I'd like to use fonction to execute the command, but all the exemple I saw are not very easy for me.

I'd like to have an usage:

./myscript.sh
Usage: myscript.sh {install -i host|remove -e host|}

Regards,

if [ "$#" -eq 0 ]
then
        echo "Usage: myscript.sh {install -i host|remove -e host|}"
        exit 1
fi

Thanks Corona, but I know to do this part.

I just need advices to howto write my functions.

Regards

We can't write the whole thing for you.

What have you tried?

This is how my script begin. For the exemple and the exercice, I am trying to execute something with differents programs. As you see, I think something missing. Do I have to write a loop with a for i in ?

#!/bin/sh
PROGS='ntp apache mysql'

set -e

if [ "$#" -eq 0 ]
then
        echo "Usage: test.sh {install-i prog|remove-r prog}"
        exit 1
fi

install() {
     echo "so you want to install $PROGS? (y/n)"

     read reponse

     if [[ $reponse == "y" ]]
       then apt-get install $PROGS
     fi
}

Without knowing what you want to do, it's difficult to say how you should write your for-loop.

You can use $# to tell how many arguments are left, and shift to delete the first argument, so you can make a loop while $# is greater than zero and check the value of $1 every loop to determine what to do.

I want to install a program (ntp, apache or mysql) with a shell script. It is just an exercice for me, because I can install it without writing anything and just launch an apt-get install.

I want this usage:

./myscript.sh -install package name

or

./myscript.sh -remove package name

where package name are a variable located in my script.

I am sorry if it sounds confused, I cannot speack english very well and I am a beginner in shell programming.

Hello Francesco

You can use something like this.

cat test.sh
#!/bin/sh
while getopts ":n:a:m:" opt; do
  case $opt in
    n)
      installing command for ntp
      ;;
    a)
      installing command for apache
      ;;
    m)
      installing command for mysql
     ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

How to run the script

./test.sh -a apache , ./test.sh -n ntp , ./test.sh -m mysql

This is the case if you want to remove or install the package one by one for ntp,apache or mysql..