Test usage of shell scripts

Source: Internet
Author: User
Tags logical operators

#!/bin/bash
#1. Integer comparison operators
#整数变量和整数常量比较
Num1=15
["$num 1"-eq] #测试num1是否等于15
echo $? # #退出状态为0, means NUM1 equals 15

["$num 1"-eq] #测试num1是否等于20
echo $? # #退出状态为1, means num1 is not equal to 20

["$num 1"-lt] # #测试num1是否小于15
echo $? # #退出状态为1, indicating num1 not less than 15

["$num 1"-gt] # #测试num1是否大于15
echo $? # #退出状态为1, indicating NUM1 not greater than 15


# #两个整数变量比较
FIRST_NUM=99 # #深圳第一个变量为99
SECOND_NUM=100 # #设置第二个变量为100
["$first _num"-gt "$second _num"]
echo $? # #退出状态为1, stating that the variable First_num value is not greater than the variable Second_num value

["$first _num"-eq "$second _num"]
echo $? # #退出状态为1, stating that the variable first_num value is not equal to the variable second_num value

["$first _num"-lt "$second _num"]
echo $? # #退出状态为0, stating that the variable First_num value is less than the variable second_num value


#2. String operators
#字符串比较运算
Str1= ""
Test "$str 1"
echo $? # #退出状态为1, indicating that the string is empty

Test-n "$str 1"
echo $? # #退出状态为1, indicating that the string is empty

Test-z "$str 1"
echo $? # #退出状态为0, indicating that the string is empty


#比较两字符串是否相等
Type1= "VI"
Type2= "Vim"
["$type 1" = "$type 2"] # #测试变量type1是否等于type2
echo $? # #退出状态为1, stating that the variable type1 is not equal to the variable type2

["$type 1"! = "$type 2"] # #测试type1是否不等于type2
echo $? # #退出状态为0, which indicates that the variable type1 is not equal to the variable type2

# #空格造成测试结果不相等
str2= "Hello" # #赋值字符串变量str2为 "Hello"
["$str 2" = "Hello"] # #测试变量str2是否与 "Hello" equals
echo $? # #退出状态为1, stating that the variable str2 is not equal to "hello"


#大小写区分
str3= "Hello" # #赋值字符串str3为 "Hello"
["$STR 3" = "Hello"] # #测试变量str3是否与 "Hell" equals
echo $? # #退出状态为1, stating that the variable str2 is not equal to "hello"


# #变量弱化造成的赋值结果不同
var1= "007" # #给变量赋值, can be used as an integer or as a string
["$var 1" = "7"] # #测试变量var1的值是否等于字符串7
echo $? # #退出在为1, indicating that the value of the variable var1 is not equal to the string 7
["$var 1"-eq "7"] # #测试变量var1的值是否等于整数7
echo $? # #退出状态为0, which indicates that the value of the variable var1 equals the integer 7


#3. File operators
#判断输入的文件是目录还是目录
LS # #显示当前目录下的所有文件
[-D File_exam] # #测试file_exam是否是目录
echo $? # #退出状态为1, description File_examp is not a directory
[-F File_exam] # #测试file_exam是否是文件
echo $? # #测试结果为0, Description File_exam is a file

# #测试文件是否存在
Ls
[-E File_exam] # #测试file_exam是否存在
echo $? # #突出状态为0, not file File-exam exist
[-E file_exam1] # #测试文件file_exam1是否存在
echo $? # #退出状态为1, indicating that the file file_exam1 does not exist

# #测试文件权限
Ls-l
[-R File_exam] # #测试文件是否可读
echo $?
[-W File_exam] # #测试文件是否可写
echo $?
[-X File_exam] # #测试文件是否执行
echo $?

# #显示通过chmod命令增减文件权限
Ls-l
[-X File_exam] # #测试文件file_exam是否可执行
echo $? # #退出状态为1, description file File_exam not executable
chmod a+x File_exam # #给文件file_exam增加可执行权限
[-X File_exam] # #再次测试文件file_exam是否可执行
echo $? # #退出状态为0, indicates that the file File_exam is now executable

# #4. Logical operators
#逻辑非
Ls
[!-e File_exam] # #使用逻辑非测试一个存在的文件
echo $? # #腿子状态为1, the description file File_exam does not exist is false
[!-e File_exam] # #使用逻辑非测试一个不存在的文件
echo $? # #退出状态为0, the description file File_exam exists is false

# #逻辑与
Ls-l
[-E File_exam-a-x file_exam] # #测试文件file_exam是否存在且可执行
echo $? # #退出状态为0, stating that the file File_exam exists and is executable
chmod a-x File_exam # #将文件file_exam的可执行权限去除
Ls-l
[-E File_exam-a-x file_exam] # #再次测试文件file_exam是否存在且可执行
echo $? # #退出状态为1, stating that the file File_exam does not exist or is not executable

# #逻辑或
Integer1=15
["$integer 1"-lt 20-o "$integer 1"-gt "# #测试整数变量integer1是否小于20或大于30
echo $? # #退出状态为0, representing integer variables with an integer less than 20 or greater than 30

["$integer 1"-lt 10-o "$integer 1"-gt] # #测试整数变量integer1是否小于10或大于20
echo $? # #测试状态为1, which indicates that the integer variable integer1 is greater than 10 and less than 20


This article is from the "All Night" blog, please make sure to keep this source http://endmoon.blog.51cto.com/8921900/1616632

Test usage of 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.