【1119】shell(上)

                                 【1119】shell(上)

5.29 shell腳本中的變量linux

5.30 shell中邏輯判斷shell

5.31 if判斷文件目錄屬性vim

5.32 if判斷的一些特殊用法bash

5.33 case用法less

5.29 shell腳本中的變量優化

當腳本中使用某個字符串較頻繁而且字符串長度很長時就應該使用變量代替spa

  1. 使用條件語句時,常使用變量 if [ $a -gt 1 ]; then … ; fi

二、引用某個命令的結果時,用變量替代 n=`wc -l 1.txt`字符串

三、寫和用戶交互的腳本時,變量也是必不可少的 read -p "Input a number: " n; echo $n 若是沒寫這個n,能夠直接使用 $REPLYinput

四、內置變量 $0, $1, $2… $0表示腳本自己,$1 第一個參數,$2 第二個 … $# 表示參數個數數學

五、數學運算 a=1;b=2; c=$(($a+$b))或者$[$a+$b]

 

5.30 shell中邏輯判斷

格式1:if 條件;then 語句;fi

[root@alexis-01 ~]# vim 01.sh

#!/bin/bash

a=5

if [ $a -gt 3 ]   //gt 大於

then

    echo ok

fi

 

格式2:if 條件;then 語句;else 語句;fi

[root@alexis-01 shell]# vim 02.sh

#!/bin/bash

a=1

if [ $a -gt 3 ]

then

    echo ok

else

    echo not ok

fi

[root@alexis-01 shell]# sh -x 02.sh

+ a=1

+ '[' 1 -gt 3 ']'

+ echo not ok

not ok

 

格式3:if …;then …;elif …;then …;else …;fi

[root@alexis-01 shell]# vim 03.sh

#!/bin/bash

a=3

if [ $a -gt 4 ]      //great than 大於

then

    echo ">4"

elif [ $a -lt 6 ]      //less than 小於

then

    echo "<6 && >1"

else

    echo not ok

fi

[root@alexis-01 shell]# sh -x  03.sh

+ a=3

+ '[' 3 -gt 4 ']'

+ '[' 3 -lt 6 ']'

+ echo '<6 && >1'

<6 && >1

 

[root@alexis-01 shell]# vim 03.sh

#/bin/bash

a=5

if [ $a -lt 4 ]

then

    echo "<4"

elif [ $a -gt 6 ]

then

    echo ">6"

else

    echo not ok

fi

[root@alexis-01 shell]# sh -x  03.sh

+ a=5

+ '[' 5 -lt 4 ']'

+ '[' 5 -gt 6 ']'

+ echo not ok

not ok

 

邏輯判斷表達式:

if [ $a -gt $b ]; -gt (>); 大於

if [ $a -lt 5 ]; -lt(<); 小於

if [ $b -eq 10 ]等 -eq(==); 等於

-le(<=);-ge(>=); -ne(!=) 注意處處都是空格

 

若是非要用 > < ,能夠用(( ))

 

[root@alexis shell]# if (($a>1));then echo ok;fi

ok

 

能夠使用 && || 結合多個條件

if [ $a -gt 5 ] && [ $a -lt 10 ]; then 而且

if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者

 

5.31 if判斷文件目錄屬性

選項 含義

[ -f file ] 判斷是不是普通文件,且存在

[ -d file ] 判斷是不是目錄,且存在

[ -e file ] 判斷文件或目錄是否存在

[ -r file ] 判斷文件是否可讀

[ -w file ] 判斷文件是否可寫

[ -x file ] 判斷文件是否可執行

[ -f file ] 判斷是不是普通文件,且存在

[root@alexis-01 shell]# vim 04.sh

#/bin/bash

f="/tmp/alexis"

if [ -f $f ]

then

    echo $f exist.

else

    touch $f

fi

 

[root@alexis-01 shell]# sh -x 04.sh

+ f=/tmp/alexis

+ '[' -f /tmp/alexis ']'

+ touch /tmp/alexis

[root@alexis-01 shell]# sh -x 04.sh

+ f=/tmp/alexis

+ '[' -f /tmp/alexis ']'

+ echo /tmp/alexis exist.

/tmp/alexis exist.

 

[ -d file ] 判斷是不是目錄,且存在

[root@alexis-01 shell]# vim file2.sh

#/bin/bash

f="/tmp/alexis"

if [ -d $f ]

then

    echo $f exist.

else

    mkdir $f

fi

[root@alexis-01 shell]# sh -x file2.sh

+ f=/tmp/arslinux

+ '[' -d /tmp/alexis ']'

+ mkdir /tmp/alexis

 

[ -e file ] 判斷文件或目錄是否存在

[root@arslinux-01 shell]# vim file3.sh

#/bin/bash

f="/tmp/arslinux"

if [ -e $f ]

then

    echo $f exist.

else

    touch $f

fi

[root@arslinux-01 shell]# sh -x file3.sh

+ f=/tmp/arslinux

+ '[' -e /tmp/alexis ']'

+ echo /tmp/alexis exist.

/tmp/alexis exist.

 

[ -r file ] 判斷文件是否可讀

[root@alexis-01 shell]# vim file4.sh

#/bin/bash

f="/tmp/alexis"

if [ -r $f ]

then

    echo $f readable.

fi

[root@alexis-01 shell]# sh -x file4.sh

+ f=/tmp/alexis

+ '[' -r /tmp/alexis ']'

+ echo /tmp/alexis readable.

/tmp/alexis readable.

 

[ -w file ] 判斷文件是否可寫

[root@alexis-01 shell]# vim file4.sh

#/bin/bash

f="/tmp/alexis"

if [ -w $f ]

then

    echo $f writeable.

fi

[root@alexis-01 shell]# sh -x file4.sh

+ f=/tmp/alexis

+ '[' -w /tmp/alexis ']'

+ echo /tmp/alexis writeable.

/tmp/alexis writeable.

 

[ -x file ] 判斷文件是否可執行

[root@alexis-01 shell]# vim file4.sh

#/bin/bash

f="/tmp/alexis"

if [ -x $f ]

then

    echo $f exeable.

fi

[root@arslinux-01 shell]# sh -x file4.sh

+ f=/tmp/alexis

+ '[' -x /tmp/alexis']'

 

簡單寫法

#!/bin/bash

f="/tmp/alex"

if [ -f $f ]

then

rm -f $f

fi

能夠不用 if 判斷寫成

#!/bin/bash

f="/tmp/alex"

[ -f $f ] && rm -f $f

 

#!/bin/bash

f="/tmp/alex"

if [ -f $f ]

then

rm -f $f

else

touch $f

fi

能夠反寫爲

 

#!/bin/bash

f="/tmp/alex"

if [ ! -f $f ]

then

touch $f

fi

 

能夠不用 if 判斷,寫成

#!/bin/bash

f="/tmp/alex"

[ -f $f ] || touch $f

 

5.32 if判斷的一些特殊用法

if [ -z 「$a」 ] 這個表示當變量 a 的值爲空時會怎麼樣

#!/bin/bash

n=`wc -l /tmp/bababa`

if [ -z "$n" ]

then

    echo error

else

    if [ $n -gt 100]

    then

        echo ok

    fi

fi

 

能夠優化爲:

#!/bin/bash

n=`wc -l /tmp/bababa`

if [ -z "$n" ]

then

echo error

exit

elif [ $n -gt 100]

then

echo ok

fi

能夠再優化:

 

#!/bin/bash

if [ ! -f /tmp/babala ]

then

echo "/tmp/babala isn't exist."

exit

fi

n=`wc -l /tmp/bababa`

if [ -z "$n" ]

then

echo error

exit

elif [ $n -gt 100]

then

echo ok

fi

 

if [ -n 「$a」 ] 表示當變量 a 的值不爲空

[root@alexis-01 shell]# if [ -n 01.sh ];then echo ok;fi

ok

 

