LinuxSir.cn,穿越时空的Linuxsir!

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

介绍三个shell脚本

[复制链接]
发表于 2003-2-7 13:00:31 | 显示全部楼层 |阅读模式
完成相同的作业,看看他们之间的区别.
  1. #!/bin/csh -f
  2. # The Party Program--Invitations to friends from the "guest" file
  3. #set guestfile = ~/shell/guests
  4. set guestfile = ./guests
  5. if ( ! -e "$guestfile" ) then
  6.    echo "$guestfile:t non-existent"
  7.    exit 1
  8. endif
  9. setenv PLACE "Sarotini's"
  10. @ Time = `date +%H` + 1
  11. set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
  12. foreach person ( `cat $guestfile` )
  13.     if ( $person =~ root ) continue
  14.     mail -v -s "Party" $person << FINIS   # Start of here document
  15.     Hi ${person}! Please join me at $PLACE for a party!
  16.     Meet me at $Time o'clock.
  17.     I'll bring the ice cream. Would you please bring $food[1] and
  18.     anything else you would like to eat? Let me know if you can't
  19.     make it. Hope to see you soon.
  20.           Your pal,
  21.                 ellie@`hostname` # or `uname -n`
  22. FINIS
  23.    shift food
  24.    if ( $#food ==  0 ) then
  25.       set food = ( cheese crackers shrimp drinks "hot dogs"
  26.       sandwiches )
  27.    endif
  28. end
  29. #!/bin/sh
  30. # The Party Program--Invitations to friends from the "guest" file
  31. #guestfile=/home/jody/ellie/shell/guests
  32. guestfile=./guests
  33. if [ ! -f "$guestfile" ]
  34. then
  35.    echo "`basename $guestfile` non-existent"
  36.    exit 1
  37. fi
  38. PLACE="Sarotini's"
  39. export PLACE
  40. Time=`date +%H`
  41. Time=`expr $Time + 1`
  42. set cheese crackers shrimp drinks "hot dogs" sandwiches
  43. for person in `cat $guestfile`
  44. do
  45.    if [ $person = root ]
  46.    then
  47.       continue
  48.    else
  49.       mail -v -s "Party" $person <<- FINIS
  50.       Hi ${person}! Please join me at $PLACE for a party!
  51.       Meet me at $Time o'clock.
  52.       I'll bring the ice cream. Would you please bring $1 and
  53.       anything else you would like to eat? Let me know if you
  54.       can't make it.
  55.       Hope to see you soon.
  56.       Your pal,
  57.       ellie@`hostname`
  58. FINIS
  59.       shift
  60.       if [ $# -eq 0 ]
  61.       then
  62.          set cheese crackers shrimp drinks "hot dogs" sandwiches
  63.       fi
  64.    fi
  65. done
  66. echo "Bye..."
  67. #!/bin/ksh
  68. # The Party Program--Invitations to friends from the
  69. # "guest" file
  70. #guestfile=~/shell/guests
  71. guestfile=./guests
  72. if [[ ! -a "$guestfile" ]]
  73. then
  74.    print "${guestfile##*/} non-existent"
  75.    exit 1
  76. fi
  77. export PLACE="Sarotini's"
  78. (( Time=$(date +%H) + 1))
  79. set cheese crackers shrimp drinks "hot dogs" sandwiches
  80. for person in $(< $guestfile)
  81. do
  82.    if  [[ $person = root ]]
  83.    then
  84.       continue
  85.    else
  86.       mail -v -s "Party" $person <<- FINIS # Start of here document
  87.       Hi ${person}! Please join me at $PLACE for a party!
  88.       Meet me at $Time o'clock.
  89.       I'll bring the ice cream. Would you please bring $1
  90.       and anything else you would like to eat? Let me know
  91.       if you can't make it.
  92.       Hope to see you soon.
  93.             Your pal,
  94.             ellie@`hostname`
  95. FINIS
  96.       shift
  97.       if (( $# ==  0 ))
  98.       then
  99.          set cheese crackers shrimp drinks "hot dogs" sandwiches
  100.       fi
  101.    fi
  102. done
  103. print "Bye..."
复制代码
发表于 2003-2-8 15:51:29 | 显示全部楼层
还是Bash和Ksh比较简洁,Csh看起来不习惯。呵呵。
发表于 2003-2-8 18:05:32 | 显示全部楼层
第一次遇到fi的时候
我猜了好半天...
发表于 2005-3-7 22:10:57 | 显示全部楼层
我也看到了这个例子,我运行了第二个程序,提示说不存在guests文件,究竟guests应该是什么格式的文件?
回复 支持 反对

使用道具 举报

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

本版积分规则

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