Use of shell scripts---if condition judgment

Source: Internet
Author: User

Testing and judging of shell scripts

1. Testing

Grammar:

[operator condition] = = Test operator condition

1) test file-level directory

Type of File:

F, File

Ls-l # #只查看字段的第一个字符, file "-"

D, Catalogue

Ls-ld

L, Link file

Ls-l

C, character device

Ls-l/dev/tty

B, block device

Ls-l/dev/sda1

s, socket file, General Service using MySQL

Ls-l/tmp/mysql.socket

P, Pipe

Mkfifo hehe # #创建管道文件

Ls-l hehe

Test operator:-f file,-D directory, whether E exists,-R is readable,-w is writable,-X is executable


Case LIST:

[-d/tmp]

echo $? # #返回值为0代表/tmp is a directory


2) Comparison of integers

Operator:

-eq equals,-ne not equal to,-gt greater than,-lt, less than, ge greater than equals, le less than equals


Case LIST:

[10-gt 11]

[$?-eq 0] # #上一条命令是否执行成功


3) string comparison

String: Refers to a data type that can be interpreted as plain text type

Operator: = = is equal,! = is not equal,-Z is empty


Case LIST:

Str1=hehe

[$STR 1 = = hehe]

[$LANG = = en]

[$STR 1! = linuxfan.cn]

str2= # #直接回车

[-Z $STR 2]

[-Z $ "/root/a"] # #这个文件是否为空


4) Logical Judgment

Logical judgment is the same as or non-

Grammar:

[Expression 1] && (and), | | (OR),! (non) [expression 2]


Case LIST:

Compiling the installation

./configure &&make &&make

[1–ge 2] | | [1–lt 2] # #测试结果为真

[$LANG! = en]


2. If judgment

1) Single Branch

Grammar:

if [Condition];then

Command

Fi

Case: Determine if the LFTP is installed if not installed

VI install_lftp.sh

#!/bin/bash

Rpm-qa |grep lftp 1>/dev/null

If [$?-ne 0];then # #若上调命令执性不成功说明没有安装lftp, install

Yum-y Install lftp &>/dev/null &&echo "lftp installed."

Fi

: Wq

2) if dual branch

Grammar:

if [Condition];then

Command

Else

Command 1

Fi

Use single-branch case script overwrite if installed and put back already installed, not installed

3) If multi-branch

Grammar:

if [Condition 1];then

Command 1

elif [Condition 2];then

Command 2

...

else [Condition 3];then

Command

Fi


Case LIST:

VI s60.sh

#!/bin/bash

Ls/etc/rc.d/rc3.d/s60vsftpd

If [$?-ne 0]

Then

Chkconfig vsftpd on

elif [-X/ETC/INIT.D/VSFTPD]

Then

chmod +x/etc/init.d/vsftpd

Else

/ETC/INIT.D/VSFTPD restart

Fi

: Wq


Consolidated Case List

VI if_then_fi.sh

#!/bin/bash

name=$ (basename)

If [$#-ne 2-a $#-ne 3];then

echo "Usage: $name vaule1 value2"

echo "$name value1 value2 vaule3"

Exit 1

Fi

If [$#-eq 2];then

echo "Args:$1,$2"

Else

echo "Three args:$1,$2,$3"

Fi

If [$#-eq 2];then

If [$1-gt $];then

echo "$ > $"

elif [$1-lt $];then

echo "$ < $"

Else

echo "$ = $"

Fi

Else

If [$1-ge $2-a $1-ge $];then

echo "$ >=$2,$1 >=$3"

elif [$1-ge $2-a $1-le $];then

echo "$ >=$2,$1 <="

elif [$1-lt $2-a $1-ge $];then

echo "$ < $2,$1 >="

Else

echo "$ < $2,$1 < $"

Fi

Fi

Exit 0

: Wq

This article from "Lp-linux" blog, declined reprint!

Use of shell scripts---if condition judgment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.