Skip to content

antony@notes:~/misc$ cat "User-serach.md"

User serach

2022-08-08· misc

User serach

#查詢帳號(search.sh)

#!/bin/bash`
`read -p "please enter UserID: " uid`
`cat /etc/passwd | cut -d":" -f1 |grep $uid &>/dev/null`
`[ $? = 0 ] && echo "user exist" || echo "user not found"

新增單一/多個帳號(useradd.sh)

#!/bin/bash
echo ' please choose you want add one User or more then two User
1.add one User
2.add two or more User
'
read -p "you want 1/2?" cho
case $cho in
1)
read -p "enter UserID " uid
cat /etc/passwd | cut -d ":" -f 1 |grep $uid &>/dev/null
[ "$?" = "0" ] && echo "UserID exist" && exit
sudo useradd -m -s /bin/bash $uid
echo $uid:$uid | sudo chpasswd
echo "UserID add ok"
;;
2)
read -p "enter UserID title: " tuid
read -p "enter number of User start: " st
read -p "enter number of User stop: " so
a=$st
b=$so

for((no=$a;no<=$b;no=no+1))
do
cat /etc/passwd | cut -d ":" -f 1 |grep $tuid$no &>/dev/null
[ "$?" != "0" ] && sudo useradd -m -s /bin/bash $tuid$no && echo $tuid$no:$$tuid$no | sudo chpasswd && echo "$tuid$no add ok" || echo "$tuid$no exist"

done
;;
*)
echo "please enter what you want to do "
return
;;
esac

刪除單一/多個帳號(userdel.sh)

#!/bin/bash
echo ' please choose you want delete one User or more then two User
1.delete one User
2.delete two or more User
'
read -p "you want 1/2?" cho
case $cho in
1)
read -p "enter delete UserID " uid
cat /etc/passwd | cut -d ":" -f 1 |grep $uid &>/dev/null
[ "$?" != "0" ] && echo "UserID not found" && exit 1
sudo userdel -r $uid &>/dev/null
echo "UserID del ok"
;;
2)
read -p "enter delete UserID title: " tuid
read -p "enter number of User start: " st
read -p "enter number of User stop: " so
a=$st
b=$so

for((no=$a;no<=$b;no=no+1))
do
cat /etc/passwd | cut -d ":" -f 1 |grep $tuid$no &>/dev/null
[ "$?" = "0" ] && sudo userdel -r $tuid$no &>/dev/null && echo "$tuid$no del ok" || echo "$tuid$no not found"
done
;;
*)
echo "please enter what you want to do "
;;
esac

修改單一/多個帳號的密碼(chpasswd.sh)

#!/bin/bash
echo ' please choose you want change one User passwd or more then two User passwd
1.change one User passwd
2.change two or more User passwd
'
read -p "you want 1/2?" cho
case $cho in
1)
read -p "enter UserID " uid
cat /etc/passwd | cut -d ":" -f 1 |grep $uid &>/dev/null
[ "$?" != "0" ] && echo "User not found" && exit 1
sudo passwd $uid
echo "User passwd change ok"
;;
2)
read -p "enter UserID title: " tuid
read -p "enter number of User start: " st
read -p "enter number of User stop: " so
a=$st
b=$so

for((no=$a;no<=$b;no=no+1))
do
cat /etc/passwd | cut -d ":" -f 1 |grep $tuid$no &>/dev/null
[ "$?" != "0" ] && echo "User not found" && exit 1
echo $tuid$no:$tuid$no | sudo chpasswd && echo "User passwd change ok"
done
;;
*)
echo "please enter what you want to do "
;;
esac
Shell Script