核心知識點(二)變量的高級用法

命令替換

語法格式

方法 語法格式
方法一 command
方法二 $(command)

小例子一:取出系統(tǒng)中所有的用戶并輸出


#!/bin/bash

index=1

users=`cat /etc/passwd | cut -d ":" -f 1`

for user in $users

do

    echo "this is $index user:$user"

    index=$((index+1))

done

小例子二: 根據(jù)系統(tǒng)時間計算今年或者明年


[wanghui@www example_1]$ echo "This is $(date +%Y) year"

[wanghui@www example_1]$ echo "This is $(($(date +%Y) +1 )) year"    ##時間計算+1

[wanghui@www example_1]$ echo "$((20+30))"     #運算

[wanghui@www example_1]$ num1=20

[wanghui@www example_1]$ num2=70

[wanghui@www example_1]$ echo "$(($num1 + $num2))"    #這樣也可以echo "$((num1 + num2))"

小例子三:計算今年過去了多少星期,還剩多少星期


[wanghui@www example_1]$ vim example2.sh

#!/bin/bash

echo "This year has passed $(date +%j) days"

echo "This year has passed $(($(date +%j)/7)) week"

echo "There is $((365 - $(date +%j))) days before new year"

echo "There is $(((365 - $(date +%j))/7)) weeks before new year"

小例子四: 判定Nginx進程是否存在,不存在就自動拉起(https://segmentfault.com/a/1190000009723940


#!/bin/bash

nginx_process_num=$(ps -ef | grep nginx | grep -v grep | wc -l)

if [ $nginx_process_num -eq 0 ];then

   systemctl restart nginx

fi

變量類型

  • declare=typeset
image.png

[wanghui@www shell_script]$ declare -r var2

[wanghui@www shell_script]$ var2="hello java"

---------聲明

[wanghui@www shell_script]$ num1=10

[wanghui@www shell_script]$ num2=$num1+10

[wanghui@www shell_script]$ echo $num2

10+10

[wanghui@www shell_script]$ declare -i num3

[wanghui@www shell_script]$ num3=$num1+10

[wanghui@www shell_script]$ echo $num3

[wanghui@www shell_script]$ declare -F   #展示系統(tǒng)函數(shù)

[wanghui@www shell_script]$ declare -a array     #聲明數(shù)組 

[wanghui@www shell_script]$ array=("jones","mike","kobe","jordan")    #定義數(shù)組

[wanghui@www shell_script]$ echo ${array[@]}    #列出所有元素

聲明環(huán)境變量


[wanghui@www shell_script]$ num5=30

[wanghui@www shell_script]$ echo $num5

30

[wanghui@www shell_script]$ vim test1.sh

#!/bin/bash

echo $num5

[wanghui@www shell_script]$ echo $num5

30

[wanghui@www shell_script]$ bash test1.sh #不能引用num5

[wanghui@www shell_script]$ declare -x num5  #聲明即可

[wanghui@www shell_script]$ bash test1.sh 

30

取消聲明的變量


declare +a

declare +r

declare +i

declare +x

expr計算

image.png

image.png

[wanghui@www shell_script]$ num1=30

[wanghui@www shell_script]$ num2=50

[wanghui@www shell_script]$ expr $num1 \> $num2

0

[wanghui@www shell_script]$ num1=17

[wanghui@www shell_script]$ num2=5

[wanghui@www shell_script]$ expr $num1 + $num2

22

[wanghui@www shell_script]$ num3=$(expr $num1 + $num2)

[wanghui@www shell_script]$ echo $num3

22

[wanghui@www shell_script]$ expr $num1 \* $num2

85

[wanghui@www shell_script]$ expr $num1 / $num2

3

[wanghui@www shell_script]$ expr $num1 % $num2

2

[wanghui@www shell_script]$ num3=$(($num1 + $num2))

[wanghui@www shell_script]$ echo $num3

22

[wanghui@www shell_script]$ num3=$(($num1 * $num2))

[wanghui@www shell_script]$ echo $num3

85

image.png

練習

提示用戶輸入一個正整數(shù),然后計算1+2+3+4+....num,必須對num是否為正整數(shù)做出判斷,不符合應(yīng)當允許其再次輸入


[wanghui@www shell_script]$ vim sum.sh

#!/bin/bash

while true

do

    read -p "Please input a positive number:" num

    expr $num + 1 &> /dev/null

    if [ $? -eq 0 ];then

        if [ `expr $num \> 0` -eq 1 ];then

            #echo "Yes,Positive Number"

            for ((i=1;i<=$num;i++))

            do

               sum=`expr $sum + $i`

            done

             echo "1+2+3+...+$num=$sum"

             exit

        fi

    fi

    echo "error input unlegal.."

    continue

done

bc計算

  • bc是bash內(nèi)建的運算器,支持浮點數(shù)運算

  • 內(nèi)漸變量scale可以設(shè)置,默認是0

image.png

[wanghui@www shell_script]$ bc

bc 1.06.95

Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'. 

1+2

3

3-2

1

3*8

24

9/3

3

10/3

3

scale=2

10/3

3.33

[wanghui@www shell_script]$ echo "scale=9;2333.88/33" | bc    #腳本中使用方法

70.723636363

浮點數(shù)運算


#!/bin/bash

read -p "input a number:" num1

read -p "input a number:" num2

echo "scale=2;$num1/$num2" | bc

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容