• About Binary Impulse
  • BashBytes
Binary Impulse

Tag Archives: Scripting

Sample debug output code for BASH/SHELL

July 5, 2013 10:41 AM / Leave a Comment / Dan

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.

Posted in: How-To's, Other / Tagged: bash, debug, echo, script, scripting, shell

Pages

  • About Binary Impulse
  • BashBytes

Recent Posts

  • My UBook X Linux Tablet Adventure and how I learned more than I ever thought I would need to know about accelerometer drivers
  • Migrating Proxmox Hypervisor’s Boot Volume ZFS Mirror To New (Smaller) Disks
  • OPNsense Performance Tuning for Multi-Gigabit Internet
  • Reddit Deplatforms Popular Microsoft Software Swap Subreddit
  • Comcast Upgrades Gigabit Pro from 3Gbps to 6Gbps!
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Apr    

Recent Comments

  • jasonistre on OPNsense Performance Tuning for Multi-Gigabit Internet
  • Nick on OPNsense Performance Tuning for Multi-Gigabit Internet
  • Kirk Schnable on OPNsense Performance Tuning for Multi-Gigabit Internet
  • nick on OPNsense Performance Tuning for Multi-Gigabit Internet
  • Eric on My UBook X Linux Tablet Adventure and how I learned more than I ever thought I would need to know about accelerometer drivers

Categories

  • How-To's
  • Humorous
  • Musings
  • Newsworthy
  • Other

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© Copyright 2025 - Binary Impulse
Infinity Theme by DesignCoral / WordPress