「linux学习」之批量创建用户脚本
#! /bin/bash
if [ $# -eq 0 -o $# -gt 2 ];then # 判断是否输入参数及参数输入是否上限
echo '/root/foo.sh redhat fedora' >&2 修改为错误输出
exit 3
elif [ $1 == redhat ];then
echo fedora
elif [ $1 == fedora ];then
echo redhat
else
echo '/root/foo.sh redhat fedora' >&2 修改为错误输出
exit 5 退出并设定返回错误码
fi
批量创建用户脚本:
#! /bin/bash
for i in `$1`
do
useradd $i
echo $i 创建成功
done
授权:chmod +x /root/batcusers.sh
/root/batcusers.sh /root/batcusers.sh
脚本优化:
! /bin/bash
if [ $# -eq 0 ];then
echo 'Usage: /root/batchusers' >&2
exit 2
elif [ -f $1 ];then
for i in `cat $1`
do
useradd -s /bin/false $i #指定shell解释器创建用户
echo $i 创建成功
done
else
echo 'Input file not found' >&2
exit 3
fi