LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1422|回复: 9

查询‘星期几’脚本。(shell编程应该不局限在“用于操作”)

[复制链接]
发表于 2005-4-28 16:49:49 | 显示全部楼层 |阅读模式
。编写脚本,已知年月日,求出当天为那星期天。

。建bin目录
$[ -d $HOME/bin ] || mkdir $HOME/bin

。编写查询文件
$vi $HOME/bin/ye.txt
$vi $HOME/bin/m-d-s.txt

。ye.txt和m-d-s.txt内容如下:

1。ye.txt文件内容:
---------------------------------------------
A 00 06 17 23 28 34 45 51 56 62 73 79 84 90 -- s15-s19-s23
B 01 07 12 18 29 35 40 46 57 63 68 74 85 91 96 s15-s19-s23
C 02 13 19 24 30 41 47 52 58 69 75 80 86 97 -- s15-s19-s23
D 03 08 14 25 31 36 42 53 59 64 70 81 87 92 98 s15-s19-s23
E 09 15 20 26 37 43 48 54 65 71 76 82 93 99 -- s15-s19-s23
F 04 10 21 27 32 38 49 55 60 66 77 83 88 94 -- s15-s19-s23
G 05 11 16 22 33 39 44 50 61 67 72 78 89 95 -- s15-s19-s23

G 00 06 17 23 28 34 45 51 56 62 73 79 84 90 -- s16-s20-s24
A 01 07 12 18 29 35 40 46 57 63 68 74 85 91 96 s16-s20-s24
B 02 13 19 24 30 41 47 52 58 69 75 80 86 97 -- s16-s20-s24
C 03 08 14 25 31 36 42 53 59 64 70 81 87 92 98 s16-s20-s24
D 09 15 20 26 37 43 48 54 65 71 76 82 93 99 -- s16-s20-s24
E 04 10 21 27 32 38 49 55 60 66 77 83 88 94 -- s16-s20-s24
F 05 11 16 22 33 39 44 50 61 67 72 78 89 95 -- s16-s20-s24

E 00 06 17 23 28 34 45 51 56 62 73 79 84 90 -- s17-s21-s25
F 01 07 12 18 29 35 40 46 57 63 68 74 85 91 96 s17-s21-s25
G 02 13 19 24 30 41 47 52 58 69 75 80 86 97 -- s17-s21-s25
A 03 08 14 25 31 36 42 53 59 64 70 81 87 92 98 s17-s21-s25
B 09 15 20 26 37 43 48 54 65 71 76 82 93 99 -- s17-s21-s25
C 04 10 21 27 32 38 49 55 60 66 77 83 88 94 -- s17-s21-s25
D 05 11 16 22 33 39 44 50 61 67 72 78 89 95 -- s17-s21-s25

C 00 06 17 23 28 34 45 51 56 62 73 79 84 90 -- s18-s22-s26
D 01 07 12 18 29 35 40 46 57 63 68 74 85 91 96 s18-s22-s26
E 02 13 19 24 30 41 47 52 58 69 75 80 86 97 -- s18-s22-s26
F 03 08 14 25 31 36 42 53 59 64 70 81 87 92 98 s18-s22-s26
G 09 15 20 26 37 43 48 54 65 71 76 82 93 99 -- s18-s22-s26
A 04 10 21 27 32 38 49 55 60 66 77 83 88 94 -- s18-s22-s26
B 05 11 16 22 33 39 44 50 61 67 72 78 89 95 -- s18-s22-s26

---------------------------------------------

2。m-d-s.txt内容:
---------------------------------------------

A B C D E F G -- 1 10
G A B C D E F -- 5 --
F G A B C D E -- 8 r2
E F G A B C D -- 2 3 11
D E F G A B C -- 6
C D E F G A B -- 9 12
B C D E F G A -- 4 7 r1

st1 st2 st3 st4 st5 st6 st7 -- 1d 8d 15d 22d 29d
st2 st3 st4 st5 st6 st7 st1 -- 2d 9d 16d 23d 30d
st3 st4 st5 st6 st7 st1 st2 -- 3d 10d 17d 24d 31d
st4 st5 st6 st7 st1 st2 st3 -- 4d 11d 18d 25d
st5 st6 st7 st1 st2 st3 st4 -- 5d 12d 19d 26d
st6 st7 st1 st2 st3 st4 st5 -- 6d 13d 20d 27d
st7 st1 st2 st3 st4 st5 st6 -- 7d 14d 21d 28d

----------------------------------------------

。编写脚本

$vi $HOME/bin/w.sh
----------------------------------------------
#!/bin/bash
# mkdir $HOME/bin;vi $HOME/bin/ye.txt;vi $HOME/bin/m-d-s.txt
# run w.sh ;import year month days ;export stars

#判断输入的位置参数,年(1500-2600)#

if [ "y$3" == "y" ]
then
  echo "errors variable: please again import"
  exit 1
elif (( $1 < 1500 || $1 > 2600 ))
then
  echo "year 1500-2600: please again import"
  exit 2
elif (( $3 < 1 || $3 > 31 ))
then
  echo "days 1-31: please again import"
  exit 3
