免费注册 查看新帖 |

Chinaunix

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

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

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

help! please simplify this shell program!

if I add -n, then will give me this:

$ test1.sh 2003
-n Sunday
-n Thursday
-n Friday

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

help! please simplify this shell program!

原帖由 "acific Yan" 发表:
You are right, this line failed, but it works in Compaq Tru64 and also IBM, but doesn't work in SunOS 5.8, why?

see the following example in Tru64 korn shell:

$ cat sorttest
Friday 1
Sunday 1
..........


一个文件使用一个 BEGIN,END对.

而上面的echo |awk 是一行就要使用一个BEGIN,END对.

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

help! please simplify this shell program!

man echo看看

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

help! please simplify this shell program!

Great !!!!  It works!  You are my hero!  


$ cat >test1.sh
#!/usr/bin/ksh
if [ $# -ne 1 ];then exit 1;fi
if [ $1 -ge 1 ] && [ $1 -le 9999 ];then

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

$ test1.sh 2003
Sunday Thursday Friday

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

help! please simplify this shell program!

nothing
有空到ChongQing/HangZhou来玩,


--------------------------------主要是想你开饭.或带点儿美利坚的小玩意儿玩玩.

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

help! please simplify this shell program!

No problem, I work for a company and also as a part-time student at the same time.


keep in touch!

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

help! please simplify this shell program!

我的妹儿: bsdi@sina.com
你的呢?

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

help! please simplify this shell program!

my email: xyan0@yahoo.com

I will give you my phone number in my email.

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

help! please simplify this shell program!

ok.
太晚了,我要去睡觉了。
祝自己晚安。不知你那儿是几点?呵呵.

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

help! please simplify this shell program!

It is great that the above program can take one argument, now how can I modify this program and let it can take two arguments?

for example:

$ test.sh 2003 2007  or $ test.sh 2007 2003  (the order should not be matter)

It will consider the years 2003 2004 2005 2006 2007, then add all of them together and export the results like before where the weekdays are separated by a single space and in ascending order.

Do I have to do like this? Why it doesn’t work?







#!/usr/bin/ksh

#case where 2 years were supplied         
         
if ((aNumber<1)) || ((aNumber>9999)) || ((anotherNumber<1)) || ((anotherNumber>9999))
           then
             echo "year must be 1 to 9999"
             exit 1
fi
           
if ((aNumber>anotherNumber)); then
             temp=$aNumber aNumber=$anotherNumber anotherNumber=$temp
           
fi
           

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


cnt=0
           
Mon=0 Tue=0 Wed=0 Thu=0 Fri=0 Sat=0 Sun=0

str=`eval "print \"\$str_$da \""|awk 'BEGIN {OFS=" "; ORS=" "} {print $1}`’

case $str in
  "Sunday" ((Sun=$Sun+1));;
  "Monday" ((Mon=$Mon+1));;
  "Tuesday" ((Tue=$Tue+1));;
  "Wednesday" ((Wed=$Wed+1));;
  "Thursday" ((Thu=$Thu+1));;   
  "Friday" ((Fri=$Fri+1));;        
  "Saturday" ((Sat=$Sat+1));;  
esac
           
           
           
             ((cnt=$cnt+1))
           done
           
esac        
echo $finalstring  

#case where 2 years were supplied         [2-9]*([0-9])
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP