Tag Archives: Bash

Sample debug output code for BASH/SHELL

When trying to debug your own scripts it can be difficult to see exactly what you want.  Pages of white text scrolling across the screen.  Here is a quick flexible function that can make life a whole lot easier by setting different debug values and instead of using echo, use the function name.

    #################################################
    # Variable Declarations
    #
    #What output to show on screen
    #debug levels:
    # 0 is no debug or logging.
    # 1 is basic output only (gree text)
    # 2 is basic output plus basic debug  (adds yellow text)
    # 3 is show everything (adds red text)
    debuglevel=2
     
    #log to file
    log=0
     
    #where the logfile is located
    logfile="/dev/null"
     
    #################################################
    #  functions
    output(){
            if [ $debuglevel -gt "0" ] && [ $1 = "1" ];
            then
                    /bin/echo -e "`/bin/date +"%m-%d-%Y %r"`:\e[00;32m $2\e[00m"
            fi
            if [ $debuglevel -gt "1" ] && [ $1 = "2" ];
            then
                    /bin/echo -e "`/bin/date +"%m-%d-%Y %r"`:\e[01;33m $2\e[00m"
            fi
            if [ $debuglevel -gt "2" ] && [ $1 = "3" ];
            then
                    /bin/echo -e "`/bin/date +"%m-%d-%Y %r"`:\e[00;31m $2\e[00m"
            fi
            if [ $log -eq "1" ];
            then
                    /bin/echo `/bin/date +"%m-%d-%Y %r"` :  $@ >> $logfile
            fi
    }

    #################################################
    # Code
    #
    output "1" "this is normal output"
    output "2" "this is basic debug output"
    output "3" "this is everything output"

Copy this into a basic script and run it, adjust the debuglevel variable at the top to see different output.  I know this will help me in the future and hope it will help others as well.

Quick and easy MSRDP script

I love Linux, but as we all know we have to work on windows computers usually on a day-to-day basis.  We have servers we constantly have to RDP (remote desktop) into to work with active directory, etc.  The Terminal server client in 10.04 was all buggy and when you closed the window it would reconnect in 30 seconds.  This was especially annoying when you left a session open on your locked computer on the other side of the building and needed to hop on real quick to do something.

ubuntu-Terminal-Server-Client

I found out that the problem was that certain libraries had bee updated and caused this bug.   I decided using rdesktop for a while would suffice and created a launcher on my panel to run this command.  It wasn’t pretty for the longest time, a bash terminal asking for usernames, etc.  I really didn’t like it.  I stumbled across zenity a while ago and decided to use that to hide the password and pass that to the rdesktop command.  It is pretty simple actually.
I prefer to hard-code my username and domain in so that I don’t have to constantly fill it out, however more zenity windows could easily be added if more security or flexibility would be needed.  I saved this into a script, made it executable, and then created a launcher to run the script.  I haven’t spent much time on it but I could not just put this code into a launcher and run it (on XFCE, I suppose other frontends like gnome2 or unity might work).

rdesktop -u <Username> -d <domain> -g 1280x1024 $(zenity --entry --text="Hostname/IP please...") -p $(zenity --password --text="Password Please...")

The code above in a launcher provides these dialogue boxes:

Username

password

 

Zenity provides a lot of flexibility in bash scripting so end users don’t have to see a techy/confusing bash window to do something simple.  Zenity options:

OPTIONS
       This program follows the usual  GNU  command  line  syntax,  with  long
       options starting with two dashes (`-').

       Dialog options

       --calendar
              Display calendar dialog

       --entry
              Display text entry dialog

       --error
              Display error dialog

       --file-selection
              Display file selection dialog

       --info Display info dialog

       --list Display list dialog

       --notification
              Display notification

       --progress
              Display progress indication dialog

       --question
              Display question dialog

       --text-info
              Display text information dialog

       --warning
              Display warning dialog

       --scale
              Display scale dialog

       --color-selection
              Display color selection dialog

       --password
              Display password dialog

       --forms
              Display forms dialog

We will definitely be using zenity to make some things more user friendly…