1

I have written shell script with different functions, that I can invoke from the command line like so:

bash script.sh -i -b

So that will run those two functions and not the other ones in the script. However, I want to reverse this logic, by have the script run every function by default if I just do

bash script.sh

And if I pass arguments like -i -b I would like to skip over those functions instead. Any help is appreciated!

5
  • 1
    check getopts in bash over google Commented Jul 1, 2019 at 17:54
  • 1
    You need to detect that no arguments are processed by getopts and implement your logic from there. Check this Unix StackExchange question. If you don't want to inspect getopts behavior, you can simply check if [ $# -eq 0 ] . Commented Jul 1, 2019 at 18:07
  • See manual loop section of mywiki.wooledge.org/BashFAQ/035 Commented Jul 1, 2019 at 19:15
  • @AlexandreJuma how do I skip over functions through command line args? Commented Jul 1, 2019 at 19:48
  • Check the new answer. Commented Jul 1, 2019 at 21:14

2 Answers 2

1

To implement those the two logics:

  1. If no arguments at all, run all functions
  2. For each argument passed, skip some function execution

You can do it like this:

#!/bin/bash

function func_i {
  echo "I am i."
}

function func_b {
  echo "I am b."
}

function main {

  # Check if there are no arguments, run all functions and exit.

  if [ $# -eq 0 ]; then
    func_i
    func_b
    exit 0
  fi

  # Parse arguments -i and -b, marking them for no execution if they are passed to the script.

  proc_i=true
  proc_b=true

  while getopts "ib" OPTION; do
      case $OPTION in
      i)
          proc_i=false
          ;;
      b)
          proc_b=false
          ;;
      *)
          echo "Incorrect options provided"
          exit 1
          ;;
      esac
  done

  # Execute whatever function is marked for run.

  if $proc_i; then func_i; fi
  if $proc_b; then func_b; fi
}

main "$@"

Some explanations:

$# returns the number of arguments passed to the script. If $# is equal to 0, then no arguments were passed to the script.

getops accepts switches -i and -b, all other switches will result in error handled in the *) case.

Sign up to request clarification or add additional context in comments.

2 Comments

I haven't implemented getopts to my script, it's just simple functions and calling them through command line. New to bash scripts so a bit more detail would be appreciated!
This solves the problem of running all the functions, but what if I want to skip a specific part of the code?
1

You could black list items from the list of functions that are called by default. Something like:

#!/bin/bash

list='a b c d e f g h i'

# define some functions
for name in $list; do
        eval "func_$name() { echo func_$name called with arg \$1; }"
done

# black list items from list
for x; do
        list=$(echo "$list" | tr -d ${x#-})
done

for name in $list; do
        func_$name $name
done

But frankly it makes more sense to do something like:

$ cat script.sh 
#!/bin/bash

list='a b c d e f g h i'
test $# = 0 && set -- $list # set default list of functions to call

# define some function
for name in $list; do
        eval "func_$name() { echo func_$name called with arg \$1; }"
done

for name; do
        func_$name $name
done
$ bash ./script.sh 
func_a called with arg a
func_b called with arg b
func_c called with arg c
func_d called with arg d
func_e called with arg e
func_f called with arg f
func_g called with arg g
func_h called with arg h
func_i called with arg i
$ bash ./script.sh c g
func_c called with arg c
func_g called with arg g

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.