Shell script learning and summary

Source: Internet
Author: User
Tags syslog

1. shell scripts are case sensitive
2. Special Unix characters: (; $? & * () [] ''+ Escape ()
3. Shell Comments start #
4. Function Definition
Function fuction_name (){
Command to execute
}
Function_name is directly used for calling.
5. Control Structure
1) If... then statement
If [test_command]
Then
Commands
If
2) If... then... else statement
If [test_command]
Then
Commands
Else
Commands
If
3) If... then... elif... then... (else) Statement
If [test_command]
Then
Commands
Elif [test_command]
Then
Commands
Else
Commands
Fi
4) for... In statement
For loop_varible in argument_list
Do
Commands
Done
5) while statement
While test_command_is_true
Do
Commands
Done
6) until statement
Until test_command_is_true
Do
Commands
Done
7) case statement
Case $ variable in
Match_1)
Commands_for_1

Match_2)
Commands_for_2

.
.
.
*) # Option for other values
Commands_for_no_match

Esac
6. break, continue, exit, and return statements
Break jumps out of the entire body, and then executes the following code in vitro;
Continue ends this cycle and continues the next cycle;
Exit to exit the entire script. Generally, an integer (such as Exit 0) is added to the end of the script and sent to the system as the return code;
Return is used to Return data in a function or to call a function.
7. here document
Redirects input to an interactive shell script or program without user intervention.
Program_name <LABLE
Program_input_1
Program_input_2
.
.
Program_input _#
LABLE
Note that there is no blank between LABLE tags in the input line of the program, and the entered data must be the expected accurate data of the program, otherwise it may become invalid.
8. Symbolic commands
() Run the commands enclosed in parentheses in a subshell.
() Evaluate and assign values to variables in a shell and perform mathematical operations
$ () Evaluates the enclosed expression
[] Same as the test command
[[] For string comparison
$ () Command replacement
''Command replacement
9. Command Line Parameters
The command line parameter $0, $1, $2,..., $9 is the location parameter, and $0 points to the Command itself.
Shift is used to move the location parameter to the left, for example, shift command $2 to $1. Shift adds a number to move multiple positions. For example, shift 3 makes $4 $1. Shift is a good way to process parameters at each location in the order listed by parameters.
10. Special Parameters
$ * Specify all command line parameters, which are of the same significance as $. The two have different meanings only when double quotation marks are added, for example
"$ *" Is used to obtain the entire parameter list as a parameter, "$ @" is used to obtain the entire parameter list and separate it into different parameters.
$? Check the returned code. The return code of a successfully executed command is 0, and the Failure value is a non-0 value.
11. Double quotation marks, single quotation marks, and '(keys under esc)
Single quotation marks ''are used to completely reference the content. That is to say, the variable command statement uses the text body without any replacement. If double quotation marks are partially referenced, character replacement or command replacement is allowed.
'(Press the button under esc) is used to execute a command or script and replace the output result, that is, replace the command. The same functions include $ (). In addition, if you want to re-read the value of a variable every time you use it, such as '$ pwd', the new value is re-read every time you use this variable.
12. File Permission and sticky bits (suid, sgid)
File permissions include read, write, and execution permissions. Set the file operation mode to always be a specific user (suid), or always as a specific group member (sgid) for execution. You can run the chmod command to modify the file permissions.
13. run commands on the remote host
Ssh user @ hostname command_to_execute
Example: ssh jack@192.168.1.3 "uptime"
14. Set traps
When a program is forced to stop, an exit signal is called a trap ). In this way, we can execute the command when capturing the exit signal, such as logging out when the exit signal is 1, 2, 3, 15:
Trap 'echo "nEXITTING on a trapped singal"; exit '1 2 3 15
Note that the exit signal of kill-9 cannot be captured.
15. View user information
Who provides the username, tty, Logon Time, and IP address of each login user)
W extension to who, including job process time, total user process time, but no user logon location information.
Last displays the user name list information that has been logged on since the creation of the wtmp file, including the logon time, Exit Time, And tty.
16. ps command
Displays information about the current system process.
17. communicate with users
Wall, rwall, write, talk
18. case-sensitive text
Use the tr or typeset command.
VALUES = "AFCDLD"
Echo $ VALUES | tr' [A-Z] ''[a-z] '# converts uppercase to lowercase; tr' [a-z] ''[A-Z] 'converts lowercase letters to uppercase letters.
Or
Before using VALUES
Typeset-l VALUES # converts uppercase to lowercase, and typeset-u converts lowercase to uppercase.
19. Run the script cron regularly.
Crontab-e: Enter the user cron table to add a scheduled script, as shown in figure
Run the script/usr/bin/test. sh at on Sunday, January 15.
# Minute (0-59) hour (0-23) Day (1-31) month (1-12) Week (0-6for Sunday-Saturday)
12 0 15 1 0/usr/bin/test. sh
Scheduled tasks can also use the at command.
20. Output Control
Silent running, that is, no content is output to the screen: 2> & 1>/dev/null
Output to the console specified by the system:>/dev/console
21. parse the command line parameter getopts
Getopts optionstring VARIABLE
Optionstring is a required parameter, which is separated by a colon. If no parameter is required, the colon can be omitted. If there is a colon Before optionstring, any unmatched? No.
Getopts is used to parse a parameter and then use this parameter for different operations. For example:
While getopts: s: m: h: d: p: TM
Do
Case $ TM in
S)
Do something

