본문 바로가기

Linux/Exercise

쉘프로그래밍 기초 3 - 연습용

문제 8] 다음과 같이 사용자를 추가하고 삭제하는 스크립트를 작성하시오.
 
1. 사용자 추가
2. 사용자 삭제
번호를 선택하세요. : 1
사용자 이름을 선택하시오. ex) test1 : test1
사용자 홈 디렉토리를 입력하시오. ex) /home/test1 : /home/test1
사용자의 쉘을 입력하시오. ex) /bin/bash : /bin/bash
사용자의 암호를 입력하시오. ex) passwd : flsnrtm
Changing password for user test1.
passwd: all authentication tokens updated successfully.
 
test1 사용자를 추가하였습니다.

---------------------------------------------------------
 
1. 사용자 추가
2. 사용자 삭제
번호를 선택하세요. : 2
삭제할 사용자 이름을 입력하시오. ex) test1 : test1
test1 사용자를 삭제하였습니다.
 
# vi useradd.sh
---------------------------------------------------------
#!/bin/bash
echo " "
echo "1. 사용자 추가"
echo "2. 사용자 삭제"
echo -n "번호를 선택하세요. : "
read num
if [ $num = 1 ]
then
        echo -n "사용자 이름을 선택하시오. ex) test1 : "
        read user
        echo -n "사용자 홈 디렉토리를 입력하시오. ex) /home/test1 : "
        read dir
        echo -n "사용자의 쉘을 입력하시오. ex) /bin/bash : "
        read shell
        echo -n "사용자의 암호를 입력하시오. ex) passwd : "
        read pass
        useradd -d $dir -s $shell $user
        echo $pass | /usr/bin/passwd --stdin $user
        echo " "
        echo "$user 사용자를 추가하였습니다."
        echo " "
        exit
elif [ $num = 2 ]
then
        echo -n "삭제할 사용자 이름을 입력하시오. ex) test1 : "
        read del
        userdel -r $del
        echo "$del 사용자를 삭제하였습니다."
        echo " "
        exit
else
        echo " "
        echo -n "잘못된 입력을 하셨습니다. (1 or 2)"
        echo " "
        sh useradd.sh
        exit
fi

내가 한 방법

#!/bin/bash
echo " "
echo "1. 사용자 추가"
echo "2. 사용자 삭제"
echo -n "번호를 선택하세요. : "
read num
if [ $num = 1 ]
then
        echo -n "사용자 이름을 선택하시오. ex) test1 : "
        read user
        echo -n "사용자 홈 디렉토리를 입력하시오. ex) /home/test1 : "
        read dir
        echo -n "사용자의 쉘을 입력하시오. ex) /bin/bash : "
        read shell
#       echo -n "사용자의 암호를 입력하시오. ex) passwd : "
#       read pass
        useradd -d $dir -s $shell $user
#       echo $pass | /usr/bin/passwd --stdin $user
        echo "사용자의 암호를 입력하시오. ex) passwd : "
        passwd --stdin $user
        echo " "
        echo "$user 사용자를 추가하였습니다."
        echo " "
        exit
elif [ $num = 2 ]
then
        echo -n "삭제할 사용자 이름을 입력하시오. ex) test1 : "
        read del
        userdel -r $del
        echo "$del 사용자를 삭제하였습니다."
        echo " "
        exit
else
        echo " "
        echo -n "잘못된 입력을 하셨습니다. (1 or 2)"
        echo " "
        sh useradd.sh
        exit
fi
---------------------------------------------------------


문제 9] 아래와 같은 스트립트를 만들어라.
단 하나의 메뉴마다 실행 후 5초 후에 다시 주메뉴가 나오도록 while, case 를 사용해서 작성하시오.

 
===================
  User Admin Util
===================
 
1. Login Information
2. User Information
3. User Add
4. User Del
5. Util Exit
 
Enter the number (1,2,3,4,5 <enter>) :

# vi useradd.sh
---------------------------------------------------------
#!bin/bash
while [ : ]
do
clear
echo " "
echo "==================="
echo "  User Admin Util"
echo "==================="
echo " "
echo "1. Login Information"
echo "2. User Information"
echo "3. User Add"
echo "4. User Del"
echo "5. Util Exit"
echo " "
echo -n "Enter the number (1,2,3,4,5 <enter>) : "
read num

case $num in
        1)
        echo " "
        echo "login inforation"
        echo " "
        w
        sleep 5;;

        2)
        echo " "
        echo " user information"
        echo " "
        echo -n "Enter the user name : "
        read user
        if [ "$user" = "" ]
        then
        echo "retry enter the name"
        else
        echo "----/etc/passwd info --------"
        echo `cat /etc/passwd | grep $user`
        sleep 5
        fi;;

     3)
        echo " "
        echo "user add"
        echo " "
        echo -n "username : "
        read user
        echo -n "Enter the $user passwd? "
        read pass
        if [ "$user" = "" ]
        then
        echo "retry enter the name"
        else
        useradd $user
        echo $pass | /usr/bin/passwd --stdin $user
        echo " "
        echo "$user add completed!!"
        fi;;

        4)
        echo " "
        echo "user del"
        echo " "
        echo -n "deleted user name : "
        read user
        if [ "$user" = "" ]
        then
        echo "retry enter the name"
        else
        userdel -r $user
        echo " "
        echo "$user delete completed!!"
        fi;;

        5)
        echo " "
        echo "util exit"
        break;;
esac
done
---------------------------------------------------------