Monday, May 7, 2012

[Bash] Check the existence of a command

#!/bin/bash
# check the existence of a command [ base on the returns of which and alias ]
# usage:
#           chkcmd.sh command-list
# e.g.,
#           chkcmd.sh pdfit
#           http://scriptdemo.blogspot.ca

source ~/.bashrc
if [ $# -eq 0 ]; then
   sed -n '2,6p' `which chkcmd.sh`
   exit
else
   for mycmd in $*
   do
      isInPath=`which ${mycmd} 2< /dev/null`
      if [ ${#isInPath} -eq 0 ]; then
         isAlias=`alias ${mycmd} 2< /dev/null`
         if [ ${#isAlias} -eq 0 ]; then
            echo "${mycmd} is not in the path";
         else
            echo "${mycmd} is an aliased command";
         fi
      else
         echo "${mycmd} is in the path";
      fi
   done
fi

Sunday, March 18, 2012

[Bash] extract pages from a pdf file using gs

#!/bin/bash
# extract pages from a big pdf file using gs
# Usage:
#           pdfselect.sh pdffile startPageNumber endPageNumber
#           http://scriptdemo.blogspot.ca

function getPDFPages()
{
   if [ -e $1 ]; then
      pdfinfo $1 | grep "Pages" | awk '{print $2}'
   else
      echo 0;
   fi
}

if [ $# -lt 2 ]; then
   sed -n '3,4p' `which pdfselect.sh`
   exit
fi

oriPdf=$1
[ ! -e ${oriPdf} ] && echo "${oriPdf} does not exist! " && exit

numPage=`getPDFPages ${oriPdf}`
[ ${numPage} -eq 0 ] && "ZERO page is found in ${oriPdf}" && exit

sPage=$2
[ ${sPage} -gt ${numPage} ] && ${sPage}=${numPage}
if [ $# -eq 2 ]; then
   ePage=${sPage}
else
   ePage=$3;
fi
[ ${ePage} -gt ${numPage} ] && ${ePage}=${numPage}

if [ $# -lt 4 ]; then
   outPdf="p${sPage}_${ePage}_${oriPdf}"
else
   outPdf=$4
fi

eval "gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=${sPage} -dLastPage=${ePage} -sOutputFile=${outPdf} ${oriPdf}"

Saturday, February 25, 2012

[Matlab] create a time series using datenum

function myT=yymmdd2x(inYear,inMonth,inDay)
% create the time axis based on input time [year, mon, date], using datenum
% usage:
%           xTime=yymmdd2x(years, months, days);
% or
%           xTime=yymmdd2x(yyyymmdd);
http://scriptdemo.blogspot.com

if (nargin==0 || nargin>3)
   help yymmdd2x;
   return
end

if nargin==1
   %yyyymmdd case
   yyyymmdd=inYear;
   inYear=floor(yyyymmdd/10000);
   inDay=mod(yyyymmdd,100);
   inMonth=floor(mod(yyyymmdd,10000)/100); clear yyyymmdd
   totalDaysInYear=datenum(inYear+1,1,1)-datenum(inYear,1,1);
   daysInYear=datenum(inYear,inMonth,inDay)-datenum(inYear,1,1)+1;
   myT=daysInYear./totalDaysInYear+inYear;
elseif nargin==2
   % no date, set to 15th, may consider create 12 month for each year if necessary
   if length(inYear)~=length(inMonth) && length(inMonth)<=12
      % repeat inMonth for each year
      inMonth=reshape(inMonth,1,[]);
      mm=repmat(inMonth,1,length(inYear));
      yy=reshape(repmat(reshape(inYear,1,[]),length(inMonth),1),1,[]);
      clear inMonth inYear
      totalDaysInYear=datenum(yy+1,1,1)-datenum(yy,1,1);
      daysInYearA=datenum(yy,mm+1,1)-datenum(yy,1,1)+1;
      daysInYear=datenum(yy,mm,1)-datenum(yy,1,1)+1;
      myT=0.5*(daysInYearA+daysInYear)./totalDaysInYear+yy;

   elseif (length(inYear)==length(inMonth))
      totalDaysInYear=datenum(inYear+1,1,1)-datenum(inYear,1,1);
      daysInYearA=datenum(inYear,inMonth+1,1)-datenum(inYear,1,1)+1;
      daysInYear=datenum(inYear,inMonth,1)-datenum(inYear,1,1)+1;
      myT=0.5*(daysInYearA+daysInYear)./totalDaysInYear+inYear;
   else
       disp('Too difficult for me to guess what your wanna to do...')
       return
   end
else
   totalDaysInYear=datenum(inYear+1,1,1)-datenum(inYear,1,1);
   daysInYear=datenum(inYear,inMonth,inDay)-datenum(inYear,1,1)+1;
   myT=daysInYear./totalDaysInYear+inYear;
end

ShowCalendar