免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: Pacific Yan
打印 上一主题 下一主题

help! please simplify this shell program! [复制链接]

论坛徽章:
0
41 [报告]
发表于 2003-07-12 03:43 |只看该作者

help! please simplify this shell program!

Now my program will grow bigger and bigger, but I still missing how to take 2 arguments,


#!/usr/bin/ksh
     
fflag=
sflag=
while getopts hf:s: name
do
            case $name in
            f)  fflag=1
                fval="$OPTARG";;
            s)  sflag=1
                sval="$OPTARG";;
            h)  printf "Usage: p2\n p2 year\n p2 -f firstyear -s second year\n"
                exit 2;;        
            ?)  printf "Usage: %s: [-f] [-s value] args\n" $0
                exit 2;;
            esac
done


# if sflag is missing, take 1st year and proceed to main computation block
if [   ! -z "$fflag"  -a  -z "$sflag"    ]; then
            year=$fval
           if ((year<1)) || ((year>9999))
           then
             echo "year must be 1 to 9999"
             exit 1
           fi            

else
{
da=0
for st in Sunday Monday Tuesday Wednesday Thursday Friday Saturday;do
        eval str_$da=$st
        da=$(($da + 1))
done

mon=1
while [ $mon -le 12 ];do
        da=`cal $mon $1 |awk 'NR==3{loc=index($0,"1";print (loc-2)/3;exit}'`
        eval "count_$da=\$((\$count_$da+1))"
        mon=$(($mon + 1 ))
done

da=0
while [ $da -le 6 ];do
        eval dcn=\$count_$da
        if [ $dcn -eq 1 ];then
                eval "print \"\$str_$da \""|awk 'BEGIN {OFS=" "; ORS=" "} {print $1}
'
fi
        da=$(($da + 1))
done
echo
}
fi


# if fflag is missing,
if [    -z "$fflag"  -a  ! -z "$sflag"    ]; then
           PS3="choose:
           1 quit
           2 change the beginning of the range
           3 change end of the range
           :"
           select indx in "quit" "change beginning" "change end"
             do
               case $indx in
                 "quit" break;;
                 "change beginning"
                                   print -n "Enter first year "
                                   read firstyear
                                   year1=$firstyear
                                   print -n "Enter second year "
                                   read secondyear
                                   year2=$secondyear                                 
                                   break
                                   ;;     
                 "change end"  
                                   print -n "Enter first year "
                                   read firstyear
                                   year1=$firstyear
                                   print -n "Enter second year "
                                   read secondyear
                                   year2=$secondyear                                 
                                   break
                                   ;;                                
                 *) echo  default;;
                esac               
            done
fi

论坛徽章:
0
42 [报告]
发表于 2003-07-12 13:14 |只看该作者

help! please simplify this shell program!

