I'm extremely new to bash and need to create a script that calls another script depending on what command line switch is taken.
Let's call it stuff.sh
than stuff.sh --help
should behave like help does in a normal program and give a list to the user what he can do
- -a you can do stuff like that.
- -b you can do stuff like that.
- -c this is extremely fancy stuff.
When I execute the script with stuff.sh - a
it should do something, let's say call another script with sudo
in front of it.
How could I do that?
any Ideas that everybody how is new to bash can understand easily?
Thank you!
$0
is the script name.$1
is the first command line argument,$2
is the second, and so on.Read bash manpage for reference.
If you are ok with
-h
instead of--help
you can also usegetopts
, which is very convenient. The code you would then need (modified from Eric Carvalho's answer):Now you can pass multiple options to your shellscript, so if you want to run a, b and c you can do
or even
The option string
'habc'
tells you which options are allowed,getopts
will automatically tell you about unsupported options. You can also add arguments to options (such as the name of the other scripts you want to run), then you need to add a colon (:
) behind your option, the argument is then stored in$OPTARG
. See this (incomplete) wikipage on how to do this.PS You can also use (the older)
getopt
(without the s),getopt
is a bit harder but allows you to use--help
.