LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2006, 05:43 AM   #1
serris
LQ Newbie
 
Registered: Oct 2006
Distribution: Fluxbuntu
Posts: 2

Rep: Reputation: 0
Xdialog checklist help


Hi, I'm writing a shell script with Xdialog. I've read several guides and downloaded several scripts to see how to do it but I got no where.

What I'm trying to do is use a checklist to select various options in my script.

Code:
Xdialog --backtitle "$BGTITLE" \
        --title "Remove user" \
        --checkist "Please select the options you'd like to remove the user with" 0 0 2 \
 "Remove" "Are you sure you want to remove this user?" off \
 "Directory" "Remove users directory?" off 2>$TEMP
That is the code that I have at the moment. What I want to achieve is that if the Remove check is selected continue with the script removing the user specified earlier as $UNAME via a inputbox. Else it will show an error message. Also if the Directory check is selected, also remove the home directory of the user.

I've read several examples (such as the XDialog home page) how to do it but non of the examples help me implement --checklist in my script.

Any help would be much appreciated.
Thank you in advance
 
Old 10-29-2006, 09:21 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,409
Blog Entries: 55

Rep: Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582
Hello and welcome to LQ. Hope you like it here.

What I would do is start by setting up an outline for what you want to achieve using shell commands and review the options the commands allow. Then get input for any options the commands may allow. Then add error checking for each option and for dialog's exit values. End with the final "do you really want to X" question before execution. If you do not check execution exit values and feed them back in a dialog you should not consider your script finished.

Here's a crude example using gdialog since I don't have xdialog (principle is the same). I've added some error checking so you get the basic idea. If you however manage to do stuff like outlined above you would have much easier time coding which should result in better readability and easier maintainability. This is a kludge:

Code:
#!/bin/sh - 
# Purpose: "Remove account" dialog example
# Args: none
# Variables. Change these.
dialog=gdialog

# Variables.
progn=${0//*\//}
tmpf="${TMP:="/tmp/${progn}"}.tmp"
msg=( "Napalm. Smell. Morning." "Wisely choose, you should." "Whatever." \
"OMG! PONIES!" "Bah!" "WTF?" "FCOL, NOT AGAIN!" "Ill hit YOU one more time!" \
"You owe me USD `date +%s`!" "Your ${RANDOM}th moronic move..." )

# Functions.
rmTemp() { [ -f "${tmpf}" ] && rm "${tmpf}"; }

selectUser() { # Select username and do some checks.
rmTemp
$dialog --title "Title" --inputbox "${mesg}Select user to remove" 10 10 2> "${tmpf}"
# Did the user click "OK"?
if [ $? -ne 0 ]; then rmTemp; exit 1; fi
USER=($(< "${tmpf}"))
# Is the variable filled?
if [ $? -eq 0 -a ${#USER[@]} -eq 0 -o ${#USER[@]} -gt 1 ]; then
 # No, rerun. Entertain user ;-p
 mesg="${msg[${RANDOM:0:1}]}\n"; selectUser
else
 # Yes. Let's prepare for checks.
 USER=${USER[0]}
 # Minimal username length
 if [ ${#USER} -lt 3 ]; then
  # Rerun
  mesg="${msg[${RANDOM:0:1}]}\n"; selectUser
 fi
 # Garbage input
 good_chars="a-zA-Z0-9_-"
 if [ "${USER//[^$good_chars]/}" != "${USER}" ]; then
  # Rerun
  mesg="${msg[${RANDOM:0:1}]}\n"; selectUser
 fi
 # Get account specs from passwd.
 USERSPECS=$(getent passwd $USER)
 USERSPECS=${USERSPECS// /_}; USERSPECS=${USERSPECS//::/:NOVALUE:}
 USERSPECS=${USERSPECS//:/ }; USERSPECS=(${USERSPECS[@]})
 # Get value of unprivileged users.
 SYSUIDS=($(grep ^UID_MIN /etc/login.defs 2>/dev/null))
 SYSUIDS=${SYSUIDS[1]}
 # Fill with reasonable default if value is empty.
 if [ -z $SYSUIDS ]; then SYSUIDS=500; fi
 # Check if user is a privileged slash system user.
 if [ ${USERSPECS[2]} -le $SYSUIDS ]; then
  mesg="Can't remove privileged users.\n"; selectUser
 fi
 # Check where $USER hangs his hat (/home).
 case "${USERSPECS[5]}" in
  /home/*) USER_NAME=${USERSPECS[0]}; USER_HOME=${USERSPECS[5]};;
  *) mesg="Home ("${USERSPECS[5]}") is not in /home.\n"; selectUser;;
 esac
fi
rmTemp
}

selectOpts() { # Username confirmation and option remove $HOME.
rmTemp
if [ -z $USER_NAME -o -z $USER_HOME ]; then exit 127; fi
$dialog --title "Title" --checklist \
"${mesg}Please select the options you'd like to remove the user with" 10 10 2 \
 r0 "Are you sure you want to remove user $USER_NAME?" off \
 r1 "Remove users home $USER_HOME?" off 2> "${tmpf}" 
if [ $? -eq 0 ]; then
 opts=($(<"${tmpf}")); opts=${opts[@]//\"/}
 # Is them variables filled?
 if [ $? -eq 0 -a ${#opts[@]} -ge 0 ]; then
  for opt in ${opts[@]}; do case "${opt}" in
  r0) DEL=YES;; r1) USERDEL_FLAGS="-r";; *) ;;
  esac; done
  # We're good to go. Don't do stuff but echo.
  # Note exit values should be checked and fed back in a $dialog.
  case "$DEL" in YES) echo "userdel ${USERDEL_FLAGS} $USER_NAME";; esac
 else
  # No, rerun. Entertain user some more.
  mesg="${msg[${RANDOM:0:1}]}\n"; selectOpts
 fi
fi
rmTemp
}

selectUser
selectOpts

exit 0

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/; }
 
Old 10-31-2006, 02:57 AM   #3
serris
LQ Newbie
 
Registered: Oct 2006
Distribution: Fluxbuntu
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you for the quick reply. I just posted a snippet of the entire script so I had covered most of the other things (such as error handling). I haven't yet had the time to look into implementing your example (or the method more like) into my script due to work and other things but I will more than likely give it a shot tonight. Thank you for also showing me the error handling if someone does not put anything into the inputbox.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Winetools and Xdialog kerryhall Linux - Software 2 04-28-2006 02:38 PM
Need help with Xdialog justintime32 Linux - Software 1 06-17-2005 02:17 PM
Using Xdialog to su death_au Linux - Newbie 1 05-31-2005 10:41 PM
Xdialog --geometry bendeco13 Linux - General 0 11-06-2004 12:20 AM
Xdialog --progress bendeco13 Linux - General 0 10-27-2004 08:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration