How can I run a function from a script in command line?

前端 未结 9 1409
再見小時候
再見小時候 2020-12-07 08:05

I have a script that has some functions.

Can I run one of the function directly from command line?

Something like this?

myScript.sh func()
         


        
9条回答
  •  情话喂你
    2020-12-07 08:21

    Solved post but I'd like to mention my preferred solution. Namely, define a generic one-liner script eval_func.sh:

    #!/bin/bash
    source $1 && shift && "@a"
    

    Then call any function within any script via:

    ./eval_func.sh   ...
    

    An issue I ran into with the accepted solution is that when sourcing my function-containing script within another script, the arguments of the latter would be evaluated by the former, causing an error.

提交回复
热议问题