Ubuntu – How to create a bash scrip that calls other scripts depending on commandline swith

scripts

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!

Best Answer

#!/bin/bash

function show_help() {
    cat << ENDHELP
-a you can do stuff like that.
-b you can do stuff like that.
-c this is extremely fancy stuff.
ENDHELP
}

case $1 in
    --help)
        show_help
    ;;
    -a)
        sudo /path/to/other/script
    ;;
    -b)
        do_some_stuff
        do_another_stuff
    ;;
    -c)
        do_extremely_fancy_stuff
        do_another_extremely_fancy_stuff
        run_as_many_commands_as_you_want
    ;;
    *)
        echo "Run $0 --help"
    ;;
esac
  • $0 is the script name.
  • $1 is the first command line argument, $2 is the second, and so on.

Read bash manpage for reference.

Related Question