03-02-2015, 10:16 AM
I found this piece of code online, it was in Spanish originally and I have translated all of it (I think). I really like the fact that it's zenity and has the potential to be easy to use. This is a piece of code we can play with for now when we are bored :)
The adduser function was not working originally, I fixed that. The remove user function doesn't currently work, and I haven't tested any of the other features fully.
The adduser function was not working originally, I fixed that. The remove user function doesn't currently work, and I haven't tested any of the other features fully.
Code:
#!/bin/bash
# Gestion de usuarios grafica
# Bajo licencia GPL
function main(){
while [ true ]
do
option=`zenity --list --height=345 --width=420 --title "Managing Users and Groups" --radiolist --column="Select" --column="Option" TRUE "1 Create a new User" FALSE "2 Create Group" FALSE "3 Group Add User / Group" FALSE "4 Clear User / Group" FALSE "5 Remove a User" FALSE "6 Show all Users of a Group" FALSE "7 Exit"`
if [ "$?" == 1 ] ; then selection 7 ; fi
selection `echo $option | cut -c 1` "$option"
done
}
function selection(){
case $1 in
1)
user=`zenity --entry --width=300 --text "Username" --title "$2"`
cod=`echo $?`
if [ $cod == 0 ]
then
sudo useradd --shell=/bin/bash -m -G users,adm,cdrom,dip,plugdev,lpadmin,sambashare $user
if [ "$?" == 1 ]
then
zenity --warning --title="AddUser" --text="User $user already exists"
else
echo "User created $user" >> /tmp/log
progress "Creating user" $user "$2"
fi
elif [ $cod == -1 ]
then
zenity --error --no-wrap --title="Error" --text="An error occurred"
fi
;;
2)
group=`zenity --entry --width=300 --text "Group Name" --title "$2"`
cod=`echo $?`
if [ $cod == 0 ]
then
creategroup "$group"
if [ "$?" == 0 ]
then
progress "Creating group" $group "$2"
echo "The group is created $group" >> /tmp/log
fi
elif [ $cod == -1 ]
then
zenity --error --no-wrap --title="Error" --text="An error occurred"
fi
;;
3)
listaUser=`cat /etc/passwd | cut -d: -f1 | sort`
user=`zenity --list --height=300 --title="Choose a user" --text="Double click to select" --column="User" $listaUser`
cod=`echo $?`
if [ $cod == 0 ]
then
lsgrp=`cat /etc/group | cut -d: -f1 | sort`
grp=`zenity --list --height=300 --title="Choose a group" --text="Double click to select" --column="Groups" $lsgrp`
sudo usermod -aG $grp $user
zenity --info --no-wrap --title="$2" --text="Was added '$user' the group $grp"
echo "Was added a $user the group $grp" >> /tmp/log
elif [ $cod == -1 ]
then
zenity --error --no-wrap --title="Error" --text="An error occurred"
fi
;;
4)
listaUser=`cat /etc/passwd | cut -d: -f1 | sort`
user=`zenity --list --height=300 --title="Choose a user" --text="Double click to select" --column="USER" $listaUser`
cod=`echo $?`
if [ $cod == 0 ]
then
lsgrp=`cat /etc/group | cut -d: -f1 | sort`
grp=`zenity --list --height=300 --title="Choose a group" --text="Double click to select" --column="GROUPS" $lsgrp`
sudo userdel -rf $user $grp
zenity --info --no-wrap --title="$2" --text="Was removed in '$user' del group $grp"
echo "Was removed in $user del group $grp" >> /tmp/log
elif [ $cod == -1 ]
then
zenity --error --no-wrap --title="Error" --text="An error occurred"
fi
;;
5)
listaUser=`cat /etc/passwd | cut -d: -f1 | sort`
user=`zenity --list --height=300 --title="Choose an option" --text="Double click to select" --column="USER" $listaUser`
cod=`echo $?`
if [ $cod == 0 ]
then
zenity --question --title="Delete User" --text="Clear the Home directory $user?"
if [ $? == 0 ]
then
sudo userdel -rf $user $grp
echo "The user $user was removed along with the directory /home/$user" >> /tmp/log
progress "Removing User" $user "$2"
else
sudo userdel -rf $user $grp
echo "User was removed $user but remains /home/$user" >> /tmp/log
progress "Removing User" $user "$2"
fi
elif [ $cod == -1 ]
then
zenity --error --no-wrap --title="Error" --text="An error occurred"
fi
;;
6)
lsgrp=`cat /etc/group | cut -d: -f1 | sort`
grp=`zenity --list --height=300 --title="Select group" --text="Double click on the group" --column="GROUPS" $lsgrp`
cod=`echo $?`
if [ $cod == 0 ]
then
member=`cat /etc/group | grep $grp: | cut -d: -f4 | sort`
zenity --info --no-wrap --title="$2" --text="The group $grp has as members: $member"
echo "Members were listed $grp" >> /tmp/log
elif [ $cod == -1 ]
then
zenity --error --no-wrap --title="Error" --text="An error occurred"
fi
;;
7)
if [ -e /tmp/log ]
then
zenity --text-info --title="User Manager Log" --filename=/tmp/log --editable 2>/tmp/tmp.txt
rm /tmp/log /tmp/tmp.txt
fi
exit
;;
esac
}
function progress(){
(
echo "0" ; sleep 1
echo "# $1" ; sleep 1
echo "25" ; sleep 1
echo "75" ; sleep 1
echo "100" ; sleep 1
echo "# Finalized"
) |
zenity --progress --title="$3" --text="" --percentage=0 --width=300
if [ "$?" = -1 ] ; then
zenity --error --text="Collect Aborted."
fi
}
function creargroup(){
gksudo groupadd $1
if [ "$?" == 9 ]
then
zenity --warning --title="Groupadd" --text="The group '$1' already exists"
return 1
else
return 0
fi
}
mainDownload your free copy of Linux Lite today.
Jerry Bezencon
Linux Lite Creator
"Do not correct a fool, or he will hate you; correct a wise man and he will appreciate you."
![[Image: X5qGkCg.png]](https://imgur.com/X5qGkCg.png)

