Sunday, June 15, 2014

Unix Shell Example: Software Builds Automation(2)

 #! /bin/bash  
   
 #usage function, use EOF statement to output the basic usage format  
 #of this script  
 usage()  
 {  
   cat <<EOF  
 Usage:  
   $script [ --? ]   
           [ --all ... ]  
           [ --cd ... ]  
           [ --check ... ]  
           [ --configure ... ]  
           [ --environment ... ]  
           [ --help ]  
           [ --logdirectory dir ]  
           [ --on "[user@]host[:dir][,envfile] ..." ]  
           [ --source "dir..." ]  
           [ --userhosts "files" ]  
           [ --version ]  
           package(s)  
 EOF  
 }  
   
 #Given the exit code($1), call the usage function then exit  
 #the script with given exit code  
 usage_and_exit()  
 {  
   usage  
   exit $1  
 }  
   
 version()  
 {  
   echo "$PROGRAM version $VERSION"  
 }  
   
 #Output the given error message to standard error stream  
 #then call "usage_and_exit" to output usage format and exit  
 #current script  
 error()  
 {  
   echo "$@" 1>&2  
   usage_and_exit 1  
 }  
   
 #Given the warning message, write to standard output  
 #And increase the EXITCODE value  
 warning()  
 {  
   echo "$@" 1>&2  
   EXITCODE=`expr $EXITCODE + 1`  
 }  
   
 find_package()  
 {  
   #Firstly we get the "base" part of the package, by removing  
   #everything starting from "-" or "_"  
   base=`echo "$1" | sed -e 's/[-_][.]*[0-9].*$//'`  
   PAR=  
   PARFILE=  
   
   #Secondly Try to locate the file we are going to build  
   #We search files based on $SRCDIRS and some common format of   
   #build package  
   for srcdir in $SRCDIRS  
   do  
     test "$srcdir" = "." && srcdir="`pwd`"  
     for subdir in "$base" ""  
     do  
       find_file $srcdir/$subdir/$1.tar.gz "tar xfz" && return  
       find_file $srcdir/$subdir/$1.tar.Z "tar xfz" && return  
       find_file $srcdir/$subdir/$1.tar "tar xf" && return  
       find_file $srcdir/$subdir/$1.tar.bz2 "tar xfj" && return  
       find_file $srcdir/$subdir/$1.tar.tgz "tar xfz" && return  
       find_file $srcdir/$subdir/$1.zip "unzip -q" && return  
       find_file $srcdir/$subdir/$1.jar "jar xf" && return  
     done  
   done  
 }  
   
 #Just test the readability and existance of giving file  
 #then populate on global variable $PAR $PARFILE if necessary  
 find_file()  
 {  
   if test -r "$1"  
   then  
     PAR="$2"  
     PARFILE="$1"  
     return 0  
   else  
     return 1  
   fi    
 }  
   
 #Given the userhosts list, we add them to the $ALTUSERHOSTS  
 set_userhosts()  
 {  
   for u in "$@"  
   do  
     if test -r "$u"  
     then  
       ALTUSERHOSTS="$ALTUSERHOSTS $u"  
     elif test -r "$BUILDHOME/$u"  
     then  
       ALTUSERHOSTS="$ALTUSERHOSTS $BUILDHOME/$u"  
     else  
       error "File not found: $u"  
     fi  
   done  
 }  

No comments:

Post a Comment