天天看點

我的第一個shell程式

初學SHELL,

==========================main.sh======================================

GSDM=0000               # 市公司代碼

clear

 while true

 do

  echo "請輸入出單機構代碼(格式為:0000**;前6位;如:000003)"

  read MAKECOM

                if [ "$MAKECOM" <> '' ] && [ `expr length $MAKECOM` = "6" ]

                then 

                 if [ `expr substr $MAKECOM 1 4` = $GSDM ]

          then

           break

           else

                     echo "錯誤的出單機構代碼,請重輸!"

                 fi

                else

                        echo "錯誤的出單機構代碼,請重輸!"

                fi

 done

  echo "請輸入業務歸屬機構代碼(格式為:0000**;前6位;如:000003)"

  read COMCODE

                if [ "$COMCODE" <> '' ] && [ `expr length $COMCODE` = "6" ]

                then

                        if [ `expr substr $COMCODE 1 4` = $GSDM ]

                        then

                                break

                        else

                                echo "錯誤的業務歸屬代碼,請重輸!"

                        fi

                        echo "錯誤的業務歸屬代碼,請重輸!"

  echo "請輸入險種代碼(格式為:D/P/A,車險/非車險,A代表全部)"

  read XZDM

  if [ "$XZDM" = "D" ] && [ `expr length $XZDM` = "1" ]

  then   CLASSESCODE=" classescode[1,1] = 'D' " ;

   break

  else

   if [ "$XZDM" = "P" ] && [ `expr length $XZDM` = "1" ]

   then   CLASSESCODE=" classescode[1,1] <> 'D' " ;

    break

   else

          if [ "$XZDM" = "A" ] [ `expr length $XZDM` = "1" ]

          then   CLASSESCODE=" classescode <> '' " ;

                  else

                 echo "錯誤的險種代碼,請重輸!"

   fi

  fi

  echo "請輸入險别代碼(格式為:S/J/A,商業險/交強險,A代表全部)"

  read XBDM

  if [ "$XBDM" = "S" ] && [ `expr length $XBDM` = "1" ]

  then    KINDCODE_R=" ('R21','R30') " ;KINDCODE_P=" ('P10','P30') " ;

   if [ "$XBDM" = "J" ] && [ `expr length $XBDM` = "1" ]

   then   KINDCODE_R=" ('R29','R39') " ;KINDCODE_P=" ('P19','P39') " ;

                           if [ "$XBDM" = "A" ] && [ `expr length $XBDM` = "1" ]

          then   KINDCODE_R=" ('R21','R30','R29','R39') " ;KINDCODE_P=" ('P10','P30','P19','P39') " ;

                           else

                 echo "錯誤的險别代碼,請重輸!"

               fi

  echo "請輸入收付起始日期(格式為:YYYYMMDD;如:20060101)"

  read STARTDATE

  if [ `expr substr $STARTDATE 1 3` = "200" ] && [ `expr length $STARTDATE` = "8" ]

  then    break

          echo "錯誤的日期,請重輸!"

  echo "請輸入收付終止日期(格式為:YYYYMMDD;如:20060101)"

  read ENDDATE

  if [ `expr substr $ENDDATE 1 3` = "200" ] && [ `expr length $ENDDATE` = "8" ]

 echo " "

 echo "出單機構代碼(格式為:0000**;前6位;如:000003): " $MAKECOM

 echo "業務歸屬機構代碼(格式為:0000**;前6位;如:000003): " $COMCODE

 echo "險種代碼(格式為:D/P/A,車險/非車險,A代表全部): " $XZDM

 echo "險别代碼(格式為:S/J/A,商業險/交強險,A代表全部): " $XBDM

 echo "收付起始日期(格式為:YYYYMMDD;如:20060101): " $STARTDATE

 echo "收付終止日期(格式為:YYYYMMDD;如:20060101): " $ENDDATE

 echo "以上輸入是否正确(Y/N)? 若按 Y ,開始提數;否則中斷退出。"

 read YESORNO

 if [ "$YESORNO" = "Y" ] || [ "$YESORNO" = "y" ]

 then

                clear ; echo " " ;

  echo "正在生成資料,請稍候..."

  sh cmd.sh "$MAKECOM" "$COMCODE" "$XZDM" "$XBDM" "$STARTDATE" "$ENDDATE" "$CLASSESCODE" "$KINDCODE_R" "$KINDCODE_P"

 else

  break

 fi

        read KEYPUT

        if [ "$KEYPUT" <> "" ]

        then exit

        else exit

        fi

