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

前端 未结 9 1381
再見小時候
再見小時候 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:32

    I have a situation where I need a function from bash script which must not be executed before (e.g. by source) and the problem with @$ is that myScript.sh is then run twice, it seems... So I've come up with the idea to get the function out with sed:

    sed -n "/^func ()/,/^}/p" myScript.sh

    And to execute it at the time I need it, I put it in a file and use source:

    sed -n "/^func ()/,/^}/p" myScript.sh > func.sh; source func.sh; rm func.sh

提交回复
热议问题