📁
notes
  • 目录
  • 嵌入式学习路线
  • Linux驱动开发
    • 字符设备驱动开发
    • Linux驱动开发
  • 安卓底层开发
    • 硬件抽象层
      • 安卓硬件驱动
      • 安卓硬件抽象层模块
      • 安卓硬件访问服务
      • 安卓应用
    • 智能指针
    • Logger日志系统
      • Logger日志
      • Logger写接口
      • Logcat
    • binder
      • binder
  • 操作系统
    • 操作系统的运行机制
    • 进程
  • linux一句话
    • 与网络无关的Linux
  • Linux内核0.12
    • 微型计算机组成结构
  • QT
    • 第一个QT
  • ubuntu
    • Linux使用
    • 使用notebook
    • 使用vscode
    • 使用ubuntu
    • Linux运行时格式
    • 配置samba进行win和Linux文件访问
  • shell
    • 自动打包
    • shell 概述
    • 变量
    • 条件测试
    • 控制
  • C
    • 第一条Linux_c输出
  • GTK
    • GTK入门
    • 控制件
    • 事件
    • Glade使用
  • sqlite
    • 数据库概述
    • SQL基础
    • SQLite的C编程
    • SQL语句进阶
  • web
    • CGI通用网关接口
  • 物联网
    • 物联网概述
    • 射频识别技术RFID
  • MySQL
    • MySQL的配置
  • 其他
    • Typora图床
Powered by GitBook
On this page
  • 条件测试语句
  • 文件
  • 字符串
  • 数字
  • 复合测试

Was this helpful?

  1. shell

条件测试

Previous变量Next控制

Last updated 4 years ago

Was this helpful?

Nnpxmj.png

条件测试语句

判断字符串是否相等, 可能还要检查文件状态或进行数字测试, 只有这些测试完成才能做下一步动作

test 命令: 用于测试字符串、 文件状态和数字

test 命令有两种格式:

test condition

使用方括号时, 要注意在条件两边加上空格

[ condition ]

shell 脚本中的条件测试如下:文件测试、 字符串测试、 数字测试、 复合测试

文件

文件测试: 测试文件状态的条件表达式

-e 是否存在

-d 是目录

-f 是文件

-r 可读

-w 可写

-x 可执行

-L 符号连接

-c 是否字符设备

-b 是否块设备

-s 文件非空

栗子 : test_file.sh

#!/bin/bash

test -e /dev/qaz
echo $?

test -e /home
echo $?

test -d /home
echo $?

test -f /home
echo $?

mkdir test_sh

chmod 500 test_sh
[ -r test_sh ]
echo $?

[ -w test_sh ]
echo $?

[ -x test_sh ]
echo $?

[ -s test_sh ]
echo $?

[ -c /dev/console ]
echo $?

[ -b /dev/sda ]
echo $?

[ -L /dev/stdin ]
echo $?

字符串

字符串测试

test str_operator “str”

test “str1” str_operator “str2”

[ str_operator “str” ]
[ “str1” str_operator “str2” ]

其中 str_operator 可以是:

= 两个字符串相等

!= 两个字符串不相等

-z 空串

-n 非空串

栗子 : test_string.sh

#!/bin/bash

test -z $yn
echo $?

echo "please input a y/n"

read yn

[ -z "$yn" ]
echo 1:$?

[ $yn = "y" ]
echo 2:$?

数字

测试数值格式如下:

test num1 num_operator num2
[ num1 num_operator num2 ]

num_operator 可以是 :

数值相等

-eq

数值不相等

-ne

数 1 大于数 2

-gt

数 1 大于等于数 2

-ge

数 1 小于等于数 2

-le

数 1 小于数 2

-lt

栗子 : test_num.sh

#!/bin/bash

echo "please input a num(1-9)"
read num

[ $num -eq 5 ]
echo $?

[ $num -ne 5 ]
echo $?

[ $num -gt 5 ]
echo $?

[ $num -ge 5 ]
echo $?

[ $num -le 5 ]
echo $?

[ $num -lt 5 ]
echo $?

复合测试

命令执行控制:

&&

command1 && command2

&&左边命令(command1) 执行成功( 即返回 0 ), shell 才执行 && 右边的命令(command2)

||

command1 || command2

||左边的命令(command1) 执行失败 ( 即返回非 0 ), shell 才执行 || 右边的命令(command2)

栗子 :

test -e /home && test -d /home && echo "true"

test 2 -lt 3 && test 5 -gt 3 && echo "equal"

test "aaa" = "aaa" || echo "not equal" && echo "equal"

多重条件判定

-a

( and )两状况同时成立! 同时具有 r 与 x 权限时, 才为 true.

test -r file -a -x file file

-o

(or)两状况任何一个成立! 具有 r 或 x 权限时, 就传回 true.

test -r file -o -x file file

!

相反状态 , 当 file 不具有 x 时, 回传 true

test ! -x file
  • @Author: cpu_code

  • @Date: 2020-07-31 09:46:09

  • @LastEditTime: 2020-08-01 14:25:26

  • @FilePath: \notes\shell\condition.md

image-20200801140435708
image-20200801140332504
image-20200731125644070
image-20200801140308980

@Gitee:

@Github:

@CSDN:

@Gitbook:

https://gitee.com/cpu_code
https://github.com/CPU-Code
https://blog.csdn.net/qq_44226094
https://923992029.gitbook.io/cpucode/