elif (( $2 < 1 || $2 > 12 ))
then
  echo "month 1-12:  please again import"
  exit 4
fi

#判断闰年,平年

ye=pingnian
(( i=$1 % 4 ));(( i100=$1 % 100 ));(( i400=$1 % 400 ))

if (( $i == 0 )) ;then
  if (( $i100 == 0 )) ;then
    if (( $i400 == 0 ))
then  ye=runnian
    fi
else ye=runnian
   fi
fi

#判断输入的位置参数,月(1-12),日(1-31)#

case $2 in
2)
if [ "$ye" = "pingnian" -a $3 -gt 28 ]
then
echo "errors:平年2月为28天"
exit 5
elif [ "$ye" = "runnian" -a $3 -gt 29 ]
then
echo "errors:闰年2月为29天"
exit 6
fi
;;
4)
if (( $3 > 30 ))
then
echo "errors2月为30天"
exit 7
fi
;;
6)
if (( $3 > 30 ))
then
echo "errors2月为30天"
exit 8
fi
;;
9)
if (( $3 > 30 ))
then
echo "errors2月为30天"
exit 9
fi
;;
11)
if (( $3 > 30 ))
then
echo "errors2月为30天"
exit 10
fi
;;
esac

# 从ye.txt文件中查询引数

(( yef=$1/100 ))
(( yeb=$1-$yef*100 ));(( $yeb < 10 )) && yeb=0$yeb
hh=`grep ".*$yeb.*s$yef.*" $HOME/bin/ye.txt|cut -d' ' -f 1`

# 从m-d-s.txt文件查询星期几

if [ $ye = pingnian ];then mon=$2
elif [ $2 = 1 -o $2 = 2 ];then mon=r$2
else mon=$2
fi
num=0

## .*$hh.* $mon.* , $mon前面留空格

for inum in `grep ".*$hh.* $mon.*" $HOME/bin/m-d-s.txt`
do
(( num=num+1 ))
[ $inum = $hh ] && break
done

day=$3'd'

# " $day", $day前面留空格

stars=`grep " $day" $HOME/bin/m-d-s.txt|cut -d' ' -f $num`

case "$stars" in
st1) stars='星期一'
;;
st2) stars='星期二'
;;
st3) stars='星期三'
;;
st4) stars='星期四'
;;
st5) stars='星期五'
;;
st6) stars='星期六'
;;
st7) stars='星期日'
;;
esac

echo "$1-$2-$3 is $stars"
unset

------------------------------------------------

$chmod +x $HOME/bin/w.sh

。例子:

$ $HOME/bin/w.sh 2005 4 22
2005-4-22 is 星期五
 楼主| 发表于 2005-4-28 16:55:53 | 显示全部楼层
请‘各位’验证一下。
回复 支持 反对

使用道具 举报

