shell文件 include 的方法——“ 。 文件”
[macg@localhost testshell]$ ls testget testlib |
[macg@localhost testshell]$ cat testlib #!/bin/bash test() { echo "hello " } [macg@localhost testshell]$ cat testget #!/bin/bash . /home/macg/testshell/testlib include 文件 test 调用include文件里的函数 |
[macg@localhost testshell]$ sh testget hello |
shell include 不止可以包含函数,也可以包含全局变量
[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 |
实际上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文件的方法
for i in /etc/profile.d} local pid # Test syntax. if [ "$#" = 0 ] ; then echo $"Usage: status {program}" return 1 fi # First try "pidof" pid=`pidof -o $$ -o $PPID -o %PPID -x $1 || / pidof -o $$ -o $PPID -o %PPID -x ${base}` if [ -n "$pid" ]; then echo $"${base} (pid $pid) is running..." return 0 fi # Next try "/var/run/*.pid" files if [ -f /var/run/${base}.pid ] ; then read pid < /var/run/${base}.pid if [ -n "$pid" ]; then echo $"${base} dead but pid file exists" return 1 fi fi # See if /var/lock/subsys/${base} exists if [ -f /var/lock/subsys/${base} ]; then echo $"${base} dead but subsys locked" return 2 fi echo $"${base} is stopped" return 3 } |
[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 |
以名字的方式杀掉一个进程
[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 PID PPID C STIME TTY TIME CMD root 4769 1 0 14:47 ? 00:00:00 rpc.mountd root 4795 1 0 14:47 ? 00:00:00 /usr/sbin/dhcpd root 4942 4408 0 16:32 pts/0 00:00:00 ps –ef [root@mm testtip]# sh test.sh input:dhcpd 0 OK ] [root@mm testtip]# ps -ef UID PID PPID C STIME TTY TIME CMD root 4769 1 0 14:47 ? 00:00:00 rpc.mountd root 4950 4408 0 16:34 pts/0 00:00:00 ps -ef |
confirm() 系统暂停,输入y/n/c
# Confirm whether we really want to run this service confirm() { [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=yes while : ; do echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] " read answer if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then return 0 elif strstr $"cC" "$answer" ; then rm -f /var/run/confirm [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=no return 2 elif strstr $"nN" "$answer" ; then return 1 fi done } |
[machome@mm testtip]$ cat test.sh #!/bin/bash . ./functions confirm result=$? echo "$result" |
[machome@mm testtip]$ sh test.sh Start service (Y)es/(N)o/(C)ontinue? [Y] y 0 [guan@mm testtip]$ sh test.sh Start service (Y)es/(N)o/(C)ontinue? [Y] n 1 [guan@mm testtip]$ sh test.sh Start service (Y)es/(N)o/(C)ontinue? [Y] c 2 |
/etc/profile中的的函数pathmunge(),给$PATH增加一个路径
#!/bin/bash pathmunge () { if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi fi } |
[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]# |