shell-流程控制

1.条件结构

测试-----test 条件
#条件为真返回 0,条件为假返回 1 #语法------[ 条件 ]
test 能够理解3中类型的表达式 
1.文件测试
2.字符串比较
3.数字比较

字符串

 -n STRING
    the length of STRING is nonzero
    -n  字符串的长度 不是零成功。
 -z STRING
    the length of STRING is zero
     -z   字符串长度。是零成功 #对于未定义或赋予空值的变量将是为空串。
 STRING1 = STRING2  (等于)
           the strings are equal
 STRING1 != STRING2  (不等于)
           the strings are not equal

break :中止循环

exit 0:正常退出脚本

exit 非0:错误退出脚本

•exit语句:退出程序的执行,并返回一个返回码,返回码为0正常退出,非0为非正常退出,例如: 
•exit 0

-z:
# vim string.sh
#!/usr/bin/bash
while : #:默认值为真
do
read -p "请输入你的密码: " a
pass=123456
if [ -z $a ];then
        echo "您输入的密码不能为空"
        exit 1
else
        if [ $a = $pass ];then
                echo "登录成功"
                break
        else
                echo "您的密码输入有误,请重新输入"
        fi
fi
done

-n:
[root@bogon yy]# cat nz.sh
#!/bin/bash
while true(while :)
do
read -p "请输入密码:" a
pass=12345
  if [ -n "$a" ];then
      if [ "$a" = "$pass" ];then
          echo "登入成功&

你可能感兴趣的:(shell,linux,服务器,运维)