$ echo $((10 + 20))# 30$ one=25$ two=12$ echo $((one+two))# 37$ echo $((one>two))# 1$ echo $((one<two))# 0$ echo $(expr $one+$two)# 25+12$ echo $(expr $one + $two)# 37比较大小需要转义
$ echo $(expr $one \> $two)$ echo $(expr $one \< $two)&&||-f判断输入内容是非为一个文件-d判断输入内容是非为一个目录-x判断输入内容是非可执行-e判断文件是非存在-eq相等-gt大于-lt小于-ne不等于==!=test和 []效果一致
$ [ 1 = 2 ] && echo "条件不成立"$ [ 1 = 2 ] || echo "条件不成立"$ test 1 = 2 && echo "条件不成立"$ test 1 = 2 || echo "条件不成立"查看使用方法
$ man [ ]$ man test#!/bin/bash#Author:zhongxin.shexit返回非零值,来退出程序# 第一种方式$ bash 1126.sh# 第二种方式$ chmod +x 1126.sh$ ./1126.sh# 第三种方式$ source 1126.sh##!/bin/bash#Author:zhongxin:<<!要注释的内容!pwd#!/bin/bash函数名(){ ...}#!/bin/bashfunction 函数名{ ...}#!/bin/bash#Author:zhongxin:<<!要注释的内容!hello(){ echo "Hello,Shell"}hellofunction greet{ echo "Hello,zx"}greetfunction greet{ echo "Hello,$1" echo "Hello,$2" echo "Hello,$3"}greet 'a' 'b' 'c'接收命令行传参
greet $1 $2 $3#!/bin/bash#Author:zhongxinread -p "请输入你的姓名:" nameread -p "请输入你的年龄:" ageread -p "请输入你的爱好:" function info{ # -e 运行\n换行 echo -e "你的姓名为:${name}\n你的年龄为:${age}\n你的爱好为:${REPLY}"}infoif [ 条件1 ]then 指令1elif [ 条件2 ]then 指令2else 指令2fi例子:
#!/bin/bash#Author:zhongxinread -p "输入你的年龄:"if [ ${REPLY} -ge 70 ]then echo "好好养生"elif [ ${REPLY} -ge 18 ]then echo "好好工作"else echo "好好学习"fifor 值 in 列表do 执行语句done例子
dirs=$(ls /)for i in ${dirs}do index=$((${index} + 1)) echo "${index}.${i}"donemax=10for ((i=1;i<=max;i++))do 执行语句done例子
#!/bin/bash#Author:zhongxinmax=10for ((i=0;i<=max;i++))do echo "i=${i}"done