Shell 函数
来源:互联网 发布:淘宝售后工作职责 编辑:程序博客网 时间:2024/05/16 06:57
基本的函数概念
函数定义menu()
{
echo "\$1 is $1"
echo "this is $2"
reture 9
函数内内语句也无分号;
}函数调用menu aa bb
和C语言不同,执行函数不带括号()函数返回值menu aa bb
result=$?
echo "$result”
函数名中间不能带杠
#!/bin/bash
verify-para()
{
}
[macg@machome ~]$ sh test.sh
test.sh: line 18: `verify-para': not a valid identifier
shell函数定义必须在函数调用之前,因为shell是个顺序解释程序
#!/bin/bash
verifypara调用
host=$1
user=$2
passwd=$3
verifypara()
{
if (( $# != 3 )); then
echo "usage: $0 $host $user $passwd" 定义
exit 1
fi
}[macg@machome ~]$ sh test.sh 192.168.1.12 macg 008421
test.sh: line 3: verifypara: command not found
注意函数退出用return,不能用exit,exit是直接退出程序
exit 不止退出函数,而是直接退出shell script,
不能用中括号条件表示式在if语句中引用函数,必须用命令/函数直接作为条件的方式在if语句中引用函数
这种方式在C语言中支持,但shell脚本中不支持
if [ getyn=1 ]
then
echo " your answer is no "
else
echo "your anser is yes "
fi[macg@mac-home ~]$ sh test.sh
input the num:
3
your answer is no根本没执行函数getyn
也不能用赋值语句引用函数
yn=getyn
if [ yn=1 ]
then
echo " your answer is no "
else
echo "your anser is yes "
fi[macg@mac-home ~]$ sh test.sh
input the num:
3
your answer is no根本没执行函数
简单的说,函数就只能象command一样被直接执行,不能放在条件表达式或赋值语句里
Shell的函数参数就和命令行参数一样,也是$1,$2,$n
menu()
{
echo "\$1 is $1"
echo "this is $2"
reture 9
}
menu aa bb[root@machome macg]# sh test.sh
$1 is aa
this is bb 当调用一个函数时,shell主程序的参数,$*,$@,$#,$1,$2等等将会被函数中的参数所替换.即函数中,$1,$2,$3是函数参数,$#是参数个数
总之,函数和command几乎一模一样
只能直接执行,不能用在赋值语句或if表达式里
函数参数和命令行参数格式相同
也有返回值,返回值变量也是$?
{
echo "\$1 is $1"
echo "this is $2"
reture 9
函数内内语句也无分号;
}
和C语言不同,执行函数不带括号()
result=$?
echo "$result”
#!/bin/bash
verify-para()
{
}
[macg@machome ~]$ sh test.sh
test.sh: line 18: `verify-para': not a valid identifier
#!/bin/bash
verifypara
host=$1
user=$2
passwd=$3
verifypara()
{
if (( $# != 3 )); then
fi
}
test.sh: line 3: verifypara: command not found
exit 不止退出函数,而是直接退出shell script,
这种方式在C语言中支持,但shell脚本中不支持
if [ getyn=1 ]
then
echo " your answer is no "
else
echo "your anser is yes "
fi[macg@mac-home ~]$ sh test.sh
input the num:
3
your answer is no
yn=getyn
if [ yn=1 ]
then
echo " your answer is no "
else
echo "your anser is yes "
fi[macg@mac-home ~]$ sh test.sh
input the num:
3
your answer is no
简单的说,函数就只能象command一样被直接执行,不能放在条件表达式或赋值语句里
menu()
{
echo "\$1 is $1"
echo "this is $2"
reture 9
}
menu aa bb
$1 is aa
this is bb
- [shell]shell函数
- shell函数
- Shell 函数
- Shell 函数
- shell函数
- shell --函数
- shell 函数
- shell函数
- shell函数
- 【shell】函数
- shell 函数
- shell---函数
- Shell 函数
- shell函数
- shell 函数
- Shell 函数
- shell函数
- Shell 函数
- SPOJ 1182. Sorted bit squence (数位统计+二分)
- shell 编程——for in 循环
- 我对linux理解之framebuffer
- C++笔记(三)重载操作符
- MySQL Partition扫盲
- Shell 函数
- 卜算子之BIOS&&DOS中断向量表
- Shell include文件
- 超棒的跨浏览器纯CSS动画实现 - Animate.css
- 利用线性表压缩稀疏矩阵
- shell里的` ` $( ) ${ } expr $(( ))
- 关于Service的启动。。
- Shell脚本里常见的一些指令和函数
- Irrlicht引擎源码剖析——第十八天