Tuesday 12 January 2016

Bash Scripting-Adding Users

Q: - Create a bash script for adding a user and modifying access rights



Code:-
 
#!/bin/bash
echo "shell script to create new user and modify access rights"
while true
do
echo "Enter your choice"
echo "1.Change ownership 2.Change access rights 3.Add user 4.delete user  5.Add group 6. Delete Group 7.Change group of user 8.exit"
read choice
case $choice in 
1)
echo "current folder contents are"
ls -l
echo "enter the name of the file for which you wish to change ownership"
read accfile
if [ -f $accfile ]
then
echo "enter the name of the new owner"
read owner
chown -v $owner $accfile
echo "ownership successfully  changed"
else
echo "incorrect file name"
fi
;;
2)
echo "current folder contains the following files"
ls -l
echo "enter the name of the file for which access rights are to be changed"
read accfile
if [ -f $accfile ]
then
echo "enter octal number for user rights"
read octal
chmod -v $octal $accfile
echo "status of folder after operation is as follows"
ls -l
else
echo "invalid file name"
fi
;;
3)
echo "Enter the name of the user to add"
echo "current user are"
grep '/home' /etc/passwd | cut -d: -f1
read user
sudo useradd $user
echo "after adding new user the current users are"
grep '/home' /etc/passwd | cut -d: -f1
;;
4)
echo "current user are"
grep '/home' /etc/passwd | cut -d: -f1
echo "enter the name of the user to delete"
read user
sudo userdel $user
echo "after adding new user the current users are"
grep '/home' /etc/passwd | cut -d: -f1
;;
5)
echo "enter the name of the group to add"
read grpname
sudo groupadd $grpname
;;
6)
echo "enter the name of the group to delete"
read grpname
sudo groupdel $grpname
echo "the $grpname has been deleted" 
;;
7)
echo "enter the name of the user for which the secondary group is to be added"
read user
echo "enter the group to which it is to be added"
read groupname
usermod -a -G $groupname $user
echo "the group has been added, displaying the contents of the /etc/group"
cat /etc/group
;;
8)
echo "terminating script"
exit
;;
*)
echo "Invalid choice, enter choice again"
esac
done


Output


[root@localhost asgn3]# sh asgn3.sh
shell script to create new user and modify access rights
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
3
current user are
exam10
hduser
Bash-Scripting-Adding-Usershduser1
student
Enter the name of the user to add
usernew
after adding new user the current users are
exam10
hduser
hduser1
student
usernew
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
1
current folder contents are
total 12
-rw-rw-r--. 1 student student 640 Aug 10 17:07 ab.cpp
-rw-rw-r--. 1 student student 1979 Aug 12 15:24 asgn3.sh
-rw-rw-r--. 1 student student 1979 Aug 12 15:24 asgn3.sh~
enter the name of the file for which you wish to change ownership
ab.cpp
enter the name of the new owner
usernew
changed ownership of `ab.cpp' from student to usernew
ownership successfully changed
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
2
current folder contains the following files
total 12
-rw-rw-r--. 1 usernew student 640 Aug 10 17:07 ab.cpp
-rw-rw-r--. 1 student student 1979 Aug 12 15:24 asgn3.sh
-rw-rw-r--. 1 student student 1979 Aug 12 15:24 asgn3.sh~
enter the name of the file for which access rights are to be changed
ab.cpp
enter octal number for user rights

777
mode of `ab.cpp' changed from 0664 (rw-rw-r--) to 0777 (rwxrwxrwx)
status of folder after operation is as follows
total 12
-rwxrwxrwx. 1 usernew student 640 Aug 10 17:07 ab.cpp
-rw-rw-r--. 1 student student 1979 Aug 12 15:24 asgn3.sh
-rw-rw-r--. 1 student student 1979 Aug 12 15:24 asgn3.sh~
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
5
enter the name of the group to add
groupnew
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
7
enter the name of the user for which the secondary group is to be added
usernew
enter the group to which it is to be added
groupnew
the group has been added, displaying the contents of the /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
mail:x:12:
uucp:x:14:
man:x:15:
games:x:20:
gopher:x:30:
video:x:39:
dip:x:40:
ftp:x:50:
slocate:x:21:
tcpdump:x:72:
ccache:x:988:
pulse:x:987:
pulse-access:x:986:
gdm:x:42:
stapusr:x:156:
stapsys:x:157:

stapdev:x:158:
exam10:x:1012:
tomcat:x:91:
hadoop:x:1015:
student:x:1017:
mega:x:1018:
usernew:x:1019:
groupnew:x:1020:usernew
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
4
current user are
exam10
hduser
hduser1
student
usernew
enter the name of the user to delete
usernew
after adding new user the current users are
exam10
hduser
hduser1
student
Enter your choice
1.Change ownership 2.Change access rights 3.Add user 4.delete user 5.Add group 6. Delete Group 7.Change group of user 8.exit
6
enter the name of the group to delete
groupnew
the groupnew has been deleted
8
terminating script
[root@localhost asgn3]#

No comments:

Post a Comment