# test

##### 字串或文字的比對

```
Str1 = str2 | 當str1與str2相同時, 傳回True
Str1 != str2| 當str1與str2不同時, 傳回True
Str1 < Str2 
Str1 <= Str2
Str1 > Str2
Str1 >= Str2
Str     | 當str不是空字符時, 傳回True
-n str  | 當str的長度大於0時, 傳回True
-z str  | 當str的長度是0時, 傳回True
```

##### \[ \]與\[\[ \]\] 差別

- `[ ... ]` : POSIX 相容指令，別名 test，用於條件判斷式。
- `[[ ... ]]` : Bash 專有語法，可用於更複雜的條件判斷式。
- `[ -n "$VAR_STR"]` : 使用 \[ \] 時要注意變數的雙引號用法。

範例：

```bash
VAR_STR=""
if [[ -n $VAR_STR ]]; then
    echo "var str is nonzero with [["
else
    echo "var str is zero with [["
fi

if [ -n $VAR_STR ]; then
    echo "var str is nonzero with ["
else
    echo "var str is zero with ["
fi
```

Output:

```
var str is zero with [[
var str is nonzero with [
```

Fix:

```bash
VAR_STR=""

if [[ -n $VAR_STR ]]; then
    echo "var str is nonzero with [["
else
    echo "var str is zero with [["
fi

if [ -n "$VAR_STR" ]; then  # <<<<
    echo "var str is nonzero with ["
else
    echo "var str is zero with ["
fi
```

##### 數值型比對

整數

```
Int1 -eq int2 |當int1等於int2時, 傳回True
Int1 -ge int2 |當int1大於/等於int2時, 傳回True
Int1 -le int2 |當int1小於/等於int2時, 傳回True
Int1 -gt int2 |當int1大於int2時, 傳回True
Int1 -ne int2 |當int1不等於int2時, 傳回True
Int1 -lt int2 |當int1小於 int2時, 傳回True 
```

浮點數

```bash
asr=87.22
thre=90.00
if (( $(echo "$asr < $thre" | bc -l) )); then
    return 0 # true
fi
```

##### 檔案的比對

```
-e file   | 檔案是否存在
-d file   | 當file是一個目錄時, 傳回 True
-f file   | 當file是一個普通檔案時, 傳回 True
-r file   | 當file是一個可讀檔案時, 傳回 True
-s file   | 當file檔案內容長度大於0時, 傳回 True; 空白內容傳回 False
-w file   | 當file是一個可寫檔案時, 傳回 True
-x file   | 當file是一個可執行檔案時, 傳回 True
```

File operators list

<table class="nixcraftinlinefaqtables" id="bkmrk-operator-returns--a-"><thead><tr><th width="25%">Operator</th><th width="75%">Returns</th></tr></thead><tbody><tr><td><kbd>-a FILE</kbd></td><td>True if file exists.</td></tr><tr><td><kbd>-b FILE</kbd></td><td>True if file is block special.</td></tr><tr><td><kbd>-c FILE</kbd></td><td>True if file is character special.</td></tr><tr><td><kbd>-d FILE</kbd></td><td>True if file is a directory.</td></tr><tr><td><kbd>-e FILE</kbd></td><td>True if file exists.</td></tr><tr><td><kbd>-f FILE</kbd></td><td>True if file exists and is a regular file.</td></tr><tr><td><kbd>-g FILE</kbd></td><td>True if file is set-group-id.</td></tr><tr><td><kbd>-h FILE</kbd></td><td>True if file is a symbolic link.</td></tr><tr><td><kbd>-L FILE</kbd></td><td>True if file is a symbolic link.</td></tr><tr><td><kbd>-k FILE</kbd></td><td>True if file has its `sticky' bit set.</td></tr><tr><td><kbd>-p FILE</kbd></td><td>True if file is a named pipe.</td></tr><tr><td><kbd>-r FILE</kbd></td><td>True if file is readable by you.</td></tr><tr><td><kbd>-s FILE</kbd></td><td>True if file exists and is not empty.</td></tr><tr><td><kbd>-S FILE</kbd></td><td>True if file is a socket.</td></tr><tr><td><kbd>-t FD</kbd></td><td>True if FD is opened on a terminal.</td></tr><tr><td><kbd>-u FILE</kbd></td><td>True if the file is set-user-id.</td></tr><tr><td><kbd>-w FILE</kbd></td><td>True if the file is writable by you.</td></tr><tr><td><kbd>-x FILE</kbd></td><td>True if the file is executable by you.</td></tr><tr><td><kbd>-O FILE</kbd></td><td>True if the file is effectively owned by you.</td></tr><tr><td><kbd>-G FILE</kbd></td><td>True if the file is effectively owned by your group.</td></tr><tr><td><kbd>-N FILE</kbd></td><td>True if the file has been modified since it was last read.</td></tr><tr><td><kbd>! EXPR</kbd></td><td>Logical not.</td></tr><tr><td><kbd>EXPR1 &amp;&amp; EXPR2</kbd></td><td>Perform the <kbd>and</kbd> operation.</td></tr><tr><td><kbd>EXPR1 || EXPR2</kbd></td><td>Perform the <kbd>or</kbd> operation.</td></tr></tbody></table>

##### 布林變數應用

```shell
# Let us Declare Two Boolean Variables
# Set this one to true
jobstatus=true
# Check it 
if [ "$jobstatus" = true ] ; then
	echo 'Okay :)'
