[[email protected] ~]# cd /install/
[[email protected] install]# mkdir -p test && cd test
-s修改时间:
[[email protected] test]# date
2017年 11月 30日 星期四 21:55:03 CST
[[email protected] test]# date `+%F %T %A`
2017年 11月 30日 星期四 21:56:25 CST
[[email protected] test]# date ‘+%F‘
2017-11-30
[[email protected] test]# date ‘+%T‘
21:57:11
[[email protected] test]# date ‘+%A‘
星期四
[[email protected] test]# date -s "2017-11-30 14:44:40"
2017年 11月 30日 星期四 14:44:40 CST
更新主板和芯片时间
[[email protected] test]# clock -w
[[email protected] test]# date ‘+%F %T %A‘
2017-11-30 14:45:47 星期四
[[email protected] test]# myname=lisi
[[email protected] test]# echo $myname
lisi
[[email protected] test]# echo my neme is $myname
my neme is lisi
[[email protected] test]# echo $mynameis man
man
[[email protected] test]# echo ${myname}is man
lisiis man
说一些指令
小括号()和反单引号效果相似:
[[email protected] test]# echo `echo kkk`
kkk
[[email protected] test]# echo $(echo kkk)
kkk
数字表示的是下标,下标是从0开始:
[[email protected] test]# echo ${#myname}
4
[[email protected] test]# echo ${myname:3}
i
[[email protected] test]# echo ${myname:4}
单斜线是替换第一次匹配结果,双斜线是全部替换:
[[email protected] test]# echo ${myname/si/gang}
ligang
[[email protected] test]# echo ${myname/si/}
li
[[email protected] test]# myname=liyongfuyongfu
[[email protected] test]# echo ${myname/yongfu/gang}
ligangyongfu
[[email protected] test]# echo ${myname//yongfu/gang}
liganggang
%表示匹配最后一个;#表示匹配第一个:
[[email protected] test]# echo ${myname/%yongfu/gang}
liyongfugang
[[email protected] test]# echo ${myname/#yongfu/gang}
liyongfuyongfu
[[email protected] test]# echo ${myname/#li/gang}
gangyongfuyongfu
echo输出会换行;printf不会换行:
[[email protected] test]# printf kkk
kkk[[email protected] test]# echo printf
printf
:-没值时不会自动赋值;而:=没值时会自动赋值;但没值时都会输出默认值;
[[email protected] test]# kk=liyongfu
[[email protected] test]# echo ${kk:-ligang}
liyongfu
[[email protected] test]# echo ${km}
[[email protected] test]# echo ${km:-ligang}
ligang
[[email protected] test]# echo ${km}
[[email protected] test]# echo ${kl:=ligang}
ligang
[[email protected] test]# echo ${kl}
ligang
数学运算:
(()):
[[email protected] test]# sum=0
[[email protected] test]# ((sum=sum+10))
[[email protected] test]# echo $sum
10
[[email protected] test]# ((sum = sum + 10))
[[email protected] test]# echo $sum
20
[[email protected] test]# a=10
[[email protected] test]# b=20
[[email protected] test]# c=+
[[email protected] test]# ((sum = ${a} ${c} ${b}))
[[email protected] test]# echo $sum
30
[[email protected] test]# ex=3+3-5*0/5
[[email protected] test]# echo $ex
3+3-5*0/5
[[email protected] test]# ((sum = $ex))
[[email protected] test]# echo $sum
6
数字比较用-gt/-lt/-eq/-ge/-le/-ne;
字符串比较>、
[[email protected] test]# [ 3>2 ] && echo "yes" || echo "no"
no
[[email protected] test]# [ 3 > 2 ] && echo "yes" || echo "no"
yes
[[email protected] test]# [ 3 < 2 ] && echo "yes" || echo "no"
yes
[[email protected] test]# [ 3 -gt 2 ] && echo "yes" || echo "no"
yes
[[email protected] test]# [ 3 -lt 2 ] && echo "yes" || echo "no"
no
[[email protected] test]# [ 3 -ge 2 ] && echo "yes" || echo "no"
yes
[[email protected] test]# [ 3 -le 2 ] && echo "yes" || echo "no"
no
[[email protected] test]# [ 3 -eq 2 ] && echo "yes" || echo "no"
no
[[email protected] test]# [ 3 -ne 2 ] && echo "yes" || echo "no"
yes
[[email protected] test]#
[[email protected] test]#
[[email protected] test]# he=ligang
[[email protected] test]# wo=yongfu
[[email protected] test]# [ $he == $wo ] && echo "yes" || echo "no"
no
[[email protected] test]# [ $he != $wo ] && echo "yes" || echo "no"
yes
-f判断文件是否存在;-d判断文件夹是否存在;
[[email protected] test]# [ -f "$fp" ] && echo "exists" || echo "not exists"
[[email protected] test]# echo this is a txt>>a.txt
[[email protected] test]# cat a.txt
this is a txt
[[email protected] test]# [ -f "$fp" ] && cat a.txt || touch a.txt
this is a txt
[[email protected] test]# [ -d "$fp" ] && echo "exists" || echo "not exists"
(1):
[[email protected] test]# vi test.sh
[[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
fp=/install/test/a
[ -d $fp ] && {
echo "dir is exists";
echo "dir is exists"
}||{
echo "dir not exists";
echo "dir not exosts"
}
[[email protected] test]# sh test.sh
(2):
[[email protected] test]# vi test.sh
[[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
fp=/install/test/a
if [ -d $fp ];then
echo "dir is exists";
echo "dir is exists"
else
echo "dir not exists";
echo "dir not exost"
fi
[[email protected] test]# sh test.sh
(3):固定变量值
[[email protected] test]# vi test.sh
[[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
age=90
if [ $age -gt 100 ];then
echo "他是老年人"
elif [ $age -gt 60 ];then
echo "他是中老年人"
elif [ $age -gt 40 ];then
echo "他是青年人"
elif [ $age -gt 20 ];then
echo "他是少年"
else
echo "他是小孩"
fi
[[email protected] test]# sh test.sh
他是中老年人
(4):运行时,传值
[[email protected] test]# vi test.sh
[[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
#第一个参数是$0,它表示当前脚本文件的名字,从数字1开始(即$1,$2...)表示传给脚本文件的名字
fileName=$0
echo "script file is $fileName"
age=$1
if [ $age -gt 100 ];then
echo "他是老年人"
elif [ $age -gt 60 ];then
echo "他是中老年人"
elif [ $age -gt 40 ];then
echo "他是青年人"
elif [ $age -gt 20 ];then
echo "他是少年"
else
echo "他是小孩"
fi
[[email protected] test]# sh test.sh 35
script file is test.sh
他是少年
序列:
[[email protected] test]# echo {1..5}
1 2 3 4 5
[[email protected] test]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[[email protected] test]# echo {A..h}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _ ` a b c d e f g h
[[email protected] test]# seq 5
1
2
3
4
5
tr截断设置分隔符;
[[email protected] test]# seq 5|tr ‘\n‘ ‘ ‘
1 2 3 4 5 [[email protected] test]# seq -s ‘ ‘ 5
1 2 3 4 5
-s直接设置分割方式;
[[email protected] test]# seq -s ‘ ‘ 5 50
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
1 2 50其中的1表示起始数字,5表示步长,50表示结束数字;
[[email protected] test]# seq -s ‘ ‘ 1 5 50
1 6 11 16 21 26 31 36 41 46
(())只能运算整数;bc可以运算所有实数;
其中的-l表示导入运行计算所需包;bc是运算标识;
[[email protected] test]# a=1.2
[[email protected] test]# b=2.6
[[email protected] test]# sum=0
[[email protected] test]# ((sum=a+b))
[[email protected] test]# echo ${a}+${b}|bc -l
3.8
[[email protected] test]# echo 190+100|bc -l
290
整数计算推荐使用(()),因为性能比较好;
bc中间使用了管道,走了磁盘IO,性能不好;
[[email protected] test]# seq -s ‘+‘ 100
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[[email protected] test]# seq -s ‘+‘ 100|bc -l
5050
反单引号内的内容当作语句执行;
单引号内的内容当作字符串;
[[email protected] test]# ((sum=`seq -s ‘+‘ 1000`))
[[email protected] test]# echo $sum
500500
[[email protected] test]# echo $((`seq -s ‘+‘ 1000`))
500500
[[email protected] test]# echo $((seq -s ‘+‘ 1000))
-bash: seq -s ‘+‘ 1000: syntax error: invalid arithmetic operator (error token is "‘+‘ 1000")
for循环两种方式:
[[email protected] test]# sum=0
[[email protected] test]# for i in `seq 100`;do ((sum=${sum}+${i}));done
[[email protected] test]# echo $sum
[[email protected] test]# for((i=1;i<=100;i++));do ((sum+=${i}));done
[[email protected] test]# echo $sum
while的两种方式,注意每次需要坝变量赋值:
[[email protected] test]# sum=0
[[email protected] test]# i=1
[[email protected] test]# while [ $i -le 100 ];do ((sum+=i));((i++));done
[[email protected] test]# echo $sum
[[email protected] test]# sum=0
[[email protected] test]# i=1
[[email protected] test]# while ((i <= 100));do ((sum+=i));((i++));done
[[email protected] test]# echo $sum