據說shell腳本如果會是一件很了不起的事情(默默的自我裝逼)
爲了很吊,我就開始學習shell script 。
今天我就學習了了一些簡單的語法。
test 指令,如果不知道,可以用 :man test 來查看。
NAME
test - check file types and compare values
SYNOPSIS
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
DESCRIPTION
Exit with the status determined by EXPRESSION.
--help display this help and exit
--version
output version information and exit
An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit
status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
這就是test 的一些基本指令規則,中文解釋
test可理解的表達式類型分爲四類:
- 表達式判斷
- 字符串比較
- 數字比較
- 文件比較
1)判斷表達式
if test (表達式爲真)
if test !表達式爲假
test 表達式1 –a 表達式 2 兩個表達式都成立 ,爲true
test 表達式1 –o 表達式2 兩個表達式有一個成立 ,爲true
2)判斷字符串
test –n 字符串 字符串的長度非零 ,則爲true
test –z 字符串 字符串的長度爲零 ,則爲true
test 字符串1=字符串 2 字符串相等 ,則爲true
test 字符串1 !=字符串2 字符串不等 ,則爲true
3)判斷整數
test 整數1 –eq 整數2 整數相等
test 整數 1 –ge 整數2 整數1大於等於整數2
test 整數1 –gt 整數 2 整數1大於整數2
test 整數1 –le 整數 2 整數1小於等於整數2
test 整數1 –lt 整數 2 整數1小於整數2
test 整數1 –ne 整數 2 整數1不等於整數2
4)判斷文件
test File1 –ef File2 兩個文件具有同樣的設備號和i結點號
test File1 –nt File2 文件1比文件2 新
test File1 –ot File2 文件1比文件2 舊
test –b File 文件存在並且是塊設備文件
test –c File 文件存在並且是字符設備文件
test –d File 文件存在並且是目錄
test –e File 文件存在
test –f File 文件存在並且是正規文件
test –g File 文件存在並且是設置了組ID
test –G File 文件存在並且屬於有效組ID
test –h File 文件存在並且是一個符號鏈接(同-L)
test –k File 文件存在並且設置了sticky位
test –b File 文件存在並且是塊設備文件
test –L File 文件存在並且是一個符號鏈接(同-h)
test –o File 文件存在並且屬於有效用戶ID
test –p File 文件存在並且是一個命名管道
test –r File 文件存在並且可讀
test –s File 文件存在並且是一個套接字
test –t FD 文件描述符是在一個終端打開的
test –u File 文件存在並且設置了它的set-user-id位
test –w File 文件存在並且可寫
test –x File 文件存在並且可執行
test xxx 可以簡寫成 [ xxx ] 的形式。
也就是說 test 和 [ ]都是判斷形式
Test 腳本例子
1 #Test shell script pratice!
2 #!/bin/bash
3 #Write by Leo
4 #2016-01-21
5 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
6 export PATH
7
8 echo "Please input a filename, i will check the name !\n"
9 read -p "Input filename: " filename
10 test -z $filename && echo "You muss input a filename!\n" && exit 0
11 test ! -e $filename && echo "This filename is not exit!\n" && exit 0
12 test -f $filename && filetype="file"
13 test -d $filename && filetype="directory"
14
15 echo " this file is $filetype !\n"