【Linux】bash shell scripts
2017-08-02 15:54
507 查看
更新中….
变量
变量以命名与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 ]判断变量是否不同
[ $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 todo elif [判断条件]; then todo else todo fi
判断条件使用上面说到的[ ]来表示,可以使用&&和||来将多个[ ]连起来。
#/bin/bash read -p "please input a num:" num echo 'the number you inputed is' $num if [ "$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 statement < d0b9 span class="hljs-keyword">done
表示当judement statement为真(>0)时进入循环,为假时跳出。
until…do——until循环
语法:until [ judement statement ] do execute statement done
表示当judement statement成立时跳出循环,否则继续循环。
一个综合的例子:
#/bin/bash #edit by liixang # echo 'learn loop statement' ##read string from keyboard read -p 'please input a number:' num echo "the number you inputed is $num" ##if else if [ $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 do while [ "$num" != "0" ] do echo "while do loop counter:$num" num=$(($num-1)) done ##until do read -p "input another number:" num1 echo "you inputed: $num1" until [ "$num1" -eq 0 ] do echo "until do loop counter:$num1" num1=$(($num1-1)) done ##for do read -p "input another number:" num2 echo "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-SHELL中sh和bash的区别
- LINUX BASH SHELL位置变量与特殊变量参数
- Linux Bash Shellshock software bug-2014.09.24
- linux bash shell常用文件操作命令
- linux bash shell中,单引号、 双引号,反引号(``)的区别及各种括号的区别
- linux bash shell 入门教程(转载)
- Using Bash shell scripts for function testing
- Linux Bash shell (for循环)
- 8.17_Linux之bash shell脚本编程入门篇(三)之循环以及函数function的使用
- linux复习笔记之bash shell (4)管道命令
- linux shell bash 内置变量参考
- linux在shell中[root@]出现-bash-
- Linux学习日记 —— 10.4.2 Shell基础-Bash变量-环境变量
- Linux Bourne shell/Bash shell的test命令
- Linux的Bash Shell详解
- How to Manually Update Bash to Patch Shellshock Bug on Older Fedora-Based Linux Systems
- Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出
- [Linux]:Bash Shell的字符串匹配方法
- linux shell bash 比较操作
- Linux Bash Shell 快捷键