Tuesday, October 5, 2010

SA118 translation (12)


Chapter XIII reading shell scripts
Translation: hfzheng
hfzheng@sohu.com
Please keep this information reproduced
Basic shell script
A shell script is an ASCII file that contains a series of commands and comments.
Note that the text of the document to illustrate the functions of the script and the finished script for each line of the function needs to be done, comments begin with #.
Determine the type of shell script
Script, the top line shows the script shell program type, for example, for the Bourn shell, the first line should be:
#! / Bin / sh
Kernel using #! To identify the procedures used to translate the script.
Note: korn shell the first line should be #! / Bin / ksh, c shell the first line should be #! / Bin / csh.
However, not all of the shell script uses the top line to identify the shell program.
Some scripts can be at the top of a basic text notes. And the same shell the parent shell to execute the script. Some scripts may not comment at the top. In this case, use the default shell program to execute the script.
Create a basic shell script
Create a basic shell script using the following steps:
1, using the vi editor, create a file named my.script, enter the command, followed by the tab key, then the definition of a # at the beginning of the Notes;
2, so that files can be implemented, then the command line, enter the file command.
$ Vi my.script
who # To view who is logged on the system.
date # To view the current date and time
ls-l # To view files in current directory
: Wq
$ Chmod 755 my.script
$. / My.script
Bourne shell programming
Solaris Operating Environment for the management of the standard management bourne shell script is the script.
In order to successfully control the behavior of the operating environment changes, the system administrator must be able to read, modify and customize these different shell script file.
Usually, all the Bourne shell script including a set of UNIX commands, Bourne shell built-in commands, program structure and comments.
Bourne shell script
To be able to read, more important is to understand the content of the basic shell script to be able to:
1, can understand how the parameters passed to the script, by using a special built-in variable called location variable transmission;
2, to identify and analyze the conditions of simple structure and process control;
Special shell built-in variables
Location parameter
Special shell built-in variables called positional parameters for passing parameters to the script from the command line.
In the command line mode, with the script name separated by spaces after each word, as parameters, these parameters in the shell script called positional parameters.
Command format
scriptname argument1 argument2 argument3 ...
When the script execution, shell command line automatically store the first parameter to the positional parameters $ 1, the second parameter to the positional parameters $ 2, the third parameter to the location parameter # 3, and so on.
A location parameter reset
set command is used to reset the positional parameters.
The following example describes how to use the set command to set the location parameter.
First implementation of the option-m with the who command to see the output:
$ Who-m
user1 pts / 5 Mar 13 11:43 (host1)
$
Now create a shell script using the cat command, named my.script3:
$ Vi my.script3
#! / Bin / sh
set 'who-m'
echo Here are the positional variables that have been set:
echo The first is: $ 1
echo The second is: $ 2
echo The third is: $ 3
echo The fourth is: $ 4
echo The fifth is: $ 5
echo The sixth is: $ 6
echo This script is: $ 0
$ ^ D
Using the chmod command so that the script can do:
$ Chmod 755 script3
$
Implementation of the new script:
$ My.script3
Here are the positional variables that have been set:
The first is: user1
The second is: pts / 5
The third is: Mar
The fourth is: 13
The fifth is: 11:43
The sixth is: (host1)
This script is: my.script3
$
Bourne shell allows the use of up to 9 positional parameters, $ 1 to $ 9. Also on the parameters of the script name $ 0 reference.
Positional parameters meaning
$ 1 to $ 9, $ 1 to $ 9 reference position parameters
$ 0 references the current shell script name
Two other useful positional parameters is $ # and $ @.
1, $ #: Returns are stored in the $ 1 to $ 9 positional parameters of the total number of command line arguments;
2, $ @: start position parameters, display parameters for each store;
The following examples illustrate the role of script in the location parameters.
Create a script called my.script4, change the property to the executable.
$ Vi my.script4
#! / Bin / sh
echo The script name is: $ 0
echo
echo The first argument passed is: $ 1
echo The second argument passed is: $ 2
echo
echo The highest numbered parameter is: $ #
echo The parameters passed to the script are: $ @
^ D
$ Chmod 755 script4
$
Implementation of the new script, with stop and start arguments.
$ My.script4 start stop
The script name is: my.script4
The first argument passed is: start
The second argument passed is: stop
The highest numbered parameter is: 2
The parameters passed to the script are: start stop
$
Conditions of structure and process control
Conditions of order
Conditions command allows you to perform true or false based on a condition corresponding task. The simplest condition is if the command command.
If the command can test conditions, test results under the conditions to change the script execution process.
Command format
if command
then
command
command
fi
Exit status
When the program after the completion of the implementation (command line or script), returns an exit status, then back to shell, an exit status is an integer from 0 to 255.
According to the general command format given above, if the structure after the command is executed, and returns exit status. If the exit status to 0 in the then and fi command to be executed between the structure, fi terminated if block.
However, how the first command, the exit status non-zero, that command fails, then Keyword subsequent statements are ignored, processes the back between the jump fi statement.
Invalid parameter detected error conditions can cause the command failed.
Shell variable $? Shell automatically set by the last executed command exit status, echo command can display the parameter value.
The following example describes how to view the command line mode command a successful and unsuccessful implementation of the exit status.
$ Mkdir newdir
$
$ Echo $?
0
$
$ Mkdir
mkdir: Failed to make directory "newdir"; File exists
$ Echo $?
127
$
test command
Built-in test command if command is usually followed.
Test command is used to calculate the value of the expression, if the result is true, return zero exit status, if the result is false, return non-zero exit status.
Command format
if test expression
then
command
fi
The following example is used to test whether the variable $ name has been pre-set to the value of user2?
if test "$ name" = "user2"
then
echo "matches"
fi
Equal sign operator to test two values the same? In other words, test ordered $ name variable used to test whether the string user2, if so, return zero exit status, otherwise it returns non-zero exit status.
Another test format
test command is usually used in shell scripts, shell commands can also identify another test format, brackets [] in fact represents the test command.
The previous command can be rewritten as the following forms:
if ["$ name" = "user2"]
...
shell or the execution of the test command, and only use this format, test like the end of the expression a] bracket. In the first brackets [and after the final brackets] must be preceded by a space.
Command format
if [expression]
then
command
command
fi
The following example is used to test the value of an expression, if the result is true, return zero exit status, the result is false, return non-zero exit status.
Create a script called my.script5 so that it can perform, and execute the script.
$ Vi> my.script5
#! / Bin / sh
if
name = user5
["$ Name" = "user5"]
echo $?
then
name = user7
["$ Name" = "user5"]
echo $?
$
$ Chmod 755 my.script5
$ My.script5
0
1
$
test command operators
Almost every shell script should deal with one or more of the script, so there is a test command files for the classification operators, these operators make the file in the build script you can provide the tools for asking questions related documents.
The following are the most commonly used file operators:
Operator returns true (zero exit status) if
-D file file for the directory
-F file file is a regular file
-R file file can be read by the process
-S file file length of the non-zero
-W file file can be written process
-X file file executable
The following example describes how to use the file operator to test whether a file exists.
Create a script, named my.script6, make it executable, and run the script, the final step, check the exit status of the results.
$ Vi my.script6
#! / Bin / sh
if [-f $ HOME]
then
echo "found it!"
fi
$ Chmod 755 my.script6
$ My.script6
found it!
$ Echo $?
0
$
case command
case command is used to test the situation with multiple conditions.
Command format
case value in
value1) command
command;;
value2) command
command;;
value3) command
command;;
backup bin bin_old conf config crawler.tar.gz crawler_bin.tar.gz data eshow eshow_sitemap.html generate.sh google.html google.html.md5 log maint news: 10 news: 11 news: 12 news: 13 news: 14 news: 15 news: 16 news: 17 news: 18 news: 2 news: 3 news: 4 news: 5 news: 6 news: 7 news: 8 news: 9 outboundLinksMgr.sql seeds sitemap.html svn tasks tmp xml2dict-2008.6-tar. gz xml2dict-read-only) command;;
esac
case the value of the
case variable values match value1, value2, and so on, until a match is found.
If the case does not match the value of the variable, program execution value *) after the command, until the pair of brackets or esac.
The following example shows how to use the case command:
$ Vi my.script7
#! / Bin / sh
hour = 'date | cut-c12-13'
echo $ hour
case "$ hour" in
0? | 1 [01])
echo "Good Morning.";;
1 [2-7])
echo "Good Afternoon.";;
*)
echo "Good Evening.";;
esac
$ Chmod 755 my.script7
$ My.script7
16
Good Afternoon.
$
case command value calculation $ hour, stored in case the value of the variable in the current time, date command to its output through the pipelines to cut command, cut command cut hours and choose characters from the output by 12 and 13.
Case value of the variable $ hour match each value, until a match is found, then the results echo to the screen.
1, the first test value used to determine whether the matching case value of the variable hours of the morning? (Eg, 1:00 am to
11:00 am): 0? | 1 [01]), 0? To 0 and used to match a single character wildcard characters? . Therefore, the test values may be 01, 02, 03, 04, 05, 06, 07, 08, 09. | Is the or statement. 1 [01] is number 1 and for a single character wildcard [], where possible test value of 10 and 11.
2, the second test value used to determine whether the case matches the value of a variable number of hours this afternoon? (Eg, 12:00 pm to
5:00 pm): 1 [2-7]), may be the test value of 12, 13, 14, 15, 16, 17.
3, the third test value used to determine whether the matching case value of a variable number of hours at night? (Eg, 6:00 pm to
12:00 am): *), may test the value of 18, 19, 20, 21, 22, 23,24.
exit command
Built-in command called exit the shell allows the user to immediately terminate the shell script's execution. Exit frequently as the end, jump or return to the command line command.
Command format
exit n
exit command in the parameter range from 0-255 integer.
Can be placed in one or more of the script exit command, used to tell the shell to meet certain conditions when the exit script, which takes precedence over the normal end of the script.
In large script file, use different integer as the exit command of different parameters, when the need to determine when a specific condition is met is very useful.
If the script exit with 0 as the parameter, indicating the successful implementation of procedures, the normal end. Non-zero parameters (1-255) s description of certain types of errors occurred.
Users can use the echo command to quickly identify exit results:
$ Echo $?
Read a script sample Solaris Management
The following example is given of the Solaris Management is a script in the directory / etc / init.d in the maintenance.
These scripts can be used at boot time, you can also use the system to use closed.
System administrators can manually start or stop system daemons and services, through the implementation of stop and start with a parameter management script.
For example, to manually stop or start the audit process, root user can invoke the following command:
# / Etc / init.d / audit stop
# / Etc / init.d / audit start
Shell script using the previous learned knowledge, the following script line by line analysis to determine the procedures for the implementation of what will happen when:
/ Etc / init.d / audit script
1 #! / Sbin / sh
2 #
3 # Copyright (c) 1997 by Sun Microsystems, Inc.
4 # All rights reserved.
5 #
6 # ident "@(#) audit 1.5 97/12/08 SMI "
7
8 case "$ 1" in
9 'start')
10 if [-f / etc / security / audit_startup]; then
11 echo 'starting audit daemon'
12 / etc / security / audit_startup
13 / usr / sbin / auditd &
14 fi
15;;
16
17'stop ')
18 if [-f / etc / security / audit_startup]; then
19 / usr / sbin / audit-T
20 fi
21;;
22
23 *)
24 echo "Usage: $ 0 (start | stop)"
25 exit 1
26;;
27esac
28exit 0
interpretation of audit management script
Following the implementation of audit script line by line to explain what happened:
Line 1: #! / Sbin / sh, explain procedures for the implementation of the shell, here for the Bourne shell;
Line 2-6: comment lines, beginning with #;
Line 7: blank line
Line 8: case expression, given the location parameter case the variable $ 1, the variable pass the first command line argument to the script.
Line 9: Description of the variables given case the first one matched value, in this case start; If the two values match, the result is true, return a zero exit status (0), the script jump to the next line, line 10 . If the two values do not match, the result is false, returns a non-zero exit status, the script jumps directly;; after the statement, here for the first 16 rows;
Line 10: if conditional statement to test the value of an expression; If the file / etc / security / audit_startup exist, the result is true, jump to the next line, 11 rows. If the file / etc / security / audit_startup does not exist, the script jumps directly to the implementation of the fi statement, here for the first 15 rows;
Line 11: implementation of the echo command to display the starting audit daemon to the screen;
Line 12: implementation of the audit subsystem initialization script audit_startup safe in the actual start of the audit daemon's security;
Line 13: Start audit daemon, the audit trace file generation process control, and audit trace file location;
Line 14: if block statement marks the end;
Line 15: Send flow control instructions, make the script skip esac statement after the implementation of 16 to 27 rows to skip and not be executed;
Line 16: blank line;
Line 17: Description of the second case the variable value, here for the stop; If the two values match, the result is true, return a zero exit status (0), skip to the next line of the script execution, which is 18 lines, if two values do not match, the result is false, returns a non-zero exit status, the script jumps directly;; after the statement, here for the first 22 rows;
Line 18: if conditional statement to test the value of an expression; If the file / etc / security / audit_startup exist, the result is true, the script the next line, that is, line 19, if the file / etc / security / audit_startup does not exist keyword after the statement is ignored, the script jumps to the Executive;; after the statement, here is the line 21;
Line 19: implementation of the / usr / sbin / audit-T function, to send signals to the audit daemon, telling audit daemon to close the current audit trace file, so that monitoring failure, death;
Line 20: end if block statements;
Line 21: send flow control instructions, make the script skip esac statement after the execution, the first 22-27 rows to skip and not be executed;
Line 22: blank line;
Line 23: If the case can not match the variable 9 rows and 17 rows of values, shell command line implementation of Article 24-25;
24 line: the implementation of echo command displays the following text string to the screen:
string to the screen:
Usage: / etc / init.d / audit (start | stop)
Line 25: implementation of the exit command to terminate the script immediately, and return to the command line mode; instruction program appears certain types of errors;
26 line: send flow control instructions, scripts, skip to the last line after the esac statement;
Line 27: end case statement block;
Line 28: implementation of the exit command to terminate the script, return to the command line mode, just exit the program successfully.









Recommended links:



AVI to 3GP



5 things to keep in mind while purchasing green



WMV to MPEG



ASF to MPG



No comments:

Post a Comment