shell增删改查用户脚本练习

127次阅读
没有评论

共计 782 个字符,预计需要花费 2 分钟才能阅读完成。

#!/bin/bash
if [$UID -ne 0];then
 echo "oline root"
 exit 9
fi

Path=/etc/user.conf
if [! -f $Path];then
 touch $Path
fi
chmod 700 $Path

if [2 -ne $#];then #-eq == -ne !=
 echo "you must be input user and password!"
 exit 2
fi

Age=$1
User=$2

if [0 -lt $(echo "$User"|grep -E "[^a-z,A-Z,0-9,_]"|wc -l) ];then
 echo "The age format is error"
 exit 2
fi

funCheckUser(){check=$(grep -w "$User" $Path|wc -l)
 if [$check -eq 0];then
 return 0
 else 
 return 1
 fi
}

funAddUser(){
 funCheckUser
 if [$? -eq 0];then
 echo "$User" >> $Path
 else
 echo "the $User is true" 
 exit 3
 fi
}

funDeluser(){
 funCheckUser
 if [$? -eq 0];then
 echo "no find this is $User"
 else
 sed -ri /^$User$/d $Path
 exit 4
 fi 
}

funSearch(){
 funCheckUser
 if [$? -eq 0];then
 echo "there is a $User name is NO find!"
 else
 echo "thers is a $User name is true"
 fi
}
case $Age in
 -add)
 funAddUser
 ;;
 -del)
 funDeluser
 ;;
 -search)
 funSearch
 ;;
 *)
 echo "USAGE: sh adduser (-add|-del|-search) username!" 
 ;;
esac
正文完
 0
yx
版权声明:本站原创文章,由 yx 于2018-04-20发表,共计782字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码