Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4482839
  • 博文数量: 109
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 2457
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-18 19:04
文章分类

全部博文(109)

文章存档

2011年(1)

2010年(10)

2009年(36)

2008年(62)

我的朋友

分类: LINUX

2008-11-29 10:28:23

Example 2.7.
1   #!/bin/bash

    # GNU bash versions 2.x

2   # The Party Program––Invitations to friends from the "guest" file

3   guestfile=~/shell/guests

4   if [[ ! –e "$guestfile" ]]

    then

5       printf "${guestfile##*/} non–existent"

        exit 1

    fi

6   export PLACE="Sarotini's"

7   (( Time=$(date +%H) + 1 ))

8   declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)

9   declare -i  n=0

10  for person in $(cat $guestfile)

    do

11      if  [[ $person == root ]]

        then

              continue

        else

              # Start of here document

12            mail –v –s "Party" $person <<- FINIS

              Hi $person! Please join me at $PLACE for a party!

              Meet me at $Time o'clock.

              I'll bring the ice cream. Would you please bring

              ${foods[$n] and anything else you would like to eat?

              Let me know if you can make it.

                     Hope to see you soon.

                          Your pal,

                          ellie@$(hostname)

              FINIS

13            n=n+1

14            if (( ${#foods[*]} ==  $n ))

              then

15               declare -a foods=(cheese crackers shrimp drinks `"hot dogs"`

                                   sandwiches)

16            n=0

              fi

        fi

17  done

    printf "Bye..."


EXPLANATION

  1. This line lets the kernel know that you are running a Bash shell script.

  2. This is a comment. It is ignored by the shell, but important for anyone trying to understand what the script is doing.

  3. The variable guestfile is set to the full pathname of a file called guests.

  4. This line reads: If the file guests does not exist, then print to the screen "guests nonexistent" and exit from the script.

  5. The built-in printf function dipslays only the filename (pattern matching) and the string "non-existent".

  6. An environment (global) variable is assigned and exported.

  7. A numeric expression uses the output of the UNIX/Linux date command to get the current hour. The hour is assigned to the variable, Time.

  8. A Bash array, foods, is defined (declare –a) with a list of elements.

  9. An integer, n, is defined with an initial value of zero.

  10. For each person on the guest list, except the user root, a mail message will be created inviting the person to a party at a given place and time, and assigning a food from the list to bring.

  11. If the value in $person is root, control goes back to the top of the for loop and starts at the next person on the list.

  12. The mail message is sent. The message body is contained in a here document.

  13. The integer, n, is incremented by 1.

  14. If the number of foods is equal to the value of the last number in the array index, the list is empty.

  15. The array called foods is reassigned values. After a message has been sent, the food list is shifted so that the next person will get the next food on the list. If there are more people than foods, the food list will be reset, ensuring that each person is assigned a food.

  16. The variable n, which will serve as the array index, is reset back to zero.

  17. This marks the end of the looping statements.

阅读(1686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~