#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
#function fun1()
fun1()
{
# 判断是否存在指定的用户
N=`grep "^$1:" /etc/passwd | wc -l`
return $N;
}
if [ $# -ne 1 ]
then
echo "没有指定用户"
exit
fi
fun1 $1
RS=$?
if [ $RS -eq 1 ]
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
运行结果:
linux@ubuntu:~/Workspace/1212$ fun.sh linux
linux is exist
fun1.sh代码:
linux@ubuntu:~/Workspace/1212$ cat fun1.sh
#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
#function fun1() #定义函数的另一种方式
fun1()
{
# 判断是否存在指定的用户
N=`grep "^$1:" /etc/passwd | wc -l`
#使用return的方式返回结果,返回值是无符号整形,范围[0,255]
return $N;
}
if [ $# -ne 1 ] # [ 参数与中括号之间必须有空格' ' ]
then
echo "没有指定用户"
exit # exit 表示退出shell
fi
fun1 $1 # 调用函数
RS=$?
if [ $RS -eq 1 ] # 判断数值相等,详见 man test
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
运行结果:
linux@ubuntu:~/Workspace/1212$ fun1.sh linux
linux is exist
fun2.sh代码
linux@ubuntu:~/Workspace/1212$ cat fun2.sh
#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
fun1()
{
A=10 # 默认在函数中定义为全局变量
local B=20 # 使用local定义局部变量
N=`grep "^$1:" /etc/passwd | wc -l`
return $N;
}
if [ $# -ne 1 ]
then
echo "没有指定用户"
exit
fi
fun1 $1
RS=$? # $?获取上一条语句的执行结果,位置不能乱
echo "A is $A" # 使用该return方式进行函数调用,可以访问子函数中定义的A变量
echo "B is $B" # 不能访问局部变量B
if [ $RS -eq 1 ]
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
运行结果:
linux@ubuntu:~/Workspace/1212$ fun2.sh linux
A is 10
B is
linux is exist
fun3.sh代码:
linux@ubuntu:~/Workspace/1212$ cat fun3.sh
#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
fun1()
{
A=10 # 默认在函数中定义为全局变量
local B=20 # 使用local定义局部变量
N=`grep "^$1:" /etc/passwd | wc -l`
test $N -eq 1 # 使用test测试,成功$?为0,否则非0
if [ $? -eq 0 ] # 判断测试结果
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
return $N;
}
if [ $# -ne 1 ]
then
echo "没有指定用户"
exit
fi
fun1 $1
RS=$? # $?获取上一条语句的执行结果,位置不能乱
echo "A is $A" # 使用该return方式进行函数调用,可以访问子函数中定义的A变量
echo "B is $B" # 不能访问局部变量B
if [ $RS -eq 1 ]
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
运行结果:
linux@ubuntu:~/Workspace/1212$ fun3.sh linux
linux is exist
A is 10
B is
linux is exist
fun4.sh代码:
linux@ubuntu:~/Workspace/1212$ cat fun4.sh
#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
fun1()
{
A=10 # 默认在函数中定义为全局变量
local B=20 # 使用local定义局部变量
N=`grep "^$1:" /etc/passwd | wc -l`
test $N -eq 1 # 使用test测试,成功$?为0,否则非0
if [ $? -eq 0 ] # 判断测试结果
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
return $N
}
if [ $# -ne 1 ]
then
echo "没有指定用户"
exit
fi
RS=`fun1 $1`
echo "A is $A" # 使用该方式进行函数调用,不可以访问子函数中定义的A变量
echo "B is $B" # 不能访问局部变量B
echo $RS
运行结果:
linux@ubuntu:~/Workspace/1212$ fun4.sh linux
A is
B is
linux is exist
fun5.sh代码:
linux@ubuntu:~/Workspace/1212$ cat fun5.sh
#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
fun1()
{
A=10 # 默认在函数中定义为全局变量
local B=20 # 使用local定义局部变量
N=`grep "^$1:" /etc/passwd | wc -l`
test $N -eq 1 # 使用test测试,成功$?为0,否则非0
if [ $? -eq 0 ] # 判断测试结果
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
return $N;
}
if [ $# -ne 1 ]
then
echo "没有指定用户"
exit
fi
fun1 $1
RS=$? # $?获取上一条语句的执行结果,位置不能乱
echo "A is $A" # 使用该return方式进行函数调用,可以访问子函数中定义的A变量
echo "B is $B" # 不能访问局部变量B
if [ $RS -eq 1 ]
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
运行结果:
linux@ubuntu:~/Workspace/1212$ fun5.sh linux
linux is exist
A is 10
B is
linux is exist
fun6.sh代码:
linux@ubuntu:~/Workspace/1212$ cat fun6.sh
#!/bin/bash
#Program:
# an example for function
#History
# 2013/12/12 LXL Firstrelease
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin/:/usr/local/bin/:/usr/local/sbin/:~/bin
export PATH
fun1()
{
A=10 # 默认在函数中定义为全局变量
local B=20 # 使用local定义局部变量
N=`grep "^$1:" /etc/passwd | wc -l`
test $N -eq 1 # 使用test测试,成功$?为0,否则非0
if [ $? -eq 0 ] # 判断测试结果
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
return $N;
}
if [ $# -ne 1 ]
then
echo "没有指定用户"
exit
fi
RS=`fun1 $1`
RES=$? # $?获取上一条语句的执行结果,位置不能乱
echo "A is $A" # 使用该方式进行函数调用,不可以访问子函数中定义的A变量
echo "B is $B" # 不能访问局部变量B
if [ $RES -eq 1 ]
then
echo "$1 is exist"
else
echo "$1 is not exist"
fi
echo $RS
运行结果:
linux@ubuntu:~/Workspace/1212$ fun6.sh linux
A is
B is
linux is exist
linux is exist