#!/bin/bash # Name: del_ftp_user # Purpose: Demonstrate some Functions of kdialog # - It makes a list of users of the ftp group in a menu # - It shows the content of the user's home directory # and asks if it should be deleted as well # - It asks for confirmation of the delete operation # - If confirmed then it asks for the root password # - if password ok then it executes the operation # - It then confirms the operation # Syntax: del_ftp_user # #---------------------------------------------------- # Preparation: # users to delete must be from the group 'ftp' as primary group # tip: create users with the command: useradd -m -g ftp username #---------------------------------------------------- # Get all the ftp users into a menu ftpgid=$(grep "^ftp:" /etc/group | cut -d: -f3) ftpusers=$(grep ":$ftpgid:" /etc/passwd | grep -v "^ftp:" | cut -d: -f1) # Put the users in a kdialog menu index=1 for user in $ftpusers ; do menu="$menu $index $user" let index++ done #echo $menu #--------------- call the kdialog menu answer=$(/opt/kde3/bin/kdialog --menu "Select user to delete" $menu) #----------- exit if 'cancel' button is clicked [ $answer ] || exit 1 #echo $answer #----- Retrieve the user from the menu list. Learning && and break index=1 for user in $ftpusers ; do [ $answer -eq $index ] && break let index++ done #echo $user #------------ show the content of the home dir of the user # get the home dir of the user homedir=$(grep "^$user:" /etc/passwd | cut -d: -f6) xterm -e sh -c "ls -la $homedir | less" & #------------ get the PID of the xterm pid=$! #------------ ask if it should be deleted /opt/kde3/bin/kdialog --yesno "The content of the home directory '$homedir' of user '$user' is \ shown in a terminal.\nShould it also be deleted?" #echo $? #----------- ask for confirmation of the delete action if [ $? -eq 0 ]; then # Sure to delete the user and directory? /opt/kde3/bin/kdialog --yesno "Are you sure that you want to delete the user '$user'\n\ AND his home directory :$homedir: ?" [ $? -eq 0 ] && erase=dir else /opt/kde3/bin/kdialog --yesno "Are you sure that you want to delete the user '$user'\n\ but NOT his home directory '$homedir'?" [ $? -eq 0 ] && erase=user fi #echo $erase #exit #------------ kill the xterm kill $pid case $erase in dir) #------------ get the root password via kdialog passwd=$(/opt/kde3/bin/kdialog --password "Please enter root password") if ! (echo $passwd | su - -c "userdel -r $user") ; then /opt/kde3/bin/kdialog --error "ERROR: root password is incorrect.\n\ User '$user' and his directory '$homedir' will NOT be erased!" else /opt/kde3/bin/kdialog --msgbox "User '$user' and his home directory \ '$homedir' has been erased." fi ;; user) #------------ get the root password via kdialog passwd=$(/opt/kde3/bin/kdialog --password "Please enter root password") if ! (echo $passwd | su - -c "userdel $user") ; then /opt/kde3/bin/kdialog --error "ERROR: root password is incorrect.\n\ User $user will NOT be erased!" else /opt/kde3/bin/kdialog --msgbox "User '$user' has been erased." fi ;; *) /opt/kde3/bin/kdialog --msgbox "User '$user' and his home directory '$homedir'\n\ will NOT be erased" ;; esac