1. 程式人生 > >Linux命令-基本變量類型及其運算

Linux命令-基本變量類型及其運算

file 全部 數字 dir mkdir 計算 目錄 script 設置

[root@Redis01 ~]# cd /install/
[root@Redis01 install]# mkdir -p test && cd test

-s修改時間:
[root@Redis01 test]# date
2017年 11月 30日 星期四 21:55:03 CST
[root@Redis01 test]# date `+%F %T %A`
2017年 11月 30日 星期四 21:56:25 CST
[root@Redis01 test]# date ‘+%F‘
2017-11-30
[root@Redis01 test]# date ‘+%T‘
21:57:11
[root@Redis01 test]# date ‘+%A‘
星期四
[root@Redis01 test]# date -s "2017-11-30 14:44:40"
2017年 11月 30日 星期四 14:44:40 CST
更新主板和芯片時間
[root@Redis01 test]# clock -w
[root@Redis01 test]# date ‘+%F %T %A‘
2017-11-30 14:45:47 星期四


[root@Redis01 test]# myname=lisi
[root@Redis01 test]# echo $myname
lisi
[root@Redis01 test]# echo my neme is $myname
my neme is lisi
[root@Redis01 test]# echo $mynameis man
man
[root@Redis01 test]# echo ${myname}is man
lisiis man


說一些指令
小括號()和反單引號效果相似:
[root@Redis01 test]# echo `echo kkk`
kkk
[root@Redis01 test]# echo $(echo kkk)
kkk


數字表示的是下標,下標是從0開始:
[root@Redis01 test]# echo ${#myname}
4
[root@Redis01 test]# echo ${myname:3}
i
[root@Redis01 test]# echo ${myname:4}


單斜線是替換第一次匹配結果,雙斜線是全部替換:
[root@Redis01 test]# echo ${myname/si/gang}
ligang
[root@Redis01 test]# echo ${myname/si/}
li
[root@Redis01 test]# myname=liyongfuyongfu
[root@Redis01 test]# echo ${myname/yongfu/gang}
ligangyongfu
[root@Redis01 test]# echo ${myname//yongfu/gang}
liganggang


%表示匹配最後一個;#表示匹配第一個:
[root@Redis01 test]# echo ${myname/%yongfu/gang}
liyongfugang
[root@Redis01 test]# echo ${myname/#yongfu/gang}
liyongfuyongfu
[root@Redis01 test]# echo ${myname/#li/gang}
gangyongfuyongfu


echo輸出會換行;printf不會換行:
[root@Redis01 test]# printf kkk
kkk[root@Redis01 test]# echo printf
printf


:-沒值時不會自動賦值;而:=沒值時會自動賦值;但沒值時都會輸出默認值;
[root@Redis01 test]# kk=liyongfu
[root@Redis01 test]# echo ${kk:-ligang}
liyongfu
[root@Redis01 test]# echo ${km}

[root@Redis01 test]# echo ${km:-ligang}
ligang
[root@Redis01 test]# echo ${km}

[root@Redis01 test]# echo ${kl:=ligang}
ligang
[root@Redis01 test]# echo ${kl}
ligang

數學運算:
(()):
[root@Redis01 test]# sum=0
[root@Redis01 test]# ((sum=sum+10))
[root@Redis01 test]# echo $sum
10
[root@Redis01 test]# ((sum = sum + 10))
[root@Redis01 test]# echo $sum
20

[root@Redis01 test]# a=10
[root@Redis01 test]# b=20
[root@Redis01 test]# c=+
[root@Redis01 test]# ((sum = ${a} ${c} ${b}))
[root@Redis01 test]# echo $sum
30

[root@Redis01 test]# ex=3+3-5*0/5
[root@Redis01 test]# echo $ex
3+3-5*0/5
[root@Redis01 test]# ((sum = $ex))
[root@Redis01 test]# echo $sum
6


