P21
bash变量的类型:
本地变量
环境变量
位置变量:$1;$2等等;shift可用来轮替参数
特殊变量:$?(返回命令执行状态码);$#(显示有几个参数);$*(显示出所有的参数);$@(显示所有的参数)

bash -n 脚本 【可以测试脚本是否存在语法错误】
bash -x 脚本 【可以把每次执行的结果显示出来,也可用来判断脚本错误】
文件测试
-e FILE :测试文件是否存在
-f FILE:测试文件是否为普通文件
-d FILE:测试指定路径是否为目录
-r FILE:测试当前用户对指定文件是否有读取权限
-w FILE
-x FILE
多分支if语句:
if 判断条件; then
command1
……
elif 判断条件; then
command2
……
elif 判断条件; then
command3
……
else command4
fi
P22
p23
字符测试
== 测试是否相等,相等为真,不等为假
!= 测试是否不等,不等为真,等为假
>
<
-n 测试指定字符串是否为空,空则真,不空则假
-s 测试指定字符串是否不空,不空为真,空为假
循环
循环:进入条件,退出条件
for;while;until
整数列表生成
{1..100};seq 起始数 步长 结束数
declare -i sum=0 【指定sun是整数,默认情况下变量是字符】

1
#!/bin/bashLINE=`wc -l /etc/passwd|cut -d" " -f1`for I in `seq 1 $LINE`doecho "Hello, `sed -n "${I}p" /etc/passwd | cut -d: -f1`, your shell: `sed -n "${I}p" /etc/passwd | cut -d: -f7`"done
2
#!/bin/bashif [ $# -lt 1 ];thenecho "Usage adminuser ARG"exit 7fiINPUT=$1if [ $INPUT == "add" ];thenfor i in {1..10}doif ! id user${i} &> /dev/null ;thenuseradd -m user${i}echo "user${i}:user${i}" |chpasswd #指定密码为用户名称echo "Add user${i} finished"elseecho "user${i} exits"fidoneelif [ $INPUT == "del" ];thenfor i in {1..10}doif id user${i} &> /dev/null ;thenuserdel -r user${i}echo "Delet user${i} finished"elseecho "user${i} not exists"fidoneelseecho "input del or add"fi
3
#!/bin/bashsum=0for i in {1..100};doa=$[i%3]if [ $a -eq 0 ];thenlet sum=$[$sum+$i]fidoneecho "sum is $sum"
4
#!/bin/bashsum=0for i in {1..100};doa=$[i%2]if [ $a -eq 0 ];thenlet oushu=$[$oushu+$i]elselet jishu=$[$jishu+$i]fidoneecho "oushu is $oushu;jishu is $jishu"~~~~~~~~~~~~~~~~~~~~~~~~~~~·#!/bin/bashsum=0for i in {1..100};do# a=$[$i%2]if [ $[$i%2] -eq 0 ];thenlet oushu=$[$oushu+$i]elselet jishu=$[$jishu+$i]fidoneecho "oushu is $oushu;jishu is $jishu"
let I+=10 变量i在自身的基础上加一并放回原变量
let I+=1 等同于 I++
