分类: LINUX
2008-11-29 10:28:23
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
This line lets the kernel know that you are running a Bash shell script.
This is a comment. It is ignored by the shell, but important for anyone trying to understand what the script is doing.
The variable guestfile is set to the full pathname of a file called guests.
This line reads: If the file guests does not exist, then print to the screen "guests nonexistent" and exit from the script.
The built-in printf function dipslays only the filename (pattern matching) and the string "non-existent".
An environment (global) variable is assigned and exported.
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.
A Bash array, foods, is defined (declare –a) with a list of elements.
An integer, n, is defined with an initial value of zero.
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.
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.
The mail message is sent. The message body is contained in a here document.
The integer, n, is incremented by 1.
If the number of foods is equal to the value of the last number in the array index, the list is empty.
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.
The variable n, which will serve as the array index, is reset back to zero.
This marks the end of the looping statements.