M)
Do something

.
.
.
?)
Exit 1

Esac
22. process files row by row
While read LINE
Do
Echo "$ LINE"
Done <$ FILENAME
23. Create a menu using the select command
Select menu in Yes No Quit
Do
Case $ menu in
Yes)
Do something

No)
Do something

Quit)
Break

*)
Do something

Esac
Done

Shell script Learning

1. Set the runtime environment

Write at the top of the script :#! /Bin/bash2, variables in SHELL and values str = hello
Variables in linux do not need to be defined. Values are assigned directly when needed. For example, str. Note that there cannot be spaces on both sides of the equal sign str = 'LS-l/tmp/Sh'. If you want to assign the execution result of a command to a variable, = 'should be included in the right side
Echo "$ str"
View the value of the variable. The result is hello3. Input a character or value from the keyboard to assign the specified variable read name. For example, if lishi is input from the keyboard, the value of the name is lishi4, "", '','' difference between double quotation marks, single quotation marks, and inverted quotation marks
Echo "my name is $ name"
Displays the string, but contains escape characters to reference the value of its variable. Result In the example: my name is tom
Echo 'My name is $ name'
The content in the single quotes is displayed as is. The result is: my name is $ name.
Echo 'LS-l'
Use the characters in the inverted quotation marks as commands for execution and display the execution results. 5. addition, subtraction, good, division, modulo operation. Be sure to enclose them in inverted quotation marks.
Expr '5 + 4'
Expr '5-4'
Expr '5 \ * 4'
Expr '20140901'
Expr '5% 4'
If the operation is performed in the script, the inverted quotation marks must include all the content on the right of the = sign. For example, sum = 0sum = 'expr $ sum + 1' 6, a command for text operations

Less
More: one screen and one screen
The header 10 line parameter-n 5 indicates that only the first 5 lines are displayed.
Tail reads the last 10 lines of text and adds the-f parameter to view the changes in log files in real time. See tomcat Log File changes. Tail-f/usr/tomcat/logs/canitsl. out

Parameter-n 5 indicates that only the first 5 rows 7, $? Indicates whether the previous command is correctly executed, 0 indicates normal, and 1 indicates Error
Ls/tmp/hello, if/tmp/does not have the file or directory "hello. Then $? 1, and 08,./test lishi wangwu
$0 program name, $1 Name of the first parameter. In this example, $0 is test.
$ * A string composed of parameters. In this example, $ * Is lishiwangwu.
$ # The total number of parameters passed to the program. In this example, $ # is 29. Variables in linux are classified into global environment variables and user configuration variables.
The working environment directory set by global environment variables for all users in the system, in/etc/profile
User configuration variables target a user. In the user login directory, 10 in. bash_profile, redirection is to change the direction of the original input and output. By default, it is a screen output device, and the keyboard is an input device. ">" Is the output redirection character. "<" Is the input redirection character. ">" Only stores the correct information, and "2>" stores the wrong information. Before each storage, the previous file content is cleared and then put into the following: ls/usr>/tmp/aaa: store all files and folder names in the/usr directory in the expired/tmp/aaa file.
For example, if ls/test 2>/tmp/aaa does not have a/test folder, an error occurs, and 2> stores the error information in the aaa file.
">" Can create new files, such as:> hello. java
">" Can clear a file such as hello. the java file contains content. I will go to "hello. java, hello. java content clearing ">" becomes the append function when the numbers are greater than one another, and append the content after the previous file content. For example, cat/tmp/sh>/tmp/aaa append all files and directory information in the/tmp/sh directory to the aaa file. The content before the aaa file is not cleared. When cat> hello. java, you can enter a lot of content on the screen, and press ctrl + D to exit. When cat hello. java is used
The entered content is all in the hello. java file 11. input redirection, for example, cat> a.txt <eee, from the screen input to a.txt, will not end until the EEE is entered !!!! This combination method is often used to automatically execute and record certain logs or write information.

SYSPROFILE =/etc/profilecat> $ SYSPROFILE <EOF
Export JAVA_HOME =/usr/java/jsdk
Export JAVA_OPTS = "-Xms64m-Xmx768m"
Export PATH = $ JAVA_HOME/bin: $ JAVA_HOME/jre/bin: $ PATH
Export CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar
EOF12, MPs queue: Use the output of the previous command as the input of the next command. As the name suggests, it is the role of connecting the two pipelines.
Connect the end of the last pipe to the header of the next pipe. Ls-l/tmp/test | wc-l counts the total number of files and directories in the/tmp/test directory. Display the files and directories in the/tmp/test directory by column. The displayed result is used as the source of wc-l command. 13. Comparison of conditional judgment statement strings: = ,! =,-N: determines whether the string length is greater than 0. If it is greater than 0, it is true.-z: determines whether the string length is equal to 0. If it is equal to 0, it is true.
Number comparison:-eq equal,-ge greater than or equal to,-le less than or equal to,-ne not equal to,-gt greater than,-lt less
Logical judgment :! Not, &, | or
File judgment:-d directory judgment,-f file judgment,-r readable,-w writable,-x executable test condition 1 Comparison condition 2 such as: test 1-eq 1
[Condition 1 Comparison condition 2], for example, [1-eq 1], [-n ""]
['Who | wc-l'-le 10] & echo "YES" determines whether the number of logon users in the current system is less than or equal to 10. If YES, YES is output.

16. Loop statement:

If the while condition is true, run
Do
..
Done example: j = 1
While (j <= 10) or while [j-le 10]
Do
Echo "j = $ j"
J = 'expr $ j + 1
Done if statement:
If
Then
Else can also be nested between elif and fi.
Fi example: x = 4; y = 7if [$ x-eq $ y]
Then
Echo "equal"
Else
Echo "not equal"
Fi case variable in
Value 1) statement ;;
Value 2) statement ;;
*) Statement; # If the value is not in the range, run the following command: USER = whoamicase $ USER in.
Lishi)
Echo "you are LISHI ";
Echo "Welcome ";;
Root)
Echo "you are ROOT"
Echo "hi root ";;
Admin)
Echo "you are admin ";
Echo "admin, hello"; *) echo "the current user is not lishi, root, admin ";;
Esac for loop example: Use FOR loop to display the information of each file in the/tmp/sh directory. The value of variable I is for each file in the/tmp/sh directory, such as for I in "a" "B" "c". in this case, the value of variable I in each loop is a, B, cpath =/tmp/sh/
For I in 'ls $ Path'
Do
Ls-l $ I
Done example: # use a combination of for and if to display an even number from 1 to the number of inputs on the keyboard
# Note that if statements have double parentheses read x
For (I = 1; I <= $ x; I ++ ))
Do
If [$ I % 2 = 0]
Then
Echo "$ I"
Fi
Done14, function # defines an accumulated function sum, then input two numbers on the keyboard, and then call the sum function.
# Note: The function must be placed before calling the function sum ()
{
A = $ x
B = $ y
Total = 'expr $ a + $ B'
Echo "total = $ total"
} Echo "please enter two number :"
Read x
Read y
Sum $ x, $ y
Shell start:

