【Linux】bash shell scripts
来源:互联网 发布:unity3d人物控制脚本 编辑:程序博客网 时间:2024/06/05 14:23
更新中….
变量
变量以命名与C语言类似,声明时无数据类型,使用时需要带上$符号,bash会自动识别数据类型(主要是整型和字符串)。
特殊变量:
$?——表示上一条指令的结果,执行成功时为0
$0——在shell中表示shell的名字
$1——表示shell的第一个参数
$2——第二个参数(在shell中你希望知道用户输入的参数时直接使用该变量表示)
…..
表达式:
&& || ;——命令结果判断
这三个符号通过前一条命令的结果来判断是否执行下一条命令:
;——无论如何都执行下一条命令,也就是用于分割两条命令的符号
&&——前一条命令执行成功才执行下一条命令
||——前一条命令执行失败才执行下一条命令
执行成功与否,看命令返回值是否为0,可以用echo $?来查看,为0时成功,否则失败。
如当存在/lx这个目录时,在其下创建一个test文件,否则先创建文件件再创建文件:
ls /lx && touch /lx/test || mkdir /lx;touch /lx/test
test——文件属性检查
test 【参数】【文件、目录名】
常用参数:
-e:文件或目录是否存在
-f:是否为file
-d:是否为director
另外,可以使用test
[ ]——判断条件
[ $vatiate1 == $vatiate2 ] 判断变量是否相同
[ $vatiate1 == $vatiate2 ]判断变量是否不同
[ $vatiate1 == $vatiate2 -o $vatiate3 == $vatiate4 ] -o表示or
还可以是:
-eq —比较两个参数是否相等(例如,if [ 2 –eq 5 ])
-ne —比较两个参数是否不相等
-lt —参数1是否小于参数2
-le —参数1是否小于等于参数2
-gt —参数1是否大于参数2
-ge —参数1是否大于等于参数2
-f — 检查某文件是否存在(例如,if [ -f “filename” ])
-d — 检查目录是否存在
需要注意的是,这个命令的所有部分都必须以空格分开。
算术表达式
var1=$((var2 运算符 var3))
运算符可以是:+ - * /
bash只支持简单的整数运算,浮点运算可以使用计算程序bc,具体用法找man。
注意:双括号,左操作数不需要带$,相当于变量的声明。注意使用*和/的时候要使用转义符号\
sctripts执行控制:
if——分支语句
if [判断条件]; then todoelif [判断条件]; then todoelse todofi
判断条件使用上面说到的[ ]来表示,可以使用&&和||来将多个[ ]连起来。
#/bin/bashread -p "please input a num:" numecho 'the number you inputed is' $numif [ "$num" == "1" ];then echo "yes,you inputed $num"elif [ "$num" == "2" ];then echo "yes,you inputed $num"else echo "you inputed other number $num"fi
[lx@localhost scripts]$ sh test.sh please input a num:2 the number you inputed is 2 yes,you inputed 2
case——分支语句
case $变量名 in "case1") //引号和后括号 todo ;; //两个分号 "case2") todo ;; "case3") todo ;; *) //表示以上结果都不是 else todo exit 1 ;;esac
while…do——while循环
语法:
while [ judgment statement ]do execute statementdone
表示当judement statement为真(>0)时进入循环,为假时跳出。
until…do——until循环
语法:
until [ judement statement ]do execute statementdone
表示当judement statement成立时跳出循环,否则继续循环。
一个综合的例子:
#/bin/bash#edit by liixang#echo 'learn loop statement'##read string from keyboardread -p 'please input a number:' numecho "the number you inputed is $num"##if elseif [ $num -ge 2 ] && [ $num -lt 5 ]; then echo "2<=num<5"elif [ $num -ge 5 ]; then echo "num>=5"else echo "num < 2"fi##while dowhile [ "$num" != "0" ]do echo "while do loop counter:$num" num=$(($num-1))done##until doread -p "input another number:" num1echo "you inputed: $num1"until [ "$num1" -eq 0 ]do echo "until do loop counter:$num1" num1=$(($num1-1))done##for doread -p "input another number:" num2echo "you inputed :$num2"for (( i=1; i<=$num2;i=i+1 ))do echo "for do loop counter:$i"done
输出结果为:
[lx@localhost scripts]$ sh whiledo.sh learn loop statement please input a number:2 the number you inputed is 2 2<=num<5 while do loop counter:2 while do loop counter:1 input another number:2 you inputed: 2 until do loop counter:2 until do loop counter:1 input another number:2 you inputed :2 for do loop counter:1 for do loop counter:2
shell脚本的执行:
[root@linux ~]# sh [-nvx] scripts.sh参数:-n :不要执行 script,仅查询语法的问题;-v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上;-x :将使用到的 script 内容显示到屏幕上,这是很有用的参数!
- 【Linux】bash shell scripts
- Writing Robust Bash Shell Scripts
- Linux学习笔记 --Bash Scripts
- linux shell scripts
- LINUX(4) shell scripts
- Using Bash shell scripts for function testing
- Bash Scripts
- 【脚本】Useful collection of linux bash scripts
- Linux & Shell (2) Write basic shell scripts
- Bash Special Parameters Explained with 4 Example Shell Scripts
- A quick guide to writing scripts using the bash shell
- Linux Bash Shell
- Linux Bash Shell 快捷键
- Linux Bash Shell
- Linux Bash Shell入门教程
- 【Linux基础】Bash Shell
- Linux Bash Shell入门教程
- Linux Bash Shell入门教程
- 设计并发服务器,使用多进程与多线程有什么区别?
- 在Windows的CMD中如何设置支持UTF8编码
- 通知
- 关于Android studio 报错:[clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mo
- python进阶强化-5
- 【Linux】bash shell scripts
- Python分词练习
- LeetCode——627. Swap Salary(数据库,sql)
- UVA156 Ananagrams(字符串处理)
- 2017杭电多校联赛第三场-RXD and dividing (hdu6060)最小生成树
- [Unity]调用GL.IssuePluginEvent()后dll中UnityRenderEvent()不执行的问题
- ocp Oracle.1z0-053 711--123
- HDU 3038 带权并查集
- HDU 6051原根 + 欧拉函数