shell脚本编程(三)——变量、常量和函数
来源:互联网 发布:人工智能论文2000字 编辑:程序博客网 时间:2024/05/21 03:57
shell script 变量、常量和函数
函数
shell函数有两种语法:
- 第一种
function name{ commands return}
- 第二种
name (){ commands return}
这两种格式等价,可以交替使用。
例1:应用函数
#!/bin/bash#shell function demofunction funct{ echo "step 2" return}#Main program starts hereecho "step 1"functecho "step 3"
将输出
step 1step 2step 3
下面给我们的最小的网页添加一些shell函数
#!bin/bash#program to output a system information pageTITLE=System Information Report For $HOSTNAMECURRENT_TIME=$(data +"%x")TIME_STAMP="Generate $CURRENT_TIME,by $USER"report_uptime(){ return}report_disk_space(){ return}report_home_space(){ return}cat << EOF<HTML> <HEAD> <TITLE>$TITLE</TITLE> </HEAD> <BODY> <H1>$TITLE</H1> <P>$TIME_STAMP</P> $(report_uptime) $(report_disk_space) $(report_home_space) </BODY></HTML>EOF
全局变量与局部变量
定义在函数外部(即不属于任何一个函数的变量)为全局变量
定义在某个函数内部,且由local标记的变量为局部变量。
例2:
#!bin/bash#local and global varsfoo=0 #global variable foofunct() { local foo foo=1 echo "funct_1: foo=$foo"}echo "global: foo=$foo"funct
可以看到
global: foo=0
funct_1: foo=1
0 0
- shell脚本编程(三)——变量、常量和函数
- Linux Shell 脚本编程(2)—变量(本地变量&环境变量)和运算符
- shell脚本编程(三)
- shell脚本编程(三)
- Shell脚本编程-变量
- Shell脚本编程-变量
- Shell脚本入门(三) - 变量
- 变量、常量和函数
- shell编程三~变量
- Shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- Shell中脚本变量和函数变量的作用域
- 30天自制操作系统-第2天
- 数组
- 1+2+3+...+100 不允许使用乘法和除法,条件分支循环等
- 阿里巴巴2014秋招笔试题
- Leetcode 435. Non-overlapping Intervals题解
- shell脚本编程(三)——变量、常量和函数
- C++ 应用程序无法正常启动程序0xc00007b
- codevs 3955 最长严格上升子序列(加强版)(DP)
- LeetCode No.106 Construct Binary Tree from Inorder and Postorder Traversal
- Activity1
- 总结
- TCP 的那些事儿(下)
- JS 提交form表单
- Charles 从入门到精通-进阶篇