Hello!
I have a question about scripts with parameters.
I have a install script and i want it to start the daemon if the install_script has the parameter start at execution.
If there is not a parameter it should just do the install routine and leave the start part untouched.
But i dont want to copy the whole script twice
case $1 in
start)
[...whole script with start]
* )
[...script without start routine]
esac
Is something like this possible?
kind regards
Jotne
June 10, 2013, 6:54am
2
This one ways to do it. (move code to run everytime after esac
)
This will run #normal script every time
#!/bin/bash
# start block
case $1 in
start)
<some start code>
<some start code>
esac
#normal script
<some code>
<some code>
To run only start code when start is added
#!/bin/bash
# start block
case $1 in
start)
<some start code>
<some start code>
exit 0
esac
#normal script
<some code>
<some code>
#!/bin/bash
# start block
if [ "$1" = "start" ]; then
<some start code>
<some start code>
fi
#normal script
<some code>
<some code>
Is this also working the other way around?
#!/bin/bash
#normal script
<some code>
<some code>
# start block
if [ "$1" = "start" ]; then
<some start code>
<some start code>
fi
Because the install stuff has to happen first. Otherwhise it will not start
kind regards
Jotne
June 10, 2013, 7:02am
4
This way, it will first run normal script
and then start script
if start
is given
So it would be your first example in post #2 ?
kind regards
Jotne
June 10, 2013, 7:54am
6
#!/bin/bash
# start block
case $1 in
start)
<some start code>
<some start code>
esac
#normal script
<some code>
<some code>
This will run start block
if start
is given.
Then it run normal script
, regardless of start
no start
.
Not sure if this is what you want.
jotne:
#!/bin/bash
# start block
case $1 in
start)
<some start code>
<some start code>
esac
#normal script
<some code>
<some code>
This will run start block
if start
is given.
Then it run normal script
, regardless of start
no start
.
Not sure if this is what you want.
yeah propably. The thing is that the normal part has to be executed first because otherwhise the start part will not work.
Is it still working if i put the case block at the bottom of the script?
kind regards
Jotne
June 10, 2013, 8:08am
8
#!/bin/bash
#normal script
<some code>
<some code>
# start block
if [ "$1" = "start" ]; then
<some start code>
<some start code>
fi
Then you need this.
Normal first, and then start, but only if start is set.
Every script starts from top and run down, easy to understand.
jotne:
#!/bin/bash
#normal script
<some code>
<some code>
# start block
if [ "$1" = "start" ]; then
<some start code>
<some start code>
fi
Then you need this.
Normal first, and then start, but only if start is set.
Every script starts from top and run down, easy to understand.
Ok so as every other language
But if I do it this way, it will not throw errors if there is a missing parameter?
After some google if found that shell is capable to do function, so i come around with this.
case $1 in
start)
F_INSTALL; sleep 5; F_START;
*)
F_INSTALL;
esac
kind regards
It's better to use getops for parsing command line arguments.
getopts - Wikipedia, the free encyclopedia
Hope that helps
Regards
Peasant.