---
# System prepended metadata

title: BASH_建立帳號（選單）
tags: [BASH]

---

---
tags: BASH
---
# BASH_建立帳號（選單）

```bash=
#!/bin/bash
[ "$USER" != "root" ] && echo "need root to run" && exit

while :
do

echo -e '選單
         1.查詢帳號
         2.建立單一帳號
         3.建立系列帳號
         4.刪除帳號
         5.刪除系列帳號
         6.更改單一帳號之密碼
         7.更改系列帳號之密碼
         8.建立單一帳號的 sudo 權限
         9.建立系列帳號的 sudo 權限
         10.離開'
         
read -p "請輸入選項 1-10 :"  number
case $number in

 1)     
	#!/bin/bash
	read -p "請輸入帳號名稱:" account
	cat /etc/passwd | cut -d ":" -f1 | grep -w ${account} &> /dev/null
	if [ $? = 0 ];then
	  echo "已存在此帳號!"
	else
	  echo "無此帳號!"
	fi
	
	read -p "請按 Enter 鍵離開"
    ;;
	
 2)
	#!/bin/bash
	read -p "請輸入帳號名稱:" account
	cat /etc/passwd | cut -d ":" -f1 | grep -w ${account} &> /dev/null
	if [ $? = 0 ];then
	  echo "已存在此帳號!"
	else
	  sudo useradd -m -s /bin/bash ${account}
	  echo "${account}:${account}" > pswd.txt
	  sudo chpasswd < pswd.txt
	  echo "帳號 ${account} 已建立"
	fi
	
	read -p "請按 Enter 鍵離開"
	;;
	
 3)
 	#!/bin/bash
	declare -i a z
	read -p "請輸入帳號名稱:" account
	read -p "請輸入起始值:" a
	read -p "請輸入尾數:" z
	while [ $a -le $z ]
	do
	  cat /etc/passwd | cut -d ":" -f1 | grep -w ${account}${a} &> /dev/null
	  if [ $? = 0 ];then
		echo "已存在帳號"${account}${a}
	  else
		acc=$account$a
		sudo useradd -m -s /bin/bash ${acc}
		echo "${acc}:${acc}" > pswd.txt
		sudo chpasswd < pswd.txt
		echo ${acc}"帳號已建立"
	  fi
	  a=$a+1
	done

	read -p "請按 Enter 鍵離開"
    ;;
	
 4)
 	#!/bin/bash
	read -p "請輸入帳號名稱:" account
	cat /etc/passwd | cut -d ":" -f1 | grep -w ${account} &> /dev/null
	if [ $? = 0 ];then
	  sudo userdel -r ${account} &> /dev/null
	  echo "帳號已刪除!"
	else
	  echo "不存在此帳號!"
	fi

	read -p "請按 Enter 鍵離開"
	;;
	
 5)
	#!/bin/bash
	declare -i a z
	read -p "請輸入帳號名稱:" account
	read -p "請輸入起始值:" a
	read -p "請輸入尾數:" z
	while [ $a -le $z ]
	do
	  cat /etc/passwd | cut -d ":" -f1 | grep -w ${account}${a} &> /dev/null
	  if [ $? = 0 ];then
		acc=$account$a
		sudo userdel -r ${acc} >/dev/null 2>&1
		echo $acc"帳號已刪除"
	  else
		echo "不存在帳號"${account}${a}
	  fi
	  a=$a+1
	done

	read -p "請按 Enter 鍵離開"
	;;

 6)
	#!/bin/bash
	read -p "請輸入想修改的帳號:" acc
	read -p "請輸入新密碼:" pswd1
	read -p "請再次輸入新密碼:" pswd2
	if [ ${pswd1} = ${pswd2} ];then
	  cat /etc/passwd | cut -d ":" -f1 | grep -w ${acc} &> /dev/null
	  if [ $? = 0 ];then
		echo "${acc}:${pswd1}" > chpswd.txt
		sudo chpasswd < chpswd.txt
		echo ${acc}"密碼已修改"
	  else
		echo ${acc}"帳號不存在"
	  fi
	else
	  echo "密碼未一致"
	fi

	read -p "請按 Enter 鍵離開"
	;;

 7)
	#!/bin/bash
	declare -i a z
	read -p "請輸入想修改的帳號:" acc
	read -p "請輸入起始值:" a
	read -p "請輸入尾數:" z
	read -p "請輸入想修改的密碼:" pswd1
	read -p "請再次輸入新密碼:" pswd2
	if [ ${pswd1} = ${pswd2} ];then
	  while [ $a -le $z ]
	  do
		cat /etc/passwd | cut -d ":" -f1 | grep -w ${acc}${a} &> /dev/null
		if [ $? = 0 ];then
		  echo "${acc}${a}:${pswd1}${a}" > chpswd.txt
		  sudo chpasswd < chpswd.txt
		  echo ${acc}${a}"密碼已修改，新密碼為您輸入的內容加帳號尾數"
		else
		  echo ${acc}${a}"帳號不存在"
		fi
		a=$a+1
	  done
	else
	  echo "密碼未一致"
	fi

	read -p "請按 Enter 鍵離開"
	;;
	
 8)
	#!/bin/bash
	read -p "請輸入帳號:" acc
	cat /etc/passwd | cut -d ":" -f1 | grep -w ${acc} &> /dev/null
	if [ $? = 0 ];then
	  sudo usermod -a -G sudo ${acc}
	  echo ${acc}"已獲權限"
	else
	  echo "不存在帳號"${acc}
	fi

	read -p "請按 Enter 鍵離開"
	;;
	
 9)
	#!/bin/bash
	declare -i a z
	read -p "請輸入帳號:" acc
	read -p "請輸入起始值:" a
	read -p "請輸入尾數:" z
	while [ $a -le $z ]
	do
	  cat /etc/passwd | cut -d ":" -f1 | grep -w ${acc}${a} &> /dev/null
	  if [ $? = 0 ];then
		sudo usermod -a -G sudo ${acc}${a}
		echo ${acc}${a}"已獲權限"
	  else
		echo "不存在帳號"${acc}${a}
	  fi
	  a=$a+1
	done

	read -p "請按 Enter 鍵離開"
	;;

 10)    
 	break
    ;;
    
 *)     echo "請選1-10"

esac
done
echo "Bye"
```