0

I have very long bash script, requirement is to execute script like command line. If I execute

 ./script.sh arg1 arg2 create

As above if i pass string "create" along with "arg1" & "arg2" should execute particular function (like create_fn() ) of script.sh

Like above

./script.sh arg1 add 

If i pass string "add" along with only arg1 --> should execute create_add() function.

Also , If if i press tab after "cr" should auto write "create" / ad -> add

can anyone help me here

2 Answers 2

0

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 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?).

1
  • I will try and update you
    – Tom15
    Commented Oct 30, 2019 at 11:00
0

I think you should change the setup a bit and use option flags via getops. Those however need to be at the beginning and be single letters only. An example:

 #!/bin/bash

 add=no
 create=no
 while getopts "ca" option ; do
   case $option in
     a) add=yes ;;
     c) create=yes ;;
     \?) echo 'Invalid option. Aborted.' ; exit ;;
   esac
 done
#shift argument numbers so that file1 is $1 etc.
shift $((OPTIND-1))

if [[ "$add" == "yes" ]] ; then
  do_add_procedure
elif [[ "$create" == "yes" ]] ; then
  do_create_procedure
else
  do_neither
fi

Depending on how you do the if-statement (i.e. split it into two statements), you now can even run both "create" and "add" by giving both options. However, bash-script arguments must be at the beginning of your command:

./script.sh -a file1 file2
2
  • I tried append your script, but this even work for -> ./script.sh arg1 create or ./script.sh create . my need is - should only work when passing arg1 arg2 create else throw error . where arg1 and arg2 is constant when passing create.
    – Tom15
    Commented Oct 31, 2019 at 16:33
  • @Tom15 Total number of arguments is given as ${#}, so check if this is 2 after shifting. (else abort). If you want specifically two fixed files, check if ${1} and ${2} are the correct strings.
    – FelixJN
    Commented Oct 31, 2019 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .