shell編程-條件判斷與流程控制-整數字符串的比較

**

按照文件類型判斷

**
在這裏插入圖片描述


# File Name: file.sh
# Author: Finley
# mail: [email protected]
# Create Time: Wed 21 Aug 2019 09:18:08 AM CST
#====================================================================================
#!/bin/bash
#判斷是否爲設備文件
if [ -b /dev ]
        then
        echo "/dev is devive file"
fi
if [ -c /dev ]
        then
        echo "/dev 不是設備字符文件"
fi
if [ -d /etc/nginx ]
        then
        echo "/etc/nginx is direcoty"
fi
if [ -e /etc/nginx/nginx.conf ]
        then
        echo "/etc/nginx/nginx.conf is a file and it exits"
fi
if [ -f /home/admin/shell/iffull.sh ]
        then
        echo "/home/admin/shell/iffull.sh is a file"
fi
if [ ! -s /home/admin/shell/no.txt ]
        then
        echo "no.txt is null"
fi
                                              

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

  • 這種條件判斷不會進行太複雜的條件判斷,只判斷是否有權限,而不會是哪一個用戶纔有這種權限。

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
博客:https://www.cnblogs.com/songgj/p/9115954.html

  • 硬鏈接:https://baike.sogou.com/v63228740.htm?fromTitle=%E7%A1%AC%E9%93%BE%E6%8E%A5
  • 來源於搜狗百科
[root@izwz97473w2ydu1pgsmzk4z test]# ll
total 8
-rw-r--r-- 1 root root  64 Aug 13 20:25 student.txt
-rw-r--r-- 1 root root 158 Aug 13 19:35 zheng.txt
[root@izwz97473w2ydu1pgsmzk4z test]# ll -i
total 8
1445009 -rw-r--r-- 1 root root  64 Aug 13 20:25 student.txt
1445008 -rw-r--r-- 1 root root 158 Aug 13 19:35 zheng.txt
[root@izwz97473w2ydu1pgsmzk4z test]# ln zheng.txt /opt/code/stu.txt
[root@izwz97473w2ydu1pgsmzk4z test]# [ zheng.txt -ef /opt/code/stu.txt] && echo "yes"||echo "no"
-bash: [: missing `]'
no
[root@izwz97473w2ydu1pgsmzk4z test]# [ /opt/test/zheng.txt -ef /opt/code/stu.txt ] && echo "yes"||echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z test]# cd /opt/code/
[root@izwz97473w2ydu1pgsmzk4z code]# ls
cppfile  shell  stu.txt
[root@izwz97473w2ydu1pgsmzk4z code]# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 13 20:56 cppfile
drwxr-xr-x 2 root root 4096 Aug 13 11:02 shell
-rw-r--r-- 2 root root  158 Aug 13 19:35 stu.txt
[root@izwz97473w2ydu1pgsmzk4z code]# ll -i
total 12
1444403 drwxr-xr-x 2 root root 4096 Aug 13 20:56 cppfile
1318545 drwxr-xr-x 2 root root 4096 Aug 13 11:02 shell
1445008 -rw-r--r-- 2 root root  158 Aug 13 19:35 stu.txt
[root@izwz97473w2ydu1pgsmzk4z code]# 

  • 整數判斷
  • -等於:equal
  • 不等於:unequal
  • 大於:greater than
  • 小於::less than
  • 注意事項:
    之前不是有說,shell中沒有明確定義的變量,類型默認爲字符串類型嗎?這需要注意的是,一旦加入了整數比較的符號。它就會把兩側表達式當成是整數來進行比較。
    還有一點需要注意,就是方括號兩(內側)側的空格,沒有空格,無法正常執行
    在這裏插入圖片描述
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -eq 22] && echo "yes" || echo "no"
-bash: [: missing `]'
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -eq 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 20 -eq 22 ] && echo "yes" || echo "no"
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 20 -en 22 ] && echo "yes" || echo "no"
-bash: [: -en: binary operator expected
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 20 -ne 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 99 -ft 22 ] && echo "yes" || echo "no"
-bash: [: -ft: binary operator expected
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 99 -gt 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 99 -lt 22 ] && echo "yes" || echo "no"
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -lt 99 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -ge 2 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -ge 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -le 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 21 -le 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 211 -le 22 ] && echo "yes" || echo "no"
no

在這裏插入圖片描述

  • 注意使用$符號時候的雙引號
[root@localhost ~]# [ -n $name ] && echo yes || echo no
yes
[root@localhost ~]# [ -n $wang ] && echo yes || echo no
yes
[root@localhost ~]# [ -n "$wang" ] && echo yes || echo no
no
[root@localhost ~]# [ -n "$name" ] && echo yes || echo no
no
[root@localhost ~]# 

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

  • 爲什麼會出現下面這些報錯呢
  • 第一:變量名命名有問題
  • 第二:不瞭解雙反引號和$()的區別
./ifroot.sh: line 5: syntax error: unexpected end of file
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# ./ifroot.sh 
./ifroot.sh: line 4: unexpected EOF while looking for matching ``'
./ifroot.sh: line 5: syntax error: unexpected end of file
[root@localhost shelltest]# ./ifroot.sh 
./ifroot.sh: line 4: unexpected EOF while looking for matching ``'
./ifroot.sh: line 5: syntax error: unexpected end of file
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# ./ifroot.sh 
$root
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# ./ifroot.sh 
root
[root@localhost shelltest]# 

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章