发表于 2005-4-28 17:14:42 | 显示全部楼层
我这里好象不行啊
  1. zhyfly@zhyfly:~/bin$ ./w.sh 2005 4 22
  2. ./w.sh: line 102: [: B: unary operator expected
  3. ./w.sh: line 102: [: C: unary operator expected
  4. ./w.sh: line 102: [: D: unary operator expected
  5. ./w.sh: line 102: [: E: unary operator expected
  6. ./w.sh: line 102: [: F: unary operator expected
  7. ./w.sh: line 102: [: G: unary operator expected
  8. ./w.sh: line 102: [: A: unary operator expected
  9. ./w.sh: line 102: [: --: unary operator expected
  10. ./w.sh: line 102: [: 4: unary operator expected
  11. ./w.sh: line 102: [: 7: unary operator expected
  12. ./w.sh: line 102: [: r1: unary operator expected
  13. ./w.sh: line 102: [: st4: unary operator expected
  14. ./w.sh: line 102: [: st5: unary operator expected
  15. ./w.sh: line 102: [: st6: unary operator expected
  16. ./w.sh: line 102: [: st7: unary operator expected
  17. ./w.sh: line 102: [: st1: unary operator expected
  18. ./w.sh: line 102: [: st2: unary operator expected
  19. ./w.sh: line 102: [: st3: unary operator expected
  20. ./w.sh: line 102: [: --: unary operator expected
  21. ./w.sh: line 102: [: 4d: unary operator expected
  22. ./w.sh: line 102: [: 11d: unary operator expected
  23. ./w.sh: line 102: [: 18d: unary operator expected
  24. ./w.sh: line 102: [: 25d: unary operator expected
  25. 2005-4-22 is
  26. zhyfly@zhyfly:~/bin$ vim $HOME/bin/w.sh
  27. zhyfly@zhyfly:~/bin$ ./w.sh
  28. errors variable: please again import
  29. zhyfly@zhyfly:~/bin$ ./w.sh 2004 5 21
  30. ./w.sh: line 102: [: G: unary operator expected
  31. ./w.sh: line 102: [: A: unary operator expected
  32. ./w.sh: line 102: [: B: unary operator expected
  33. ./w.sh: line 102: [: C: unary operator expected
  34. ./w.sh: line 102: [: D: unary operator expected
  35. ./w.sh: line 102: [: E: unary operator expected
  36. ./w.sh: line 102: [: F: unary operator expected
  37. ./w.sh: line 102: [: --: unary operator expected
  38. ./w.sh: line 102: [: 5: unary operator expected
  39. ./w.sh: line 102: [: --: unary operator expected
  40. ./w.sh: line 102: [: st5: unary operator expected
  41. ./w.sh: line 102: [: st6: unary operator expected
  42. ./w.sh: line 102: [: st7: unary operator expected
  43. ./w.sh: line 102: [: st1: unary operator expected
  44. ./w.sh: line 102: [: st2: unary operator expected
  45. ./w.sh: line 102: [: st3: unary operator expected
  46. ./w.sh: line 102: [: st4: unary operator expected
  47. ./w.sh: line 102: [: --: unary operator expected
  48. ./w.sh: line 102: [: 5d: unary operator expected
  49. ./w.sh: line 102: [: 12d: unary operator expected
  50. ./w.sh: line 102: [: 19d: unary operator expected
  51. ./w.sh: line 102: [: 26d: unary operator expected
  52. 2004-5-21 is
  53. zhyfly@zhyfly:~/bin$
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-4-29 00:04:37 | 显示全部楼层
牛!!!
为什么有cal现成的不用 :ask

  1. read year month day
  2. LC_ALL=C cal $monty $year|awk 'NR<3;/\<'"$day"'\>/'
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-4-29 09:44:55 | 显示全部楼层
用date好像也可以。
我认为使用shell的目的1就是程序自动化,2就是使复杂的东西变得简单,有简单的不用,那就不好玩了。
楼主的脚本太复杂了,(我基本是一眼带过)而且还出错,出错后还很难找出问题。
要加注释
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-29 10:22:55 | 显示全部楼层
没错!
版主再试一下,我刚才试了一次,没问题。

打开gedit,分别用全文复制方法在bin目录下生成ye.txt,m-d-s.txt,w.sh,
然后chmod +x w.sh

版主应该错复制了这一点:

$cat -n w.sh|grep -B 5 102
    97        ## .*$hh.* $mon.* , $mon前面留空格
    98       
    99        for inum in `grep ".*$hh.* $mon.*" $HOME/bin/m-d-s.txt`
   100        do
   101        (( num=num+1 ))
   102        [ $inum = $hh ] && break
回复 支持 反对

使用道具 举报

发表于 2005-4-29 11:50:28 | 显示全部楼层
Post by thumb-xiaowei
没错!
版主再试一下,我刚才试了一次,没问题。

打开gedit,分别用全文复制方法在bin目录下生成ye.txt,m-d-s.txt,w.sh,
然后chmod +x w.sh

版主应该错复制了这一点:

$cat -n w.sh|grep -B 5 102
    97        ## .*$hh.* $mon.* , $mon前面留空格
    98       
    99        for inum in `grep ".*$hh.* $mon.*" $HOME/bin/m-d-s.txt`
   100        do
   101        (( num=num+1 ))
   102        [ $inum = $hh ] && break

恩,应该没问题,呵呵,可能是我搞错了要出去,等回来好好试试!
回复 支持 反对

使用道具 举报

发表于 2005-5-5 17:54:06 | 显示全部楼层
date --date="2005/11/13" |awk '{print $1}'
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-6 15:50:24 | 显示全部楼层
up up up up up up
up
up up up up up up
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-6 15:50:18 | 显示全部楼层
Post by yuri-master
用date好像也可以。
我认为使用shell的目的1就是程序自动化,2就是使复杂的东西变得简单,有简单的不用,那就不好玩了。
楼主的脚本太复杂了,(我基本是一眼带过)而且还出错,出错后还很难找出问题。
要加注释



## 本人用以下几点回答所题问题 ##

例子: 查询2005.4.21是星期几

1. 用cal命令
$ cal 4 2005
      四月 2005
日 一 二 三 四 五 六
。 。 。 。 。 01 02
03 04 05 06 07 08 09
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

[ 用眼睛翻 --+ 懒人所为 ]

2. root 用date命令
$ su -

把当前系统时间改为2005.4.21
# date -s 20050421
# date
四  4月 21 00:00:15 CST 2005
# date +%A
星期四

把系统时间改成当前正确时间
# date -s 20050506
# date -s 14:27:00
# clock -w

[ --+ 小题大作 ]

3. 普通帐户用date命令

$ date -s 20050421
date: cannot set date: Operation not permitted
四  4月 21 00:00:00 CST 2005

[ 最快, 最直接, 利用错误信息 --+ 不是正途 ]

4. panlm 提供date合适用法
$ date --date="2005/4/21"
四  4月 21 00:00:00 CST 2005

[ good ! 学习之道! --+ 知道走了弯路, 才明确转折点 ]

5. 在网页www.douzhe.com/linuxtips/type4.html 有类似脚本,
跟( ta )交流一下吧 .

6.  
$ $HOME/bin/w.sh 2005 4 21

[ 而本人的脚本什么评价都可以, 只要加一点严谨.
最重要的是 --+ 你们仍... 仍..... 仍.. 仍在. 仍在聊天 ]

-
-
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表