Shell has bsh, bash, cash, etc.

1. When an administrator user logs on to linux, the prompt is: #. Generally, the prompt is: $
After logging in, exit or switch the user, use the exit command to exit normally.
2. Check the shell version in the current system and view it in the/etc/shell directory.
3. view the default shell versions of different users in the system. view the shell and echo shell of the current user in/etc/passwd.
4. directly use commands to change the shell environment of a user: The chsh system user name, and enter the new shell path as prompted, such as/bin/bash.
5. view the environment variables and ID of the current user, set | grep user, set | grep uid or, and view the/etc/passwd file.
6. Check the location of a command. For example, which ifconfig. Some commands in some common users cannot be found.
Or when the command is executed, it is generally because the path of the command is not added to the environment variable. Use the export command to set Environment Variables
7. Check the previous command history-c to clear the previous command
8. It is used in shell to differentiate the end of a command. Multiple commands are allowed in one line.
9. debug shell scripts with. script file name or bash script file name
10. There are three types of file permissions:
A. File owner: the user who creates the file
B. users in the same group: any user in the user group that owns the file
C. Other users: a user who does not belong to the user group that owns the file
For example,-rwxr-xr-x 1 root 217 08-10 test1.sh
The first character indicates the type of the file, whether it is a folder or a common file example-indicates a common file
The last nine characters are divided into three sections. The first section is the permission of the file owner.
The second part is the permissions of users in the same group, and the third part is the permissions of other users.
Grant permissions: g indicates users in the same group, and o indicates other users.
Chmod go + wx./test. sh grant write and execution permissions to users in the same group and other users.
Chmod u + wr./test. sh grant read and write permissions to yourself
Chmod o + wrx./test. sh grant read, write, and execution permissions to other users.
The same is true for permission assignment. You only need to replace "+" "-".
Chmod go-rw./B. c removes the read and write permissions of users in the same group and other users.

11. General permissions can also be represented by numbers: 4: read, 2: Write, 1: Execute,
If you use a number to grant permissions to a file, you need to write three numbers, for example, 764, which indicates
Users are read and write, users in the same group are read and write, and other users are read permissions.
12. When granting permissions to files and folders, the two do not interfere with each other unless the-R parameter is included in the folder authorization.
In this case, all the contents in the folder are granted the same permissions as those in the folder. Use-R with caution.
13. when viewing the folder permission, use ll-d/tmp/sh-d to view the folder. Otherwise, only
The contents of this folder are listed.

14. Change the owner of the file, chown oracle/tmp/sh/api. sh
Change the group to which the file belongs, chown: oracle/tmp/sh/api. sh
At the same time, change the owner and group of the file. For chown oracle: dba/tmp/sh/api. sh, the owner and group information of api. sh are as follows:
-Rwxrwxr-x 1 oracle dba 264 07-28/tmp/sh/api. sh
15. The id command is used to view the current user and the group's equivalent information.
16. groups: Check the number of groups in the system. The groups user name is groups oracle. view the group to which the user belongs.
17. getent group name: getent group dba, for example, to check which users in the dba group
18. Create a user and add it to the specified group. useradd wangcai-G root

