is_nginx_running &&echo"Ningx is running"|| ehco "Ningx is Stop"# Ningx is running
获取系统所有的用户
#!/bin/bash## get all usernamefunction get_users
{
users=`cat /etc/passwd |cut -d : -f1`echo$users}# echo all users name
users=`get_users`
index=1
for s in$usersdoecho"The $index user is $s."
index=$(($index+1))done
变量的作用域
在shell中如果不做特殊声明,那么变量不管是在函数体内还是函数外都是全局变量
如果要在函数内使用局部变量需要使用local关键字 谨慎使用全局变量。
函数没有运行 函数体定义的全局变量函数外部或者其他函数内部访问无效。
案列
#!/bin/bash#
var1="hello world"function test1
{
var2=123
}function test2
{
local var3="local variable"echo$var2}function test3
{echo$var3}# 测试echo$var1$var2$var3# hello world
test1
test2
test3
echo$var1$var2$var3# hello world 123
函数的定义定义#!/bin/bash## method one name1(){ echo "123"}# method two function name2{ echo "123"}function name3(){ echo 123}三种方法都可以这里后面所有用到的定义都是用function name { command… }这样的方法调用# call function by namename1# 123name2# 123nam