本文由larrylgq编写,转载请注明出处:http://blog.csdn.net/larrylgq/article/details/7395261
作者:吕桂强
邮箱:larry.lv.word@gmail.com
变量:
aaa="lv";不要空格
echo $aaa->lv
echo ${aaa}txt->lvtxt
echo $aaa.txt->lv.txt
求值:
a=1
b=2
1:let:
let c=a+b
echo $c
3
2:$[]:
d=$[ a + b ]
echo $d
3
3:$(()):
e=$(( a + b ))
echo $e
3
条件判断:
1:数值判断:
-ne -lt -lessthan -gt -le
if [ $a -gt 0 ]; then
> echo hahaha
> fi
hahaha
2:字符串判断:
a=a1
b=b1
if [[ $a == $b ]]; then
!= <= >= -z(是不是空) -n(是否非空)
> echo test
> else echo ok
> fi
ok
3:其它
if [[ -n $b ]]; then echo test; else echo ok; fi
-n是否为空
-f是否文件
-d是否目录
-x是否可执行
-e是否存在
-w是否可写
-r是否可读
-l是否link
if [ -f lv ];then echo yes; else echo no; fi
if [ -d lv ];then echo yes; else echo no; fi
a=zookeeper
if [ -f $a ];then echo yes; else echo no; fi
yes
注意:[]里面都要有空格
循环:
1:for ((i=0;i<100;i++)){ echo $i;}
;echo之前要有空格
2:for ((i=0;i<100;i++));do echo $i;done
3:for i in {1..100};do echo $i;done
4:echo {1..100}
echo {a..z}
;结果没有换行
5:新建1.text,在1.text中输入数字
for i in `cat 1.txt`; do echo $i; done
实战1:copy20个2.csv到root@192.168.1.208:~/分别改名为201-220,然后删除
for ((i=1;i<10;i++));do scp 2.csv root@192.168.1.208:~/20$i.csv;done
for ((i=10;i<=20;i++));do scp 2.csv root@192.168.1.208:~/2$i.csv;done
或者
for ((i=1;i<=20;i++));do if [[ $i -lt 10 ]]; then scp 2.csv root@192.168.1.208:~/n20$i.csv; else scp 2.csv root@192.168.1.208:~/n2$i.csv;fi;done
ssh root@192.168.1.208
for ((i=1;i<=20;i++));do if [[ $i -lt 10 ]]; then rm ~/20$i.csv; else rm ~/2$i.csv;fi;done