条件判断 if 语法一: if 条件: # 条件成立时执行的子代码块 代码1 代码2 代码3 示例: sex='female' age=18 is_beautiful=True if sex == 'female' and age > 16 and age < 20 and is_beautiful: print('开始表白...') print('other code1...') print('other code2...') print('other code3...') 语法二: if…
1. 选择执行: 1.1. if 单分支的if语句: if 测试条件 then 代码分支 fi 双分支的if语句: if 测试条件; then 条件为真时执行的分支 else 条件为假时执行的分支 fi 示例:通过参数传递一个用户名给脚本,此用户不存时,则添加之: #!/bin/bash # if ! grep "^$1\>" /etc/passwd &> /dev/null; then useradd $1 echo $1 | passwd --stdin $1…