+ Reply to Thread
Results 1 to 3 of 3

Thread: Unix Shell Script

  1. #1
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Unix Shell Script

    i'm writing a shell script to make echoing in color easier. but i keep getting an error. there is a help message for explaining how the command works; and no matter what i do, the help keeps showing up instead of the rest of the script running.

    please help.

    the script is:
    Code:
    #!/bin/bash
    #
    # cecho
    # By: James Brumond
    # Created: 19 January 2009
    #
    
    helpmsg () {
            echo
            echo "USE: cecho [-h | -f:COLOR | -b:COLOR] 'TEXT'"
            echo
            echo "OPTIONS:"
            echo " -f COLOR         Sets the foreground color."
            echo " -b COLOR         Sets the background color."
            echo " -h                       Display this message."
            echo
            echo "FOREGROUND COLORS:"
            echo -e '\E[;mdefault            \E[;1mldefault          \E[;30mblack    \E[;1;30mdgrey          \E[;31mred          \E[1;31mlred'
            echo -e '\E[;32mgreen            \E[1;32mlgreen          \E[;33myellow           \E[;1;33mlyellow        \E[;34mblue \E[1;34mlblue'
            echo -e '\E[;35mmagenta          \E[1;35mlmagenta        \E[;36mcyan             \E[;1;36mlcyan          \E[;37mwhite \E[1;37mlwhite'
            echo
            echo "BACKGROUND COLORS:"
            echo -en '\E[;1mdefault\E[;m    '
            echo -en '\E[40;1mblack\E[;m    '
            echo -en '\E[41;1mred\E[;m      '
            echo -e '\E[42;1mgreen\E[;m     '
            echo -en '\E[43;1myellow\E[;m   '
            echo -en '\E[44;1mblue\E[;m     '
            echo -en '\E[45;1mmagenta\E[;m  '
            echo -e '\E[46;1mcyan\E[;m      '
            echo -e '\E[47;1mwhite\E[;m     '
            echo
            tput sgr0
            }
    
    setup () {
            # foreground codes
            foregroundCode[0]=m
            foregroundCode[1]=1m
            foregroundCode[2]=30m
            foregroundCode[3]="1;30m"
            foregroundCode[4]=31m
            foregroundCode[5]="1;31m"
            foregroundCode[6]=32m
            foregroundCode[7]="1;32m"
            foregroundCode[8]=33m
            foregroundCode[9]="1;33m"
            foregroundCode[10]=34m
            foregroundCode[11]="1;34m"
            foregroundCode[12]=35m
            foregroundCode[13]="1;35m"
            foregroundCode[14]=36m
            foregroundCode[15]="1;36m"
            foregroundCode[16]=37m
            foregroundCode[17]="1;37m"
    
            # foreground text
            foregroundText[0]=default
            foregroundText[1]=ldefault
            foregroundText[2]=black
            foregroundText[3]=dgrey
            foregroundText[4]=red
            foregroundText[5]=lred
            foregroundText[6]=green
            foregroundText[7]=lgreen
            foregroundText[8]=yellow
            foregroundText[9]=lyellow
            foregroundText[10]=blue
            foregroundText[11]=lblue
            foregroundText[12]=magenta
            foregroundText[13]=lmagenta
            foregroundText[14]=cyan
            foregroundText[15]=lcyan
            foregroundText[16]=white
            foregroundText[17]=lwhite
    
            # background codes
            backgroundCode[0]=
            backgroundCode[1]=40
            backgroundCode[2]=41
            backgroundCode[3]=42
            backgroundCode[4]=43
            backgroundCode[5]=44
            backgroundCode[6]=45
            backgroundCode[7]=46
            backgroundCode[8]=47
    
            # background text
            backgroundText[0]=default
            backgroundText[1]=black
            backgroundText[2]=red
            backgroundText[3]=green
            backgroundText[4]=yellow
            backgroundText[5]=blue
            backgroundText[6]=magenta
            backgroundText[7]=cyan
            backgroundText[8]=white
            }
    
    startup () {
            setup
            if [ $# -eq 0 ]
            then
                    helpmsg
                    exit 0
            elif [ $# -eq 1 ]
            then
                    if [ $1 = "-h" ]
                    then
                            helpmsg
                            exit 0
                    elif [ $1 = "-f:*" ]
                    then
                            echo
                            exit 0
                    elif [ $1 = "-b:*" ]
                    then
                            echo
                            exit 0
                    else
                            echo "$1"
                    fi
            elif [ $# -eq 2 ]
            then
                    echo 2
            elif [ $# -eq 3 ]
            then
                    echo 3
            else
                    echo "Improper Use!"
                    helpmsg
                    exit 1
            fi
            }
    
    startup

  2. #2
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: Unix Shell Script

    You're going to kick yourself when I tell you. You forgot to pass any arguments to 'startup'. The last line should be:
    Code:
    startup "$@"
    Here's a few other tips. Feel free to ignore or use them.
    • Canonical name for 'helpmsg' function would be 'usage'.
    • A personal preference: I find a single 'printf' or 'echo' with a multiline string more readable and more editable than multiple echo statements:
      Code:
      progname=`echo $0 | sed -e 's/.*\///'`
      
      usage() {
          # printf "%s" "..." would be safer, but it doesn't seem to interpret '\E' as escape char
          printf "USAGE: $progname  [-h | -f:COLOR | -b:COLOR] 'TEXT'
      OPTIONS:
      ...
      FOREGROUND COLORS:
      \E[;mdefault            \E[;1mldefault          \E[;30mblack    \E[;1;30mdgrey          \E[;31mred          \E[1;31mlred
      ...
      "
          if [$# -gt 0] ; then
              exit $1
          exit 0
      }
    • Since every call to usage is followed by an exit, put exit in usage
    • Use case rather than a sequence of ifs. It might look something like:
      Code:
      case $# in
          0) usage ;;
          1) case $1 in
                  -f:*) ... ;;
                  ...
              esac;;
          ...
          *) echo "Improper Use!"
              helpmsg 1;;
      esac
    • Rather than the case statement on $# handling the arguments, parse any arguments in a loop:
      Code:
      if  [ $# -eq 0 ] ; then
          usage
      else
          fg=1
          while [ $# -gt 0 ] ; do
              case $1 in
                  -h) usage ;;
                  -f:*) fg=`echo $1 | sed s/^-f://` ;;
                  -b:*) bg=... ;;
                  -*) echo "Unknown option: $1";
                      usage 1;;
                  *) msg="$msg $1" ;; #TODO: fix this so that an extra ' ' isn't prepended to msg
              esac
              shift
          done
          echo '\E[${bg};${fg}m${msg}\E[;m'
      fi

    I hope some of that is useful.

  3. #3
    kbjradmin's Avatar
    kbjradmin is offline x10 Elder kbjradmin is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    Washington State, USA
    Posts
    512

    Re: Unix Shell Script

    oh... duh.
    ok, i can't really believe i missed that.

    anyway, thank you for the help and the suggestions.

+ Reply to Thread

Similar Threads

  1. Unix Shell Password Check
    By kbjradmin in forum Programming Help
    Replies: 9
    Last Post: 01-24-2009, 12:11 PM
  2. Unix Shell Programming
    By kbjradmin in forum Programming Help
    Replies: 1
    Last Post: 01-13-2009, 01:45 PM
  3. Access to 300+ PHP scripts (2500 credits)
    By jonathanyaniv in forum The Marketplace
    Replies: 11
    Last Post: 06-03-2008, 11:58 PM
  4. mass video conversion shell script
    By hopper in forum Computers & Technology
    Replies: 0
    Last Post: 04-28-2008, 06:33 PM
  5. Replies: 8
    Last Post: 12-03-2007, 05:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers