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.
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.
#!/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..