19. When a script needs to be executed by the owner or group user, suid and guid must be used.
If suid or guid is set in the file, if the file does not have the execution permission, it is meaningless when suid or guid is set, and uppercase "S" is used"
. 4 represents suid, 2 represents guid
For example: start-orcl.sh, first use chown to change the user of the file, and then use chmod to change the suid and guid permissions of the file
-Rwxr-xr-x 1 root 632 08-15 start-orcl.sh
Chown oracle start-orcl.sh
-Rwxr-xr-x 1 oracle root 632 08-15 start-orcl.sh
Chmod 6751 start-orcl.sh
-Rwsr-s -- x 1 oracle root 632 08-15 start-orcl.sh

20. Execute a script su-oracle-c "/tmp/sh/start-orcl.sh" with the specified user identity"
Run the start-orcl.sh script as oracle
21. Create a shortcut ln-s/tmp/sh 1. Create a shortcut 1 pointing to/tmp/sh. Access 1 is equivalent to access/tmp/sh.
22. scheduled task:
Use service crond status to view the cron service status. If it is not started, service crond start starts it.
Basic usage:
Crontab-l
List the current crontab task
Crontab-d
Delete the current crontab task
Crontab-e (crontab-r above solaris5.8)
Edit a crontab task and ctrl_D ends.
Crontab filename
Crontab is in the format of hour, day, month, and week (separated by spaces ).
The entries in the crontab file are read from the left, the first column is the minute, and so on, and the last column is the command to be executed.
Each column is called a crontab field. In these fields, you can use-to connect a time range, for example, from Monday to Friday, which can be expressed as 1-5.
A single time point can be separated by commas (,). For example, Monday and Thursday can be expressed. If there is no special limit on a time domain, you can use the * sign. Each time entry contains five fields separated by spaces.
For example, if I want to run the cleanup. sh file under the bin directory at every night, the command should be:
30 21 ***/app/bin/cleanup. sh)
For example, if you want to run the backup. sh file at every month, the command should be:
00 00, 20 **/app/bin/backup. sh)
# Every two hours
0 */2 * date
Crontab-e and then edit the content in the opened file, such as: 0 */2 *** date. Save and exit.
You can also put the content of 0 */2 * date in a filename file, and then use crontab filename.
Add the content to crontab. Then, you can use crontab-l to view the content in the filename file.
Make the configuration file take effect: If the configuration file takes effect, you have to restart cron. Remember that since the cron configuration file under each user is modified.
You also need to restart the cron server,/etc/init. d/crond restart. Edit the/etc/crontab file and add a line at the end: 30 5 * root init 6 so that the system is configured to automatically restart at every morning.
You need to set crond as a service automatically started after the system starts. You can add
Service crond start
If you need to load other services in the system, you can continue to add the startup commands for other services.
For example, service mysqld start

Shell script learning Summary

1. Character truncation:

For normal path truncation, you can use the basename and dirname tools:
Basename can intercept a file name from a file path.
For example:
$ Basename/home/file.tar
File.tar
Dirname can be truncated from a file path to a directory path.
For example:
$ Dirname/home/file.tar
/Home
Do not use external tools for character Truncation
Bash has built-in functions to truncation variables. Generally, "#", "#", "%", "*" groups are used.
Integration. For example:

