linux中循环控制语句,linux shell脚本用到循环、控制语句

匿名用户

1级

2014-05-09 回答

#!/bin/sh

# Name: useraddmuti

# Descripton: To add users to your system. Users can be list in a file.

# To exec this command your ID must be 0.

# Author: PopZslam@Linux.net

#-----------------------------------

chkUID(){

getUID(){

id|sed -e 's/(.*$//' -e 's/^uid=//'

}

if [ "`getUID`" -ne 0 ]

then

echo -e "\tYou are not root!"

exit 0

fi

}

chkUID

usagePRT(){

echo ${USAGE:='USAGE:' `basename $0` '-f namelistfile'}

}

chkFILE(){

if [ ! -z "`awk 'NF!=2{print NF;exit;}' $1`" ] && [ "`awk 'NF!=2{print NF;exit;}' $1`" -ne 2 ] ; then

echo -e "The file's format is not right!"

exit 0

fi

}

userCHK(){

for USER in `awk '{print $1;}' $1`

do

if grep -wq $USER /etc/passwd ; then

echo -e "The user($USER) has been added!"

exit 1

fi

if echo $USER|grep -wq "^[0-9].*" ; then

echo -e "The user($USER)'s name is wrong format!"

exit 1

fi

done

}

setOPT(){

echo -e "Now Let's set some options or you can use default settings."

setGRPNAME(){

while :

do

echo -e "Would you like to add a new group to add these users to it?"

echo -e "Enter YES to create a new group otherwise you must verify the group."

printf "Your Answer: "

read grpopt

case $grpopt in

yes)

printf "Please enter the group's name: "

read grpoptnew

if cat /etc/group|sed 's/:.*//'|grep -wq $grpoptnew ; then

echo "The group's name($grpoptnew) exist."

exit

else

grpname=$grpoptnew

echo -e "All these users will be added to group($grpname)..."

echo -e "Adding group ..."

if cp /etc/group /etc/group.$$ >; /dev/null 2>;&1 ; then

if groupadd $grpname ; then

echo -e "The group($grpname) is added!"

rm -f /etc/group.$$

break 1

else

echo -e "There's something wrong when adding the group($grpname)."

echo -e " *** Please recovered the group file. *** "

echo -e "You can cp /etc/group.$$ to /etc/group to recover."

fi

else

echo "Error! Please check the program or your disk space."

exit 0

fi

fi

;;

*) : ;;

esac

done

}

setGRPNAME

}

addUSER(){

if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then

for user in `sed 's/ .*//' $1`

do

pass=`awk '{

$1~/$name/

{print $2;exit}

} name=$user' $1`

if [ -z "$pass" ] ; then

echo -e "The passwd is used by default sun123."

pass=sun123

fi

if [ ${#pass} -lt 6 ] ; then

echo -e "The user($user)'s password is too short!"

echo -e "Use default password: sun123."

pass=sun123

fi

if useradd $user ; then

echo -e "The user($user) is added."

if echo $pass|passwd $user --stdin >; /dev/null 2>;&1 ; then

echo -e "The user($user)'s password is setted!"

else

echo -e "The user($user)'s password is NOT set!"

fi

else

echo -e "The user($user) is NOT add."

fi

done

rm -f /etc/passwd.$$ /etc/shadow.$$

else

echo -e "There something wrong when backup the passwd and shadow file."

fi

}

if [ $# -ne 2 ] ; then

usagePRT

exit 0

fi

case "$1" in

-f)

if [ -f "$2" ] ; then

echo -e "Reading usernamelist file""("$2")" "..."

chkFILE $2

userCHK $2

setOPT

addUSER $2

else

echo -e "There's no usernamelist file!"

fi

;;

*) usagePRT

exit 0

;;

esac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值