shell函式的呼叫執行

2021-09-05 22:08:45 字數 593 閱讀 6208

要想呼叫執行自己編寫的shell檔案中的函式,有如下方法:

比如,自己編寫了乙個test.sh檔案如下,

#**********=test.sh:

#! /bin/sh

echo_line()

echo_line()

#********************==

要想在shell裡呼叫echo_line()和echo_line()方法,首先需要註冊test.sh到當前shell

命令:  . test.sh

然後直接在shell裡呼叫以上兩個方法就可以了

命令:

echo_line (呼叫函式)

echo_hello (呼叫函式)

注意:不過這種方法每次都得註冊原指令碼到當前shell。

另外也可以在test.sh檔案裡用引數控制直接呼叫:

if [ "$1" = echo_line  ]

then

echo_line

fi if [ "$1" = echo_hello  ]

then

echo_hello

fi

使用exec函式組呼叫執行shell指令碼

linux下的exec函式不是單一的函式,而是乙個函式組,分別為 cpp view plain copy intexecl const char path,const char arg,intexeclp const char file,const char arg,intexecle const ...

用ruby呼叫執行shell命令

碰到需要呼叫作業系統shell命令的時候,ruby為我們提供了六種完成任務的方法 1.exec方法 kernel exec方法通過呼叫指定的命令取代當前程序 例子 irb exec echo hello hostname hello nate.local 值得注意的是,exec方法用echo命令來取...

C函式呼叫shell腳

c程式呼叫shell指令碼共有三種方式 system popen exec系列函式 1 system shell命令或shell指令碼路徑 執行過程 system 會呼叫fork 產生子程序,由子程序來呼叫 bin sh c string來執行引數string 字串所代表的命令,此命令執行完後隨即返...