第七周
Shell脚本
避免交互
原来
passwd hcz01
现在
echo "123" | passwd -stdin hcz01
传递参数
原来
- 用户名写在程序里,不灵活
现在
- 用户名作为参数传递给脚本
Shell的命令行参数
- $# (参数个数)
- $0 (脚本文件名)
- $1、$2、$3、$4…… (参数列表)
示例:
#! /bin/bash
useradd -s /sbin/nologin hcz01
echo "hcz01" | passwd --stdin hcz01
chmod o+x /home/ hcz01
#! /bin/bash
useradd -s /sbin/nologin $1
echo "$1" | passwd --stdin $1
chmod o+x /home/ $1
循环添加
- for
- while
for i in hcz01 hcz02 hcz03
do
useradd ${i}
done
for i in {01..50}
do
useradd hcz${i}
done
for i in `cat UserList.txt`
do
useradd ${i}
done
for i in $(cat UserList.txt)
do
useradd ${i}
done
#! /bin/bash
#从UserList.txt中读取数据创建用户, UserList.txt中每行一个用户名
groupadd students
for UserName in $(cat UserList.txt)
do
useradd -g students -s /sbin/nologin ${UserName}
echo ${UserName} | passwd --stdin ${UserName}
chmod o+x /home/${UserName}
done