不用那么复杂的.写脚本我觉得实用为主,写的速度越快越好.加点注释就行了,简单功能不用考虑什么usage(),下面是另一个script [ hh.sh ],它会调用上次的test.sh。另外写只是为了结构更清楚。你可以自己试着把两个脚本合在一起成为一个.good luck!


  1. #!/bin/sh
  2. #shell script [ hh.sh ]
  3. if [ $# -ne 2 ];then if [ $# -eq 1 ];then ./test.sh $1;exit 0;else exit 1;fi;fi
  4. if [ $1 -ge 1 ] && [ $1 -le 9999 ] && \
  5.         [ $2 -ge 1 ] && [ $2 -le 9999 ] && [ $1 -ne $2 ];then

  6.         if [ $1 -gt $2 ];then from=$2;to=$1;
  7.         else from=$1;to=$2;fi

  8.         while [ $from -le $to ];do
  9.                 echo -n "$from " #这条指令你机器上不行,你自己改.
  10.                 ./test.sh $from    #在这儿调用上次的test.sh
  11.                 from=$(($from + 1))
  12.         done
  13. fi
复制代码


测试:

  1. $ ./hh.sh 2002
  2. Wednesday Thursday Saturday
  3. $ ./hh.sh 1999 2003
  4. 1999 Sunday Tuesday Saturday
  5. 2000 Sunday Monday Thursday
  6. 2001 Tuesday Wednesday Friday
  7. 2002 Wednesday Thursday Saturday
  8. 2003 Sunday Thursday Friday
  9. $ ./hh.sh 2003 1999
  10. 1999 Sunday Tuesday Saturday
  11. 2000 Sunday Monday Thursday
  12. 2001 Tuesday Wednesday Friday
  13. 2002 Wednesday Thursday Saturday
  14. 2003 Sunday Thursday Friday
  15. $
复制代码

论坛徽章:
0
43 [报告]
发表于 2003-07-12 21:43 |只看该作者

help! please simplify this shell program!

I know C is a really powerful and faster program.  But my major is biology and even the shell and PERL is difficult for me to learn.  

Your program is exercent, but the output format isn't what I want.  I want it will give me the same format as you input one argument.

That mean:

$test.sh 2003
Sunday Thursday Friday

$test.sh 2003 2009  or $test.sh 2009 2003
Wednesday Thursday Saturday   (I guess it)

论坛徽章:
0
44 [报告]
发表于 2003-07-13 18:27 |只看该作者

help! please simplify this shell program!

只要删除上面[ hh.sh ]的下面这一行就行了.
"echo -n "$from " #这条指令你机器上不行,你自己改. "

论坛徽章:
0
45 [报告]
发表于 2003-07-13 19:10 |只看该作者

help! please simplify this shell program!

That only solve me one problem, but how can I add all the years together like from 2003 to 2009, then give just one line output, I don't want it list all the years.  For example,

I should have
$ hh.sh 2003 2009
$ Wednesday Thursday Saturday (one line output, I guess it)

rather than
$ hh.sh 2003 1999
Sunday Tuesday Saturday
Sunday Monday Thursday
Tuesday Wednesday Friday
Wednesday Thursday Saturday
Sunday Thursday Friday

论坛徽章:
0
46 [报告]
发表于 2003-07-13 20:24 |只看该作者

help! please simplify this shell program!

Sunday Tuesday Saturday
Sunday Monday Thursday
Tuesday Wednesday Friday
Wednesday Thursday Saturday
Sunday Thursday Friday
我不太明白你的意思,从这些行里输出哪一行的依据是什么?

论坛徽章:
0
47 [报告]
发表于 2003-07-13 20:33 |只看该作者

help! please simplify this shell program!

for example ( all the number is my guess):

2003 (Sunday 2 Friday 2 Thursday 1 Saturday 3 Tuesday 2  Monday 1 Wednesday 1)   

2004  (Sunday 1 Friday 1 Thursday 2 Saturday 1 Tuesday 2 Monday 3 Wednesday 2)

2005 (Sunday 1 Friday 2 Thursday 1 Saturday 3 Tuesday 2  Monday 1 Wednesday 2)

add all sunday, monday, tuesday, ..... together, after you get the numbers, then you compare to get the output.


$hh.sh 2003 2005
Sunday Thursday

Note: Sunday and Thursday only appears 4 times, then both of them is the lowest.

论坛徽章:
0
48 [报告]
发表于 2003-07-13 21:04 |只看该作者

help! please simplify this shell program!

ftp://bu.dns0755.net/newtest.sh


  1. #!/bin/sh
  2. # shell script [ newtest.sh ]

  3. if [ $# -ne 2 ];then exit 1;fi
  4. if [ $1 -ge 1 ] && [ $1 -le 9999 ] && \
  5.         [ $2 -ge 1 ] && [ $2 -le 9999 ];then

  6. if [ $1 -gt $2 ];then from=$2;to=$1;else from=$1;to=$2;fi

  7. da=0
  8. for st in Sunday Monday Tuesday Wednesday Thursday Friday Saturday;do
  9.         eval str_$da=$st
  10.         da=$(($da + 1))
  11. done

  12. while [ $from -le $to ];do
  13.         mon=1
  14.         while [ $mon -le 12 ];do
  15.                 da=`cal $mon $from |awk 'NR==3{loc=index($0,"1");print (loc-2)/3;exit}'`
  16.                 eval "count_$da=\$((\$count_$da+1))"
  17.                 mon=$(($mon + 1 ))
  18.         done
  19.         from=$(($from + 1))
  20. done


  21. min=$count_0

  22. da=1
  23. while [ $da -le 6 ];do
  24.         eval dcn=\$count_$da
  25.         if [ $dcn -lt $min ];then min=$dcn;fi
  26.         da=$(($da + 1))
  27. done

  28. da=0
  29. while [ $da -le 6 ];do
  30.         eval dcn=\$count_$da
  31.         if [ $dcn -eq $min ];then
  32.                 eval "echo -n \"\$str_$da \""
  33.         fi
  34.         da=$(($da + 1))
  35. done
  36. echo
  37. fi
复制代码

论坛徽章:
0
49 [报告]
发表于 2003-07-13 21:09 |只看该作者

help! please simplify this shell program!

确认一下你是不是想要累加和最小的     
你仍然要把echo好儿改成适合你系统的。

论坛徽章:
0
50 [报告]
发表于 2003-07-13 21:17 |只看该作者

help! please simplify this shell program!

Thanks, 夜未眠

I will test it after I do my shopping.  It is almost 10 clock, wish you have a good sleep.  If you need any any softwares, books, please let me know.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP