Shell程式設計之函式

2022-08-02 15:57:14 字數 4007 閱讀 6519

將程式裡多次被呼叫的相同**組合起來(函式體),並為其取乙個名字(函式名),其他所有想重複呼叫這部分**的地方都只需呼叫這個名字即可。

優勢:

寫法一:

function 函式名()

寫法二:

function 函式名

寫法三:

函式名()

(1)不帶引數的函式

直接輸入函式名即可(不帶小括號)

注意:(2)帶引數的函式

函式名 引數1 引數2

注意:(1)建立兩個簡單的函式並呼叫執行

[root@codis-178 ~]# cat 8_1.sh 

#!/bin/bash

oldboy()

function oldgirl()

oldboy

oldgirl

[root@codis-178 ~]# sh 8_1.sh

i am oldboy.

i am oldgirl.

(2)分離函式體和執行函式指令碼

[root@codis-178 ~]# cat >> /root/8_2.sh<<- eof

> oldboy()

> eof

[root@codis-178 ~]# cat 8_2.sh

oldboy()

[root@codis-178 ~]# cat 8_3.sh

#!/bin/bash

[ -f /root/8_2.sh ] && . /root/8_2.sh || exit 1

oldboy

[root@codis-178 ~]# sh 8_3.sh

i am oldboy.

(3)帶引數的函式

[root@codis-178 ~]# cat 8_2.sh 

oldboy()

[root@codis-178 ~]# cat 8_4.sh

#!/bin/bash

[ -f /root/8_2.sh ] && . /root/8_2.sh || exit 1

oldboy xiaoda

[root@codis-178 ~]# sh 8_4.sh

i am oldboy.you are xiaoda

(4)將函式的傳參轉換成指令碼檔案命令傳參

[root@codis-178 ~]# cat 8_2.sh 

oldboy()

[root@codis-178 ~]# cat 8_4.sh

#!/bin/bash

[ -f /root/8_2.sh ] && . /root/8_2.sh || exit 1

oldboy $1

[root@codis-178 ~]# sh 8_4.sh xiaoda

i am oldboy.you are xiaoda

[root@codis-178 ~]# cat 8_5.sh 

#!/bin/bash

function usage()

function check_url()

function main()

main $* # 將命令列接收的所有引數傳給函式

. /etc/init.d/functions #引入系統函式庫

www.bbaidu.com is no. [failed]

思路:

[root@codis-178 ~]# cat sys_opt.sh

#!/bin/bash

# author:tongxiaoda

# set env

export path=$path:/bin:/sbin:/usr/sbin

# require root to run this script

if [ "$uid" != "0" ];then

echo "please run this script by root."

exit 1

fi# define cmd var

service=`which service`

chkconfig=`which chkconfig`

function mod_yum()

function close_selinux()

function close_iptables()

function least_service()'|bash

chkconfig|egrep "crond|sshd|network|rsyslog|sysstat"|awk ''|bash

}function adduser()

function charset()

function time_sync()

function com_line_set()

function open_file_set()

function set_kernel()

function update_linux()

main()

main

[root@codis-178 ~]# cat check_opt.sh 

#!/bin/bash

# set env

export path=$path:/bin:/sbin:/usr/sbin

# require root to run this script

if [ "$uid" != "0" ];then

echo "please run this script by root."

exit 1

fi# source function library

. /etc/init.d/functions

function check_yum()

function check_selinux()

function check_services()

function check_open_file()

main()

main

[root@codis-178 ~]# cat rsync_start 

#!/bin/bash

# chkconfig: 2345 20 80

# description: rsync startup scripts

. /etc/init.d/functions

function usage()"

exit 1

}function start()

function stop()

function main()

main $*

shell程式設計之函式

shell函式的本質是一段可以重複使用的腳步 這段 被提前編寫好了,放在了指定的位置,使用時直接呼叫即可 1 定義函式 可以帶function fun 定義,也可以直接fun 定義,不帶任何常數 方法一 function name 方法二 name function 是shell中的關鍵字,專門用來...

shell 程式設計之函式

shell 函式的定義和普通的c語言函式定義差不多 function shell 函式的返回值,可以顯示的return 語句,如果沒有return 那麼就會把最後一條語句的執行結果作為返回值 shell 函式的返回值只能是整數 例子 hello echo you select hello hello...

Shell指令碼程式設計之Shell函式

1.linux shell可以使用者定義函式,然後在shell指令碼中可以隨便呼叫 shell中函式的定義格式如下 function funname 1 可以帶function fun 定義,也可以直接fun 定義,不帶任何引數 2 引數返回,可以顯示加 return返回,如果不加,將以最後一條命令...