恒温混匀仪,stokke,冰鳞蓝鱼
函数基本使用
[root@vm_0_9_centos ~]# test() > {} -bash: syntax error near unexpected token `{}' [root@vm_0_9_centos ~]# test() {} -bash: syntax error near unexpected token `{}' [root@vm_0_9_centos ~]# test() > { > echo "test function" > } [root@vm_0_9_centos ~]# test test function [root@vm_0_9_centos ~]# function greeting > { > echo "hello world" > } [root@vm_0_9_centos ~]# greeting hello world [root@vm_0_9_centos ~]#
实例一:写一个守护进程,nginx如果关闭自动开启
vim nginx_daemon.sh
#!/bin/bash # #运行脚本的进程id,如果脚本名字有nginx字样,也需要把这个过滤掉 this_pid=$$ while true do ps -ef |grep nginx |grep -v grep | grep -v $this_pid &> /dev/null if [ $? -eq 0 ];then echo "nginx is running well!" sleep 3 else systemctl start nginx echo "nginx is down,start it....." fi done
把这个脚本放到后台运行
nohup sh nginx_daemon.sh &
关闭后查看
tail -f nohup.out
shell中传参
function name { echo "hello $1" echo "hello $2" }
函数调用
name derek alice
举例
[root@vm_0_9_centos shell_learn]# function greeting > { > echo "hello $1" > } [root@vm_0_9_centos shell_learn]# [root@vm_0_9_centos shell_learn]# greeting derek hello derek [root@vm_0_9_centos shell_learn]# greeting alice hello alice [root@vm_0_9_centos shell_learn]#
返回值的方式
方式一:return 方法二:echo
使用return返回值
使用echo返回值
实例一
#!/bin/bash # this_pid=$$ function is_nginx_running { ps -ef |grep nginx |grep -v grep | grep -v $this_pid &> /dev/null if [ $? -eq 0 ];then return else return 1 fi } is_nginx_running && echo "nginx is runnig...." || echo "nginx is stop!"
实例二:获取用户列表
#!/bin/bash # function get_users { users=`cat /etc/passwd | cut -d: -f1` echo $users } user_list=`get_users` index=1 for user in $user_list do echo "the $index user is: $user" index=$(($index+1)) done
全局变量
局部变量
函数库
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论