免费注册 查看新帖 |

Chinaunix

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

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

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

help! please simplify this shell program!

都用ksh还not a good programmer?

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

help! please simplify this shell program!

well, I am just a begineer to shell programming.  I am not a modest guy.  Computer is really too much for me, a biologist to learn!

论坛徽章:
0
13 [报告]
发表于 2003-07-11 00:28 |只看该作者

help! please simplify this shell program!

等 一下,我帮你写一个得了,

论坛徽章:
0
14 [报告]
发表于 2003-07-11 00:36 |只看该作者

help! please simplify this shell program!

文件1

  1. #awk script [ cutweek.awk ]
  2. NR==2{
  3.         FMT=$0
  4. }
  5. NR==3{
  6.         loc=index($0,"1")
  7. }
  8. END{
  9.         print substr(FMT,loc-1,2)
  10. }
复制代码


文件2

  1. #!/bin/sh
  2. # shell script [ test.sh ]
  3. if [ $# -ne 1 ];then exit 1;fi
  4. if [ $1 -lt 1 ] || [ $1 -gt 9999 ];then exit 1;fi

  5. mon=1
  6. while [ $mon -le 12 ];do
  7.         wd=`cal $mon $1 |awk -f cutweek.awk`
  8.         echo "$1-$mon-1: $wd"
  9.         mon=$(($mon + 1 ))
  10. done
复制代码


测试:

  1. $ ./test.sh 2001
  2. 2001-1-1: Mo
  3. 2001-2-1: Th
  4. 2001-3-1: Th
  5. 2001-4-1: Su
  6. 2001-5-1: Tu
  7. 2001-6-1: Fr
  8. 2001-7-1: Su
  9. 2001-8-1: We
  10. 2001-9-1: Sa
  11. 2001-10-1: Mo
  12. 2001-11-1: Th
  13. 2001-12-1: Sa
  14. $
复制代码

论坛徽章:
0
15 [报告]
发表于 2003-07-11 00:42 |只看该作者

help! please simplify this shell program!

You are great!!!  There could be several days have the same low frequency, like 2003:
Friday           1
Sunday         1
Thursday      1
Monday        2
Tuesday       2
Wednesday  2
Saturday      3

then how can I report all of them (Friday, Sunday, Thursday) in one line.   


By the way, if you need something, please let me know.  

Thanks a lot

论坛徽章:
0
16 [报告]
发表于 2003-07-11 00:49 |只看该作者

help! please simplify this shell program!

then how can I report all of them (Friday, Sunday, Thursday) in one line.
请举个输出的例子      
是不是这样

awk脚本不变


  1. #!/bin/sh
  2. # shell script [ test.sh ]
  3. if [ $# -ne 1 ];then exit 1;fi
  4. if [ $1 -lt 1 ] || [ $1 -gt 9999 ];then exit 1;fi

  5. mon=1
  6. while [ $mon -le 12 ];do
  7.         da="$1-$mon-1"
  8.         case `cal $mon $1 |awk -f cutweek.awk` in
  9.         Mo) w1="${w1:="Mo "}$da ";;
  10.         Tu) w2="${w2:="Tu "}$da ";;
  11.         We) w3="${w3:="We "}$da ";;
  12.         Th) w4="${w4:="Th "}$da ";;
  13.         Fr) w5="${w5:="Fr "}$da ";;
  14.         Sa) w6="${w6:="Sa "}$da ";;
  15.         *) w7="${w7:="Su "}$da ";;
  16.         esac
  17.         mon=$(($mon + 1 ))
  18. done

  19. for stat in "$w1" "$w2" "$w3" "$w4" "$w5" "$w6" "$w7";do
  20.         echo $stat
  21. done
复制代码


测试

  1. $ ./test.sh 1981
  2. Mo 1981-6-1
  3. Tu 1981-9-1 1981-12-1
  4. We 1981-4-1 1981-7-1
  5. Th 1981-1-1 1981-10-1
  6. Fr 1981-5-1
  7. Sa 1981-8-1
  8. Su 1981-2-1 1981-3-1 1981-11-1
  9. $
复制代码

论坛徽章:
0
17 [报告]
发表于 2003-07-11 01:34 |只看该作者

help! please simplify this shell program!

[quote]原帖由 "acific Yan"]yes, you are really good! By the way, I am a bioinformatics and a biologist, and I am not a good programmer.  So, I am trying to learn something.  Also, do your guys have any any idea want to do somet..........[/quote 发表:

Hehe, I am also a newbie to shell-program, 夜未眠 is really a __GURU__.......

论坛徽章:
0
18 [报告]
发表于 2003-07-11 02:06 |只看该作者

help! please simplify this shell program!

[quote]原帖由 "夜未眠"][/quote 发表:
   

No, should like this:

$ test.sh 2003
$ Sunday Thursday Friday


Since in 2003,  Sunday Thursday Friday only appear once, then the OUTPUT should be separated by a single space and sorted in ascending order from Sunday to Saturday, nothing else will be exported except Sunday Thursday Friday

Thanks

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

help! please simplify this shell program!

脚本

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

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

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

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

  16. da=0
  17. while [ $da -le 6 ];do
  18.         eval dcn=\$count_$da
  19.         if [ $dcn -eq 1 ];then
  20.                 eval "echo -n \"\$str_$da \""
  21.         fi
  22.         da=$(($da + 1))
  23. done
  24. echo
  25. fi
复制代码


测试

  1. $ ./test.sh 2003
  2. Sunday Thursday Friday
  3. $ ./test.sh 1981
  4. Monday Friday Saturday
  5. $
复制代码


什么时候请吃钣啊?

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

help! please simplify this shell program!

That is no problem if you can give me your telephone number.

However, there is something wrong when I test it on my computer, here is the output:

$ test.sh 2003
final_test[14]: count_2.66667=1:  not found
final_test[14]: count_5.33333=1:  not found
final_test[14]: count_2.66667=1:  not found
final_test[14]: count_6.66667=1:  not found
final_test[14]: count_1.33333=1:  not found
final_test[14]: count_1.33333=1:  not found
-n Sunday
final_test[21]: test: argument expected
final_test[21]: test: argument expected
final_test[21]: test: argument expected
final_test[21]: test: argument expected
final_test[21]: test: argument expected

$
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP