Skip to main content
deleted 3 characters in body
Source Link
FelixJN
  • 14.1k
  • 2
  • 36
  • 55

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 ":c:a:""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

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 ":c:a:" 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

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
Source Link
FelixJN
  • 14.1k
  • 2
  • 36
  • 55

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 ":c:a:" 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