Register
It is currently Thu Oct 23, 2014 11:10 am

exit status of every cmd in script and return OK or FAILED


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Mar 03, 2014 11:05 am   

Joined: Mon Mar 03, 2014 10:50 am
Posts: 2
Hello,

I am currently writing a bash script that will install software. For each software installation, I created a function. For example:

********************
Code:
#!/bin/bash

install_software1 {
     cmd1 #Downloading setup
     cmd2 #Extracting Setup
     cmd3 #Downloading response file
     cmd4 #Filing values in response file
     cmd5 #Run setup
     cmd6 #Cleanup temp directory
}

install_software1
...
install_softwareX

************************
I would like to be able to output the progress of all the commands to the screen as the installation goes along with an [ OK ] or [ FAILED ] just like the Linux bootup style.

i.e.
DESCRIPTION STATUS
Installing software1 ********
Downloading software1 setup... [ OK ]
Extracting setup... [ OK ]
...
Software1 successfully installed ********

The issue I am having right now is to check the exit status of all commands in the function without having to type the function that check the exit status after each command. Is there a cleaner way to do that?

Should I create a trap for all ERR, and use it to output [ FAILED ]? How will I output all the [ OK ]?

Please help me figure this out :)

Thanks,
Joey


Top
 Profile  
 PostPosted: Mon Mar 03, 2014 11:52 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
After each command the exit status is available in the variable $?, however this information is, of course, lost when another command is executed so I think the checks will need to be built into the install function. $? > 0 is a failure of some sort.

There also the option of looking to see what the linux startup scripts use to print the OK/FAIL, it'll likely be better coded :)


Top
 Profile  
 PostPosted: Fri Mar 07, 2014 2:54 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
I have in my .functions:
Code:
function exitstatus {
    EXITSTATUS="$?"
if [ "${EXITSTATUS}" -eq 0 ]
   then
      printf "\e[1;32m%$(($COLUMNS))s\e[m" "[  OK  ] "
   else
      printf "\e[1;31m%$(($COLUMNS))s\e[m" "[ ERROR ] "
fi
}

and in my .bashrc:
Quote:
PROMPT_COMMAND=exitstatus


Hope that yields some help for you.


Top
 Profile WWW  
 PostPosted: Tue Mar 11, 2014 11:24 am   

Joined: Mon Mar 03, 2014 10:50 am
Posts: 2
I was able to accomplish what I needed by modifying some of the Linux built-in functions:

Code:
## Screen Dimensions
# Find current screen size
if [ -z "${COLUMNS}" ]; then
   COLUMNS=$(stty size)
   COLUMNS=${COLUMNS##* }
fi

# When using remote connections, such as a serial port, stty size returns 0
if [ "${COLUMNS}" = "0" ]; then
   COLUMNS=80
fi

## Measurements for positioning result messages
COL=$((${COLUMNS} - 8))
WCOL=$((${COL} - 2))

## Set Cursor Position Commands, used via echo
SET_COL="\\033[${COL}G"      # at the $COL char
SET_WCOL="\\033[${WCOL}G"    # at the $WCOL char
CURS_UP="\\033[1A\\033[0G"   # Up one line, at the 0'th char

## Set output colors
NORMAL="\\033[0;39m"         # Standard console grey
SUCCESS="\\033[1;32m"        # Success is green
FAILURE="\\033[1;31m"        # Failures are red
BRACKET="\\033[1;34m"        # Brackets are blue

timespec()
{
   STAMP="$(echo `date +"%b %d %T %:z"` `hostname`) $LINENO "
   return 0
}

log_success_msg()
{
    echo -n -e "${@}"
    echo -e "${SET_COL}${BRACKET}[${SUCCESS}  OK  ${BRACKET}]${NORMAL}"

    timespec
    echo -e "${STAMP} ${@} OK" >> ${LOG_FILE}
    return 0
}

log_failure_msg()
{
    echo -n -e "${@}"
    echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"

    timespec
    echo -e "${STAMP} ${@} FAIL" >> ${LOG_FILE}
    echo "Installation failed. Log can be found here: " ${LOG_FILE}
    exit 1
}


Then you can create a function such as:

Code:
function make_dir()
{
   echo -n "   Creating directory: "${1}""
   
   if [ -d "${1}" ]; then
        #Directory exists - no need to create
      log_success_msg
   else
      #Directory doesn't exist - create directory
      mkdir -p "${1}" >> ${LOG_FILE} 2>&1
      if [ "$?" = "0" ]; then
         #Directory creation succeeded
           log_success_msg
       else
          #Directory creation failed
           log_failure_msg
           exit 1
       fi
   fi
return 0
}


That would be called using:

Code:
make_dir "/web/logs"


If the directory creation is successful, the screen output looks like:
Code:
Creating directory: /web/logs                                                                                                  [  OK  ]


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP