shell简单编程
2016-06-10 13:17
489 查看
基础
标准shell变量(内置)
赋值与循环分支
for循环以及数组
打印
$0 表示shell文件本身的文件名. $1,$2,$3 指向每个参数 $#表示参数的个数 $*/$@表示所有参数的整体 $x 用于取变量x的值 $$ 进程id. shift移动一个参数。 ':',相当于nop,是一条空命令,while[ true ] : --> 死循环. ';',用于在一行分隔命令,if[ condition ] ; then export x=100 ;导出一个环境变量x. 命令替换:使用`A`或者$(A),将$(A)的操作结果,传给下一个调用者. eg cat $(ls) > merge // cat `ls` > merge exit val ;结束当前脚本并以val作为返回值. $? ; 返回上一条命令的执行结果. test 用于shell条件判断,if test 2 -eq 2 //不需要[] ----用于测试文件信息是否正确----- test -f fn ;fn是否为为文件 test -d fn ;fn是否为文件夹 test -r/w/x ;当前用户对该文件是否有读/写/执行权限 ----value-------------- test "sfas" == "afds" test "sf" -eq "afas" test 12 -lt 13 && 和 || 就是那个意思没错!!!!!
标准shell变量(内置)
date --> 日期 pwd --> 当前绝对工作路径
赋值与循环分支
let i=$x+$y,为整数操作赋值,所以必须有两个以上操作数.当只有一个操作数时使用,i=$x. #!/bin/bash echo "please input num:" read num echo "the num is $num" #声明变量,不能有空格 sum=0 i=0 # while循环,需要留出空格 while [ $num -gt $i ] do let mo=$i%2 # 开始条件需要留出空格 # then,else 要单独留出空行 if [ $mo -eq 0 ] then 3 赋值不能有空格 let sum=$sum+$i elif [ $mo -eq 2 ] then echo "ll" else touch $i tar cvf - `find . -mtime -1 -type f -print` > x.tar # 结束条件 fi let i=$i+1 # 结束循环 done echo "the sum is $sum"
for循环以及数组
#!bin/bash echo "my student number is :1133710513, my name is zhiwei huang" city[1]=Beijing city[2]=Shanghai city[3]=Wuxi city[4]=Wenzhou city[5]=Berkeley # for z in 1 2 7 for i in ${city[*]} do echo $i done
打印
#!/bin/bash echo "my student number is :1133710513, my name is zhiwei huang" i=0 for ((k=0;k<10;k++)) do for((j=0;j<=k;j++)) do echo -n "$j " done echo " " done
相关文章推荐
- android wifi 无线调试
- 运维入门
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Linux Shell常用技巧
- Shell 脚本编程陷阱
- shell字符串操作详解
- Shell中删除某些文件外所有文件的3个方法
- Ruby中执行Linux shell命令的六种方法详解
- VB使用shell函数打开外部exe程序的实现方法
- Shell编程的一些知识集合
- Shell中的for和while循环详细总结
- 什么是Shell?Shell脚本基础知识详细介绍
- Shell脚本中引用、调用另一个脚本文件的2种方法
- Shell脚本解压rpm软件包
- Linux Shell 数组建立及使用技巧
- Shell脚本实现复制文件到多台服务器的代码分享
- Shell脚本实现批量替换文件内容
- Shell脚本实现的一个简易Web服务器例子分享
- linux Shell学习笔记第五天