0.创建shell脚本 ,及运行方法
创建:vim xxx.sh 脚本的第一行为: #/bin/bash
运行: bash xxx.sh
1.变量的定义
#!/bin/bash
#变量的定义
#注意:不能像其他编程语言那样留有空格
A=123
B=456
#引用,echo是输出命令
$A
$B
echo "the value of A is $A"
#数字变量
$0 为脚本的名称
$1,...,$n 为执行脚本的外部参数,如 bash xxx.sh 123 456 789
则脚本获取到 $1=123 $2=456 $3=789
2.if条件判断
#!/bin/bash
A= 123
if (($A>100));then
echo "the value of A is $A"
else
echo "error"
fi
3.For循环
#!/bin/bash
# ` `命令替换,先执行` `的内容 ,等价于 $(seq 1 15)
for i in `seq 1 15`
do
echo "$i"
done
#--------------------
j=0
for ((i=1;i<=100;i++))
do
let j=`expr $i+$j`
done
4.While循环
#!/bin/bash
i=0
#注意空格
while [ $i -le 10 ]
do
echo "$i"
i++
done
5.Until
#!/bin/bash
i = 0
Until [ $i -ge 10 ]
do
echo "$i"
i++
done
5.逻辑符号
大于 -gt greater than
小于 -lt less than
大于或等于 -ge greater than or equal
小于或等于 -le less than or equal
不相等 -ne not euqal
6.计算
#!/bin/bash
#shell默认变量都为字符串,要做数值计算,有几个方式
#1. expr
a=10
b=100
expr $a + $b
#注意空格
#2.$(())
$(($a+$b))
#3.$[]
$[$a+$b]
#$4.let
let c=$a+$b