[root@alexis-01 shell]# if [ -n "$b" ];then echo "$b";else echo "b is null";fi

b is null

 

命令能夠做爲判斷結果

if grep -q ‘123’ 1.txt; then 表示若是 1.txt 中含有’ 123’ 的行時會怎麼樣

[root@alexis-01 shell]# if grep -w 'user1' /etc/passwd;then echo "user1 exist";fi

user1:x:1001:1001::/home/user1:/bin/bash

user1 exist

 

[root@alexis-01 shell]# if grep -wq 'user1' /etc/passwd;then echo "user1 exist";fi

user1 exist //grep -q 靜默輸出

 

if [ ! -e file ]; then 表示文件不存在時會怎麼樣

if (($a<1)); then …等同於 if [ $a -lt 1 ]; then…

[ ] 中不能使用<,>,==,!=,>=,<=這樣的符號

 

5.33 case用法

格式:case 變量名 in

value1)

command

;;

value2)

command

;;

*)

commond

;;

esac

 

;; 表示一個判斷結束,進入下一個判斷

在 case 程序中,能夠在條件中使用 |,表示或的意思, 好比

2|3)

command

;;

*)除以上全部以外的

 

shell 腳本案例

#!/bin/bash

read -p "Please input a number: " n //從標準輸入讀取輸入並賦值給變量 n

if [ -z "$n" ]

then

    echo "Please input a number."

    exit 1

fi

 

n1=`echo $n|sed ‘s/[0-9]//g’` //從變量n中將數字替換爲空,若是n1不爲空,則不是純數字,那麼現實輸入一個數字並退出

 

if [ -n "$n1" ]

then

    echo "Please input a number."

    exit 1

fi

if [ $n -lt 60 ] && [ $n -ge 0 ] //純數字往下接着判斷

then

    tag=1 //標記tag

elif [ $n -ge 60 ] && [ $n -lt 80 ]

then

    tag=2

elif [ $n -ge 80 ]  && [ $n -lt 90 ]

then

    tag=3

elif [ $n -ge 90 ] && [ $n -le 100 ]

then

    tag=4

else

    tag=0

fi

case $tag in

    1)

echo "not ok"

        ;;

    2)

        echo "ok"

        ;;

    3)

        echo "great"

        ;;

    4)

        echo "brilliant"

        ;;

    *)

        echo "The number range is 0-100."

        ;;

esac

 

執行腳本看結果:

輸入符合條件的純數字

[root@alexis-01 shell]# sh -x case.sh

+ read -p 'Please input a number: ' n

Please input a number: 77

+ '[' -z 77 ']'

++ echo 77

++ sed 's/[0-9]//g'

+ n1=

+ '[' -n '' ']'

+ '[' 77 -lt 60 ']'

+ '[' 77 -ge 60 ']'

+ '[' 77 -lt 80 ']'

+ tag=2

+ case $tag in

+ echo ok

ok

 

輸入含字母的數字組合

[root@alexis-01 shell]# sh -x case.sh

+ read -p 'Please input a number: ' n

Please input a number: l33l

+ '[' -z l33l ']'

++ echo l33l

++ sed 's/[0-9]//g'

+ n1=ll

+ '[' -n ll ']'

+ echo 'Please input a number.'

Please input a number.

+ exit 1

 

輸入大於 100 的數字

[root@alexis-01 shell]# sh -x case.sh

+ read -p 'Please input a number: ' n

Please input a number: 234

+ '[' -z 234 ']'

++ echo 234

++ sed 's/[0-9]//g'

+ n1=

+ '[' -n '' ']'

+ '[' 234 -lt 60 ']'

+ '[' 234 -ge 60 ']'

+ '[' 234 -lt 80 ']'

+ '[' 234 -ge 80 ']'

+ '[' 234 -lt 90 ']'

+ '[' 234 -ge 90 ']'

+ '[' 234 -le 100 ']'

+ tag=0

+ case $tag in

+ echo 'The number range is 0-100.'

The number range is 0-100.

相關文章
相關標籤/搜索