Copy codeThe Code is as follows: $ string = hellowbashshell
$ Echo $ {string # * sh}
Ell
$ Echo $ {string # * sh}
Shell
$ Echo $ {string % sh *}
Hellowba

----------------------- Page 2 -----------------------
$ Echo $ {string % sh *}
Hellowbash
"#" Indicates that it is removed from the starting part of the character. It is removed immediately after matching.
"##" Indicates to remove from the starting part of the character and search for the matching of the longest sum of the entire string.
"%" Indicates to remove from the end of a character. If it matches a public character, it is immediately removed.
"%" Indicates to remove from the end of the character and search for the longest match in the entire character.
"*" Unified character, usually associated with "#" or "#", is placed on the left of the search String, for example: $ {String # * sh} (on the left of sh
Edge). When it is used together with "%" or "%", it will be placed on the right side of the matching String, for example: $ {String % sh *}
Common tips:
Get the file name in the path: $ {path ##*/} (same as basename)
Path: $ {path %/*} (same as dirname)
File Extension: $ {path ##*.}
2. Receipt of Independent Variables
Receives parameters passed in from the command line. The first parameter is represented by $1, and the second parameter is represented by $2 ,... And so on.
Note: $0 indicates the script file name. In shell programming, "$ @" is often used to represent all parameters.
,. You can use a loop to traverse this parameter. If you use java for analogy, You can regard $ @ as a man.
Array defined in the function
3. if statement:
Format:
If [condition]
Then
Action
Fi
Note: space is required between "if" and "[". if you do not have space, shell reports a syntax error.
Incorrect. I was wasted on this for a while.
----------------------- Page 3 -----------------------
Conditon test type table
Operator Description Example
File comparison operator
-E filename if filename exists, it is true [-e/var/log/syslog]
-D filename if filename is a directory, it is true [-d/tmp/mydir]
-F filename if filename is a regular file, then [-f/usr/bin/grep]
True
-L filename if filename is a symbolic link, [-L/usr/bin/grep]
True
-R filename if filename is readable, it is true [-r/var/log/syslog]
-W filename if filename is writable, it is true [-w/var/mytmp.txt]
-X filename if filename is executable, it is true [-L/usr/bin/grep]
Filename1-nt if filename1 is more than filename2 [/tmp/install/etc/services-nt
New filename2, true/etc/services]
Filename1-ot if filename1 than filename2 [/boot/bzImage-ot
Old filename2, true arch/i386/boot/bzImage]
String comparison operator (please note the use of quotation marks, which is a good way to prevent space from disturbing the code)
-Z string if the string length is zero, it is true [-z "$ myvar"]
-N string if the string length is not zero, it is true [-n "$ myvar"]
String1 = string2 if string1 is the same as string2, ["$ myvar" = "one two three"]
True
String1! = String2 if string1 is different from string2, ["$ myvar "! = "One two three"]
True
Arithmetic comparison operator
Num1-eq num2 equals to [3-eq $ mynum]
Num1-ne num2 is not equal to [3-ne $ mynum]
Num1-lt num2 less than [3-lt $ mynum]
The num1-le num2 is less than or equal to [3-le $ mynum]
Num1-gt num2 greater than [3-gt $ mynum]
Num1-ge num2 is greater than or equal to [3-ge $ mynum]
It seems that if in bash is more intelligent than some other languages. In bash, test the existence and comparison of a file.
The two numbers are of the same size.
----------------------- Page 4 -----------------------
4. for statement
Bash statements are always so user-friendly and very similar to natural languages. In for statements
Iterate any data type similar to the Set (maybe this is not true, but I really cannot think of it better
).
Let's look at an example:
#! /Bin/bash
For args in $ @
Do
Echo $ args
Done
Save the preceding code entry as showargs. sh and set it to executable (chmod + x showargs. sh)
Run:
$./Showargs. sh arg1 arg2 arg3 arg4
Arg1
Arg2
Arg3
Arg4
In this example, we use "$ @", which represents all command line parameters. Use
Traverse it, and the system iteratively extracts the command line parameter from $ @ and puts it in args, and finally uses
Echo $ args for output.
For is more often used to traverse directories. The following example is used to list all files and files in the current directory.
Folder nameCopy codeThe Code is as follows: $ for file in *
> Do
> Echo $ file
> Done

Here, "*" represents the current directory and lists the names of all files and folders. Here, the folder
You cannot tell the difference between it and the file. if you need it, you should use if [-d $ {file}] for judgment.
----------------------- Page 5 -----------------------
For file traversal, you can add multiple expressions after "in. That is to say,
You can traverse multiple directories at a time.
The following code copies the files in the go folder and do folder in the current directory to the fo folder.
LowerCopy codeThe Code is as follows :#! /Bin/bash
For args in./go/*./do /*
Do
Cp $ {args}./fo
Echo "copying $ {args} to./fo/$ {args }"
Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.