數字比較用-gt/-lt/-eq/-ge/-le/-ne;
字符串比較>、<、==、!=
[root@Redis01 test]# [ 3>2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 > 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 < 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 -gt 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 -lt 2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 -ge 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 -le 2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 -eq 2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 -ne 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]#
[root@Redis01 test]#
[root@Redis01 test]# he=ligang
[root@Redis01 test]# wo=yongfu
[root@Redis01 test]# [ $he == $wo ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ $he != $wo ] && echo "yes" || echo "no"
yes


-f判斷文件是否存在;-d判斷文件夾是否存在;
[root@Redis01 test]# [ -f "$fp" ] && echo "exists" || echo "not exists"

[root@Redis01 test]# echo this is a txt>>a.txt
[root@Redis01 test]# cat a.txt
this is a txt
[root@Redis01 test]# [ -f "$fp" ] && cat a.txt || touch a.txt
this is a txt

[root@Redis01 test]# [ -d "$fp" ] && echo "exists" || echo "not exists"

(1):
[root@Redis01 test]# vi test.sh
[root@Redis01 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"
}
[root@Redis01 test]# sh test.sh

(2):
[root@Redis01 test]# vi test.sh
[root@Redis01 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
[root@Redis01 test]# sh test.sh

(3):固定變量值
[root@Redis01 test]# vi test.sh
[root@Redis01 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
[root@Redis01 test]# sh test.sh
他是中老年人

(4):運行時,傳值
[root@Redis01 test]# vi test.sh
[root@Redis01 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
[root@Redis01 test]# sh test.sh 35
script file is test.sh
他是少年

序列:
[root@Redis01 test]# echo {1..5}
1 2 3 4 5
[root@Redis01 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
[root@Redis01 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
[root@Redis01 test]# seq 5
1
2
3
4
5

tr截斷設置分隔符;
[root@Redis01 test]# seq 5|tr ‘\n‘ ‘ ‘
1 2 3 4 5 [root@Redis01 test]# seq -s ‘ ‘ 5
1 2 3 4 5
-s直接設置分割方式;
[root@Redis01 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表示結束數字;
[root@Redis01 test]# seq -s ‘ ‘ 1 5 50
1 6 11 16 21 26 31 36 41 46


(())只能運算整數;bc可以運算所有實數;
其中的-l表示導入運行計算所需包;bc是運算標識;
[root@Redis01 test]# a=1.2
[root@Redis01 test]# b=2.6
[root@Redis01 test]# sum=0
[root@Redis01 test]# ((sum=a+b))
[root@Redis01 test]# echo ${a}+${b}|bc -l
3.8
[root@Redis01 test]# echo 190+100|bc -l
290


整數計算推薦使用(()),因為性能比較好;
bc中間使用了管道,走了磁盤IO,性能不好;
[root@Redis01 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
[root@Redis01 test]# seq -s ‘+‘ 100|bc -l
5050

反單引號內的內容當作語句執行;
單引號內的內容當作字符串;
[root@Redis01 test]# ((sum=`seq -s ‘+‘ 1000`))
[root@Redis01 test]# echo $sum
500500
[root@Redis01 test]# echo $((`seq -s ‘+‘ 1000`))
500500
[root@Redis01 test]# echo $((seq -s ‘+‘ 1000))
-bash: seq -s ‘+‘ 1000: syntax error: invalid arithmetic operator (error token is "‘+‘ 1000")

for循環兩種方式:
[root@Redis01 test]# sum=0
[root@Redis01 test]# for i in `seq 100`;do ((sum=${sum}+${i}));done
[root@Redis01 test]# echo $sum
[root@Redis01 test]# for((i=1;i<=100;i++));do ((sum+=${i}));done
[root@Redis01 test]# echo $sum


while的兩種方式,註意每次需要壩變量賦值:
[root@Redis01 test]# sum=0
[root@Redis01 test]# i=1
[root@Redis01 test]# while [ $i -le 100 ];do ((sum+=i));((i++));done
[root@Redis01 test]# echo $sum

[root@Redis01 test]# sum=0
[root@Redis01 test]# i=1
[root@Redis01 test]# while ((i <= 100));do ((sum+=i));((i++));done
[root@Redis01 test]# echo $sum

Linux命令-基本變量類型及其運算