-1

[Update]Please note that this is a simplified code for this post in order to satisfy minimal reproducible example. There will more functions in the script.

I have a Bash script on a Github repo:

#!/usr/bin/env bash

fn_test1() {
    echo "starting ..."
    read -rp "Do you want to continue? yes/y or no/n   " PANS
    ans=$(echo "$PANS" | cut -c 1-1 | tr "[:lower:]" "[:upper:]")
    if [[ "$ans" = "Y" ]]; then
        echo "confirmed 1."
    fi
    echo "done"
}

fn_main() {
    if [ $# -eq 1 ]; then
        case $1 in
        "test")
            fn_test1
            ;;
        esac
    else
        echo "Something wrong."
        exit 1
    fi
}

fn_main "$@"

I want to call the test function from this script like this, but it doesn't work:

bash -c "$(curl -s https://raw.githubusercontent.com/shinokada/awesome/main/test; test)"

The output is:

Something wrong.

How can I call a function in a bash script? Is it possible?

shin
  • 31,901
  • 69
  • 184
  • 271
  • `bash -c "$(curl -s https://raw.githubusercontent.com/shinokada/awesome/main/test)" bash test`. This is not a good practice at all. – oguz ismail Jun 04 '21 at 07:26
  • 2
    @shin: There is no function `test` in your script (and, BTW, it would be a bad idea to name a function _test_). The only functions in your script ar `fn_test1` and `fn_main`. – user1934428 Jun 04 '21 at 07:45
  • The script is not receiving any parameters – DTan13 Jun 04 '21 at 07:52
  • @user1934428 OP wants to run this script with `test` as the mere positional parameter, and expects `read -rp` in `fn_test1` to work. – oguz ismail Jun 04 '21 at 07:59
  • @user1934428 Ah~. Maybe I should remove the main function and just call fn_test1? – shin Jun 04 '21 at 08:12
  • @shin : If oguzismail interpreted your question correctly (but plese clarify this by updating your question), I would simpy store your script into a file (for instance `curl ... >icntoagfn.sh`) and then run it with `bash icntoagfn.sh test`. – user1934428 Jun 04 '21 at 08:16
  • @user1934428 Yes, curl -s https://raw.githubusercontent.com/shinokada/awesome/main/test > temp1 && bash temp1 test && rm temp1 works. Thanks. – shin Jun 04 '21 at 08:31

1 Answers1

0

The script is not receiving any parameters. You can do the following simple method

wget https://raw.githubusercontent.com/shinokada/awesome/main/test

bash test test
DTan13
  • 428
  • 6
  • 16