如何在Shell脚本中使用函数
#!/bin/sh
hello()
{
echo “Hello there today ‘s date is `date`”
}
echo “now going to the function hello ”
hello
echo “back from the function ”
运行脚本func1,结果为:
$func1
now going to the function hello
Hello there today ‘s date is Fri Jul 22 09:05:37 BEIJING 2005
Back from the function
上面例子中,函数定义于脚本顶部。可以在脚本中使用函数名hello调用它。函数执行后,控制返回函数调用的下一条语句,即反馈语句back from the function。
从调用函数中返回
当函数完成处理或希望函数基于某一测试语句返回时,可做两种处理:
1) 让函数正常执行到函数末尾,然后返回脚本中调用函数的控制部分。
2) 使用return返回脚本中函数调用的下一条语句,可以带返回值。0为无错误,1为有错误。
函数返回值测试
可以直接在脚本调用函数语句的后面使用最后状态命令来测试函数调用的返回值。例如:
check_it_is_a_directory $FILENAME
if [$? = 0]
then
echo “All is OK”
else
echo “Something went wrong!”
fi
hello()
{
echo “Hello there today ‘s date is `date`”
}
echo “now going to the function hello ”
hello
echo “back from the function ”
运行脚本func1,结果为:
$func1
now going to the function hello
Hello there today ‘s date is Fri Jul 22 09:05:37 BEIJING 2005
Back from the function
上面例子中,函数定义于脚本顶部。可以在脚本中使用函数名hello调用它。函数执行后,控制返回函数调用的下一条语句,即反馈语句back from the function。
从调用函数中返回
当函数完成处理或希望函数基于某一测试语句返回时,可做两种处理:
1) 让函数正常执行到函数末尾,然后返回脚本中调用函数的控制部分。
2) 使用return返回脚本中函数调用的下一条语句,可以带返回值。0为无错误,1为有错误。
函数返回值测试
可以直接在脚本调用函数语句的后面使用最后状态命令来测试函数调用的返回值。例如:
check_it_is_a_directory $FILENAME
if [$? = 0]
then
echo “All is OK”
else
echo “Something went wrong!”
fi
【如何在Shell脚本中使用函数】相关文章
2. shell程序如何生命变量?shell变量是弱变量吗?
4. ShellScript面试题一则-ShellScript编程
6. Spring对多种ORM框架提供了很好的支持,简单描述在Spring中使用Hibernate的方法
8. shell程序中如何注释
本文来源:https://www.mianshiwenti.com/a13624.html
进入下载页面
上一篇:linux系统都有哪些运行级别
下一篇:linux面试题参考答案(5)
﹝如何在Shell脚本中使用函数﹞相关内容
- shell变量的作用空间是什么
- Shell编程面试题
- shell的种类有哪些
- 如果NULL定义成#define NULL((char *)0)难道不就可以向函数传入不加转换的NULL了吗
- 软件测试LoadRunner面试题:Explain the following functions: – lr_debug_message
- What\'s the difference between deep copy and shallow copy? (深拷贝与浅拷贝有什么区别)
- 软件测试LoadRunner面试题:What is correlation? Explain the difference between automatic correlation and manu
- 软件测试LoadRunner面试题:If web server, database and Network are all fine where could be the problem?
- 我是如何在Stack Overflow找到工作的
- 如何在spring中实现国际化