else
	echo 'Noop :('
fi
# Double bracket format syntax to test Boolean variables in bash
bool=false
if [[ "$bool" = true ]] ; then
	echo 'Done.'
else
	echo 'Failed.'
fi
```

##### 更多範例

```shell
# 如果前個指令執行有錯誤時，會執行兩個程序
[ ! $? -eq 0 ] && echo "Abort the process!! you can try it agian after you made change." && exit 1

# 或者
[ ! $? -eq 0 ] && {
 echo "Abort the process!! you can try it agian after you made change."
 exit 1
} 

#
if [[ $RETURN_CODE != 0 ]]; then
        echo "Zulip first start database initi failed in \"initialize-database\" exit code $RETURN_CODE. Exiting."
        exit $RETURN_CODE
fi

#
if [[ $TIMEOUT -eq 0 ]]; then
            echo "Could not connect to database server. Exiting."
            unset PGPASSWORD
            exit 1
fi
#
if (($? > 0)); then
            echo "$SECRET_KEY = $SECRET_VAR" >> "$DATA_DIR/zulip-secrets.conf"
            echo "Secret added for \"$SECRET_KEY\"."
fi
#
if test "x$newbranch" = x; then
    newbranch=`git branch -a | grep "*" | cut -d ' ' -f2`
fi

# Check router home directory.
[ -d "$PROD_HOME" ] || {
	echo "Router home directory ($PROD_HOME) not found"
	exit 1
}

# AND
autoBackupConfiguration() {
    if ([ "$AUTO_BACKUP_ENABLED" != "True" ] && [ "$AUTO_BACKUP_ENABLED" != "true" ]); then
        rm -f /etc/cron.d/autobackup
        echo "Auto backup is disabled. Continuing."
        return 0
    fi
}

# OR
if [ "$MANUAL_CONFIGURATION" = "False" ] || [ "$MANUAL_CONFIGURATION" = "false" ]; then
        databaseConfiguration
        secretsConfiguration
        authenticationBackends
        zulipConfiguration
fi

# Die if $f1 or $f2 is missing 
if [ ! -f "$f1"  ] ||  [ ! -f "$f2" ]
then
	echo "Required files are missing."
else
	echo "Let us build SFTP jail."
fi
# And
if [[ $age -ge 18 ]] && [[ $nat -eq "Indian" ]];then
    echo "You can vote!!!"
else
    echo "You can not vote"

fi

# multiple AND
if [ -e "$DATA_DIR/.initiated" ] && ([ "$FORCE_FIRST_START_INIT" != "True" ] && [ "$FORCE_FIRST_START_INIT" != "true" ]); then
    echo "First Start Init not needed. Continuing."
    return 0
fi


# one-liner
 [[ -z "$var" ]] && echo "NULL" || echo "NOT NULL"
# if age is greater than or equal to 18 then the program will print "Adult" or it will print "Minor".
[[ $age -ge 18 ]] && echo "Adult" || echo "Minor"

# Contain a substring
if [[ $var = *pattern1* ]]; then
    echo "Do something"
fi

[[ $1 != *cyberciti.biz/faq/* ]] && { printf "Error: Specify faq url (e.g., http://www.cyberciti.biz/faq/url-1-2-3/)\n"; exit 2; }

if [[ $fullstring == *"$substr"* ]];

# Regex
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then

head="win00000:hascom_command.ksh : hostname:wintstc, monitor port#:17911"
if [[ $head =~ ^win00000.*$ ]]; then

# Check the the number of the version
[ "$(echo "$TMUX_VERSION >= 2.4" | bc)" = 1 ] || echo "The version $TMUX_VERSION is outdated" 
```

Two functions (Is\_alert &amp; Notify) return True

```bash
if Is_alert && Notify; then
    echo "Send_mail"
fi
```