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

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

    Edit: WARNING - seems this doesn't work in all cases, but works well on many public scripts.

    If you have a bash script called "control" and inside it you have a function called "build":

    function build() { 
      ... 
    }
    

    Then you can call it like this (from the directory where it is):

    ./control build
    

    If it's inside another folder, that would make it:

    another_folder/control build
    

    If your file is called "control.sh", that would accordingly make the function callable like this:

    ./control.sh build
    

提交回复
热议问题