I need to create a Bash script to remove a user.
We use RHEL version 4, 5, & 6.
Lets say usernames are Ray4 & Ray6 & the script name is deal.
Specific tasks for this script are:
- Does the user exist ?
- If user exists, backup /homedirectory for this user, remove username and place in /root/DeletedUsers
- If /root/DeletedUsers directory doesn't exist, create it.
- If any firewall rules exist for this user, email me the results for those rules and on which nodes.
- If this user exists in sudoers, don't delete, but comment out.
This is what I have so far. I want to make sure this works, before I run it in RHN Satellite. After making the suggested changes. Here are the new errors I am getting now.
[root@localhost bin]# ./deal
./deal: line 7: [[!: command not found
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files,
even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files,
even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
Null message body; hope that's ok
./deal: line 22: [: -me: binary operator expected
This is source code:
[root@localhost bin]# cat -n deal
1 #!/bin/bash
2
3 count=$(egrep -c Ray[46] /etc/passwd)
4 firewall=$(grep -c "192.168.5.5" /etc/sysconfig/iptables)
5 doers=$(egrep -c Ray[46] /etc/sudoers)
6
7 if [[! -d /root/DeletedUsers]]
8 then mkdir /root/DeletedUsers
9
10 fi
11
12 cp -Rf /home/Ray[46] /root/DeletedUsers
13 userdel -rf Ray [4]
14 userdel -rf Ray [6]
15
16 if [ $firewall -ne 0 ]
17
18 then mail -s "$firewallrulesexist" emailaddress < /dev/null
19
20 fi
21
22 if [ $doers -me 0 ]
23 then sed ^Ray[46] /#/i
24
25 EOF
26 fi
if [[ $firewall -ne 0 ]]
toif [ $firewall -ne 0 ]
. And similarly, changeif [[ $doers -me 0 ]]
toif [ $doers -ne 0 ]
. – Ramesh Apr 25 '14 at 14:53DELETED USERS
directory? If it is under current directory, then no need of` and also use
"` to create the directory. Something like,mkdir "DELETED USERS"
. – Ramesh Apr 25 '14 at 14:56-count
to-d
inif [[! -count /Deleted_Users]]
– Creek Apr 25 '14 at 15:13[[! -d /root/DeletedUsers]]
you want! [ -d /root/DeletedUsers ]
. Or, honestly, if you usemkdir -p
, that won't error if it already exists, so you can omit the test. – derobert Apr 25 '14 at 16:17