免费注册 查看新帖 |

Chinaunix

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

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

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

help! please simplify this shell program!

免费吗?免费就要啦.

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

help! please simplify this shell program!

of course, it is free.  I have a lot of ebooks about oracle, linux shell and also PERL, but for the hard copies, I guess the shipping fee is too much.

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

help! please simplify this shell program!

Here is my testing, it looks like for 2 arguments are OK, but for one argument, I got no output, and also one small output error I guess I need to figure it out.  All the output errors are marked as below:

$ newtest.sh 2003 2009
newtest.sh[5]:  :  not found   <<<<<<<<<<< This isn't what I want
Friday

xyan<176>newtest.sh 2003    <<<<<<<<<<<< no output


xyan<178>newtest.sh 2001 2003
newtest.sh[5]:  :  not found        <<<<<<<<the same errors
Wednesday

xyan<182>newtest.sh 2003 2005
newtest.sh[5]:  :  not found       <<<<<<<<the same errors
Sunday Friday

The results are all correct.

Thanks, 夜未眠, this project cost me a lot of time.  I really appreciate your efforts and kindness.

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

help! please simplify this shell program!

如果要计算某一年.如果你不怕麻烦的话给newtest.sh传递两个相同的参数就可以了. 也不知道你给这个sh传一个参数时是想得到当年统计次数最少的还是全部都显示(象前面的test.sh一样)

比如要得到2003年的,这样计算出来的是2003年出现次数最少的

  1. $ ./newtest.sh 2003 2003
  2. Sunday Thursday Friday
  3. $
复制代码


如果非要用一个参数的话那么把程序开头的那段if语句改一下.

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

  4. if [ $1 -gt $2 ];then from=$2;to=$1;else from=$1;to=$2;fi
复制代码


改为

  1. if [ $# -gt 2 ];then exit 1;fi
  2. if [ $# -eq 1 ];then v1=$1;v2=$1;else v1=$1;v2=$2;fi
  3. if [ $v1 -ge 1 ] && [ $v1 -le 9999 ] && \
  4.         [ $v2 -ge 1 ] && [ $v2 -le 9999 ];then
  5. if [ $v1 -gt $v2 ];then from=$v2;to=$v1;else from=$v1;to=$v2;fi
复制代码


实际上这种方法很笨的。 就是把一个的参数人为的设为两个相同的参数,如果要更高的效率的话应该改程式结构。而不应该象这样改。还有就是参数检查,如果只是自己用的话没必要加那么大一段的参数检查。如果果我自己用的话我就会删除这些检查输入参数在1-9999的语句.至于那些not found错误我就不清楚了。在freebsd下正常,不了解你的系统  

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

help! please simplify this shell program!

Thanks very much! It works.  

related to some useless code, it is my stupid assignment's requirement.

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

help! please simplify this shell program!

Hi, can you send the file and your phone number to xyan0@yahoo.com again? my company block your email because it is a shell script.  Thanks

论坛徽章:
0
57 [报告]
发表于 2007-06-22 18:42 |只看该作者

what's the problem?!!!

to LZ
you can read Chinese , but why can't you type it?!
you can do things on the cyber,why can't you download a software to input Chinese?!!

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
58 [报告]
发表于 2007-06-22 18:48 |只看该作者
原帖由 ewancarrey 于 2007-6-22 18:42 发表
to LZ
you can read Chinese , but why can't you type it?!
you can do things on the cyber,why can't you download a software to input Chinese?!!

刨坟啊?
N年前我也同样质问过我的一个朋友,甚至说如果找不到可以帮他找,详细一问,是苹果机。我晕!经过一顿狂搜,终无果。或许没找对路,总之很难找,八成当时就没有。

论坛徽章:
0
59 [报告]
发表于 2007-06-23 10:43 |只看该作者
date -d "20010101" +"%a"

论坛徽章:
0
60 [报告]
发表于 2007-06-23 12:02 |只看该作者
try

  1. YEAR=2001
  2. seq 12 |sed "s,^,${YEAR}-," |awk '
  3. BEGIN {
  4.         initWeekTable();
  5. }

  6. function initWeekTable(){

  7.         _week[0] = "Sunday";
  8.         _week[1] = "Monday";
  9.         _week[2] = "Tuesday";
  10.         _week[3] = "Wednesday";
  11.         _week[4] = "Thursday";
  12.         _week[5] = "Friday";
  13.         _week[6] = "Saturday";       
  14. }

  15. function isLeapYear(pYear){

  16.         return pYear % 400 || pYear % 4 && !(pYear % 100);
  17. }

  18. function dispWeek(pWeek){

  19.         return _week[pWeek];
  20. }

  21. # date format: 2007[-06[-20]]
  22. function dateToWeek(pDate, tmpArr, count, year, month, day, days){

  23.         count = split(pDate, tmpArr, "-");

  24.         year = tmpArr[1]+0;
  25.         month = count > 1 ? tmpArr[2]+0 : 1;
  26.         day = count > 2 ? tmpArr[3]+0 : 1;

  27.         days = year-1 + int(year/4) - int(year/100) + int(year/400);
  28.         days += 2 * (month-1) + int(month/2) + int((month-1)/8);
  29.         if (month > 3) {

  30.                 if (!isLeapYear(year)) days--;
  31.                 else days -= 2;
  32.         }

  33.         return (days + day) % 7;
  34. }

  35. {
  36.         sum[dispWeek(dateToWeek($0))]++;
  37. }

  38. END {

  39.         for (i in sum) print i " " sum[i];
  40. }' |sort -n -k2 |cut -d' ' -f1
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP