shell文件 include 的方法——“ 。 文件”
shell include 不止可以包含函数,也可以包含全局变量
实际上sys v的启动文件都采用此种办法include 事先定义在/etc/rc.d/init.d/functions中的函数
[root@localhost macg]# cat /etc/init.d/sendmail
#!/bin/bash
# Source function library.
. /etc/rc.d/init.d/functions
for in 语句include *.sh文件的方法
以名字的方式杀掉一个进程
confirm() 系统暂停,输入y/n/c
/etc/profile中的的函数pathmunge(),给$PATH增加一个路径
[macg@localhost testshell]$ ls testget |
[macg@localhost testshell]$ cat testlib #!/bin/bash test() { echo "hello " } [macg@localhost testshell]$ cat testget #!/bin/bash . /home/macg/testshell/testlib test |
[macg@localhost testshell]$ sh testget hello |
[macg@localhost testshell]$ cat testlib #!/bin/bash test() { echo "hello " } TESTPRARAM="THIS IS A TEST" [macg@localhost testshell]$ cat testget #!/bin/bash . /home/macg/testshell/testlib test echo $TESTPRARAM |
[macg@localhost testshell]$ sh testget hello THIS IS A TEST |
[root@localhost macg]# cat /etc/init.d/sendmail
#!/bin/bash
# Source function library.
. /etc/rc.d/init.d/functions
for i in /etc/profile.d} } |
[root@mm testtip]# vi test.sh #!/bin/bash . ./functions echo -n "input:" read para status $para result=$? echo "$result" |
[root@mm testtip]# sh test.sh input:bgpd bgpd (pid 3430) is running... 0 [root@mm testtip]# sh test.sh input:xxx xxx is stopped 3 |
killproc()
[root@mm testtip]# vi test.sh #!/bin/bash . ./functions echo -n "input:" read para killproc $para result=$? echo "$result" |
[root@mm testtip]# ps -ef UID root root root [root@mm testtip]# sh test.sh input:dhcpd 0 [root@mm testtip]# ps -ef UID root root |
# Confirm whether we really want to run this service confirm() { } |
[machome@mm testtip]$ cat test.sh #!/bin/bash . ./functions confirm result=$? echo "$result" |
[machome@mm testtip]$ sh test.sh Start service 0 [guan@mm testtip]$ sh test.sh Start service 1 [guan@mm testtip]$ sh test.sh Start service 2 |
#!/bin/bash pathmunge () { } |
[root@vm test]# vi tip.sh pathmunge /home/macg pathmunge /home/mac echo $PATH |
[root@vm test]# ./tip.sh /home/mac:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr /X11R6/bin:/home/macg/bin:/home/macg [root@vm test]# |