=============================cmd.sh=====================================

FTPIP=192.168.0.1     # ftp 登入 IP 位址 

FTPUSER=test            # ftp 登入使用者名

FTPPASS=test        # ftp 登入密碼

MAKECOM=$1

COMCODE=$2

XZDM=$3

XBDM=$4

STARTDATE=$5

ENDDATE=$6

CLASSESCODE=$7

KINDCODE_R=$8

KINDCODE_P=$9

echo " "

echo $MAKECOM $COMCODE $XZDM $XBDM $STARTDATE $ENDDATE $SFFDB $GSDM

isql $SFFDB - <<! 0>>sssf.$COMCODE.log 1>>sssf1.$COMCODE.log 2>>sssf2.$COMCODE.log

unload to "sff_r.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl"

select

policyno,classescode,rateamount,kindcode,departcode[1,6],acceptopname,deskdate,documentno,insurant

from $SFFDB:sf02

where ksdm[1,6] = "$MAKECOM"

and departcode[1,6] = "$COMCODE"

and kindcode in $KINDCODE_R

and $CLASSESCODE 

and procflag in ("Y","F")

and deskdate >= "$STARTDATE" and deskdate <= "$ENDDATE"

order by deskdate,policyno,kindcode ;

unload to "sff_p.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl"

policyno,classescode,-rateamount,kindcode,departcode[1,6],acceptopname,deskdate,documentno,insurant

and kindcode in $KINDCODE_P

!

cat head.unl sff_r.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl sff_p.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl > sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl

rm sff_r.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl sff_p.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl

toHtml() { 

awk < sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl '

BEGIN{ 

FS="|" 

title=sprintf("%s","'$title'") 

printf("<HTML>\n") 

printf("<HEAD>\n") 

print "<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">" 

printf("<TITLE> %s (%s %s年%s月)</TITLE>\n",title,"'$gsname'","'$yyyy'","'$mm'") 

print "<style>" 

print "<!--" 

print "th {font-size:16px}" 

print "td {font-size:14px}" 

print "-->" 

print "</style>" 

printf("</HEAD>\n") 

printf("<BODY><TABLE border=\"1\" align=\"center\" bordercolorlight=\"#FC9C0C\">\n")

}

{ for (m=1;m<=NF-1;m++) { 

if(m==1) { 

printf(" <TR align=\"right\" valign=\"middle\">\n") 

printf(" <TD align=\"left\" nowrap>%s&nbsp</TD>\n",$m) 

else 

if($m==0) printf(" <TD>0</TD>\n") 

else

if(m==3) printf(" <TD align=\"right\" nowrap>%s</TD>\n",$m) 

if(m==NF-1) printf(" </TR>\n\n") } 

END { 

print "</TABLE>" 

print "</BODY>" 

print "</HTML>" 

toHtml sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl > sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.xls

rm sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.unl

echo "資料生成完畢!"

    echo "正在向市公司内部網傳送資料,請稍候..."

    ftp -n $FTPIP <<!

        user $FTPUSER $FTPPASS

        cd $COMCODE/資訊技術部

        bin

        put sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.xls 實收實付.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.xls 

        pwd

        dir 實收實付.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.xls

    by

rm  sff.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.xls

    echo " "

    echo "資料提取結束,并已傳至市公司内部網;"

    echo "您此次提取資料的檔案名為:實收實付.$COMCODE.$XZDM.$XBDM.$STARTDATE-$ENDDATE.xls"

==========================head.unl==================================

字段名1|字段名2|字段名3|......|

   本文轉自ipist 51CTO部落格,原文連結:http://blog.51cto.com/ipist/11119,如需轉載請自行聯系原作者