How to run shell script which has functions

hi Team,

I have a shell script in the repository which has set of functions with case statement usually I pass parameters to match the switch statement to call function based on the requirement how to achieve the same using Github actions.

@chaitanya-bojja-by ,

Here is an example workflow to run bash script, you can reference.

Script file (shell-scripts/bash-script.sh)

$1, $2 and $3 are the parameters that passed from outside when call script file.

#!/bin/bash

echo "Hello $1"
echo "Current time is $2"

if [[ $3 == 1 ]]; then
  echo "Execute Function-1"
elif [[ $3 == 2 ]]; then
  echo "Execute Function-2"
else
  echo "Execute Function-3"
fi

Workflow file

  • param1, param2 and param3 are the variables will be as parameters to pass into the script, they correspond to $1, $2 and $3 in the script.
  • Considering that the values of the passed parameters may contain whitespace, you’d better surround each parameter with the double quotes.
jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.2

      - name: Run bash script
        shell: bash
        run: |
          chmod +x ./shell-scripts/bash-script.sh
          param1='GitHub'
          param2=$(date +"%Y-%m-%d %H:%M:%S")
          param3=4
          ./shell-scripts/bash-script.sh "${param1}" "${param2}" "${param3}"

@chaitanya-bojja-by ,

How are things going?
Is my above example workflow helpful to you?
Generally, most shell scripts use the similar methods to pass parameters from outside into the script files.
Please reference to the example to have a try on your side. If you have any updates, feel free to tell us.