The basic implementation of testing and judging shell scripts

Source: Internet
Author: User

condition test: Whether the condition of judgment is established

1. type of condition test: file test; integer comparison; string comparison; logic test

2 . Conditional test Syntax:[ operator conditional expression ] equals test operator conditional expression

3. file test:

1) Type of file:-(normal file),D(inside), c(character device),s(socket), b(block device),p(pipe file, Can be used for screen demonstrations)

2) file judge:-F file,-D Directory,e- file exists,-rwx to determine whether there is permission [ operator file /c13>]

4. integer comparison

1) syntax:[ number 1 operator number 2]

2) Operator:-eq ,-ne unequal,-gt greater than,-lt less than,-ge greater than equals,-le less than equals

5. String comparison

1) syntax:[ string 1 operator string 2]

2) Operator: = = equal ,-Z is empty,! = Unequal

Second, if use:

1. Single-branch syntax:

If [ conditional expression ];Then

Command actions

Fi

Note: When the condition is established, the command operation is executed, and the exit does not execute when the command is not

2. Double-branch syntax:

If [ conditional expression ];Then

Command operation for condition-established execution

Else

The condition does not establish the action performed

Fi

Note: A two-branch operation is done on both sides of the condition,

3. If Multi-branch syntax:

If [ conditional expression 1];Then

Command Action 1

elif [ conditional expression 2];Then

Command Action 2

elif [ conditional expression 2];Then

Command Action 3

Else

default command Action

Fi

Note: Multiple branches perform multiple test conditions, and only command operations that have a condition set are performed.


Practice cases:

Case:
Str1=hehe
[$STR 1 = = hehe]
[$LANG = = en]
[$STR 1! = linuxfan.cn]
str2= # #直接回车
[-Z $STR 2]
4) Logical Judgment
Logical judgment is the same as or not.
Grammar:
[Expression 1] && (and), | | (OR),! (non) [expression 2]
Command 1 &&,| |,! Command 2
Case:
Compile and install:
./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
Yum-y Install lftp &>/dev/null &&echo "lftp installed."
Fi
2) if dual branch
Grammar:
if [Condition];then
Command 1
Else
Command 2
Fi
Use a two-branch single-branch case script to overwrite if the installation is already installed and is installed if it is not installed.
3) If multi-branch
Grammar:
if [Condition 1];then
Command 1
elif [Condition 2];then
Command 2
....
Else
Command
Fi
Case:
#!/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
Comprehensive case:
[email protected] bin]# cat 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
[Email protected] bin]#














This article from the "11982647" blog, reproduced please contact the author!

The basic implementation of testing and judging shell scripts

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.