First of all, welcome to the site.
The answer to your question depends on how consistent the arguments are passed to the script.
If it is guaranteed that
- the argument "create" is only used when there are a total of three arguments, with "arg1" and "arg2" preceding "create", and similarly
- the argument "add" is only used when there are a total of two arguments, with "arg1" preceding "add"
then you can perform an if
test inside your script operating on the builtin variable $#
which contains the total number of arguments with which the script is called:
#!/bin/bash
if [[ "$#" == "3" && "$3" == "create" ]]
then
create_fn <possible arguments here>
elif [[ "$#" == "2" && "$2" == "add" ]]
then
create_add <possible arguments here>
fi
To explain: In the if
test we check if the number of arguments ($#
) is 3 and the third argument ($3
) is "create", and then call create_fn
(possibly with arguments; you didn't specify if it takes any in your question); or if the number or arguments is 2 and the second argument is "add", in which case we call create_add
.
If the order of command-line arguments, on the other hand, is arbitrary, you will have to implement a more intelligent parameter handling routine, and something like getopts would be your friend.
For more information about shell programming, have a look into the Advanced Bash Scripting Guide.
Concerning the second part of your question: The feature you are looking for is called "bash completion" and is not a trivial thing, in particular since you want to auto-complete command-line arguments where the possible values can depend on the position of the currently typed argument in the list and the preceding items on the command-line. It is possible, but not trivial (see e.g. How to customize Bash command completion?).