Evaluating Variables in a Flat File

2 272 0
Evaluating Variables in a Flat File

Đang tải... (xem toàn văn)

Thông tin tài liệu

181 ■ ■ ■ CHAPTER 26 Evaluating Variables in a Flat File O ne common scripting technique is to create a flat file that is one of a potential number of canned messages for users to receive—messages such as notifications of downtimes, changes in the environment, or use of system quota. (I consider a flat file simply a file that contains text.) Based on the logic in the script, the proper message will be sent to the user or users. A more advanced implementation of this technique provides a template that refers to environment variables and contains customizable elements so the message can be tailored specifically to the recipient. In Chapter 36, I present a script that checks every morning for user passwords that are aging and therefore should be changed. In that script, when a password has reached the predetermined cut-off age, the script sends an e-mail to the account owner to state that the account will be locked if the owner doesn’t update her password. The script annoys the user every day for a couple of weeks, after which the account is locked. After that, if the user wants to use the account she would have to call and explain why she didn’t heed the friendly e-mail warnings. The canned template file looks like this: $ENVIRONMENT account password for \\"$USERID\\" expires in $REMAINING day\\(s\\) ============================================================== ++ ACTION NEEDS TO BE TAKEN OR YOUR ACCOUNT WILL BE LOCKED ++ ++ IN $REMAINING day\\(s\\) ++ ============================================================== If the password isnt changed within $REMAINING day\\(s\\), account will be locked. Instructions for changing passwords are located at: \\<a href=\\"http://server.company.com/chg_passwd.html\\"\\> http://server.company.com/chg_passwd.html\\</a\\> If you are unable to change your password, please call the Help Desk. 182 CHAPTER 26 ■ EVALUATING VARIABLES IN A FLAT FILE If you no longer need this account, please let us know so we can remove it. System Administration Note in particular the shell variables (ENVIRONMENT, USERID, and REMAINING) and their escape sequences. When the script executes, the account expiration date for each possi- ble USERID is checked, and the script determines how many days REMAINING there are before the account is frozen. The ENVIRONMENT variable specifies the environment from which the message is being sent. The occurrences of these variables must be replaced with their values before the script is run and the message is sent. Likewise, each escape sequence \\ evaluates to a single backslash that causes the following special character to be escaped so that it is treated as plain text when the script executes. For each user with a soon-to-expire password, the template file is evaluated at runtime to replace the included variables with their contents. The code to perform the replace- ments in each case is fairly simple. It consists of one small loop that looks at each line in the flat file and replaces any variables with their assigned values. You can easily modify the code for other purposes—for example, to display the modified file. cat $flat_file | while read a_line do place_holder=`eval echo $a_line` echo $place_holder done | $MAIL -s "$ENVIRONMENT UNIX Account Notification" $RECIPIENT The loop processes each line of the file using the eval statement, which causes the variables to be replaced with their string values. The expanded line is assigned to the variable place_holder so that it can be echoed to standard output. Once all lines have been processed, the complete output is sent via e-mail to the specified RECIPIENT by the e-mail command in the final element of the command pipeline. The specific e-mail command is up to you; a couple of common ones are mailx and mail, depending on your operating system. This technique has many potential uses. The example script here is much like a tradi- tional mail merge where you customize the message based on specific users. You could also use this technique for creating and maintaining configuration for an application, or custom files for individual users patterned after a default file. . CHAPTER 26 Evaluating Variables in a Flat File O ne common scripting technique is to create a flat file that is one of a potential number of canned messages. http://server.company.com/chg_passwd.html\< /a > If you are unable to change your password, please call the Help Desk. 182 CHAPTER 26 ■ EVALUATING VARIABLES IN A FLAT FILE

Ngày đăng: 05/10/2013, 08:51

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan