《Shell 程式設計》07 函式

2021-08-30 04:41:45 字數 1686 閱讀 7241

標籤(空格分隔): shell

標準寫法:

function 函式名 ()
簡化寫法 1:

function 函式名
簡化寫法 2:

函式名 ()
shell 的函式分為最基本的函式和可以傳參的函式兩種,其執行方式分別如下:

1)執行不帶引數的函式時,直接輸入函式名即可(注意不帶小括號)。格式如下:

函式名

2)帶引數的函式執行方法,格式如下:

函式名 引數 1 引數 2

cat >>/etc/init.d/functions<<- eof

ylt()

eof

[root@web001 ~]# tail -3 /etc/init.d/functions

ylt()

#!/bin/bash

[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1

ylt

帶引數的 shell 函式

[root@web001 ~]# tail -3 /etc/init.d/functions

ylt()

#!/bin/bash

[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1

ylt yyy

[root@web001 scripts]# cat rsync1.sh

#!/bin/bash

# checkconfig: 2345 20 80

. /etc/init.d/functions

function usage()"

exit 1

}function start()

function stop()

function main()

main $*

[root@web001 scripts]# sh rsync1.sh start

rsyncd is started. [ ok ]

[root@web001 scripts]# sh rsync1.sh stop

rsyncd is stopped. [ ok ]

[root@web001 scripts]# sh rsync1.sh restart

rsyncd is stopped. [ ok ]

rsyncd is started. [ ok ]

[root@web001 scripts]# sh rsync1.sh s

usage:rsync1.sh

Shell程式設計 Shell函式

shell函式 1.將命令序列按格寫在一起 2.可方便重複使用命令序列 3.shell函式定義 function 函式名 4.呼叫函式的方法 函式名 引數1 引數2 5.shell函式應用示例 1 兩個數字求和 要求 通過sum 定義函式 兩個數求和 方法一 root localhost vim d...

07 詳解scala函式程式設計

在main函式體外面的原始碼 def test str string unit def main args array string unit 在main函式體裡面的原始碼 def main args array string unit test 張三 def test unit def test1...

shell程式設計 函式

函式是一段完成特定功能的 片段 塊 在shell中定義了函式,就可以使 模組化,便於復用 注意函式必須先定義才可以使用。函式名 function 函式名 函式名函式名 引數1 引數2 函式編寫shell指令碼,編寫系統工具,編寫迴圈的指令碼,功能選單 通過shell指令碼,編寫系統工具箱 編寫迴圈指...