Shell tip command (z)

Source: Internet
Author: User
Tags echo command

Http://hi.baidu.com/zxvb3000/blog/item/9ecd2b022d15490b4bfb5199.html

1. Test command
The test command is used to check whether a condition is true. It can be used to test values, characters, and files,
The test characters and corresponding functions are as follows:
(1) numerical test:
-EQ: true if it is equal
-Ne: true if not equal
-GT: true if the value is greater
-Ge: true if the value is greater than or equal
-Lt: true if the value is smaller
-Le: true if the value is less than or equal
(2) string test:
=: Equal to true
! =: True if not equal
-Z string: the pseudo-String Length is true.
-N string: the string length is true if it is not pseudo.
(3) file test:
-E file name: true if the file exists
-R file name: true if the file exists and is readable
-W file name: true if the file exists and can be written.
-X file name: true if the file exists and can be executed
-S file name: true if the file exists and contains at least one character
-D file name: true if the file exists and is a directory
-F file name: true if the file exists and is a normal file
-C file name: true if the file exists and is a special character file
-B file name: true if the file exists and is a special file.
In addition, Linux also provides , Or ("-O), non ("-a ") three logical operators are used to connect test conditions,
The priority is "!". Highest, followed by "-a", and "-o.
Bash can also perform simple arithmetic operations in the following format:
$ [Expression]
Example: var1 = 2
Var2 = $[var1*10 + 1]
Then, the value of var2 is 21.

2. If Condition Statement
If [-x/sbin/quotaon]; then
Echo "turning on quota for root filesystem"
/Sbin/quotaon/
Elif [-x/sbin/quotaon]; then
/Usr/bin/bash
Else
Echo "OK"
Fi

3. For Loop
#! /Bin/sh
WORD = "a B c d e f g h I j l m n o p q r s t u v w x y z"
For I in $ word; do
Echo $ I
Done

#! /Bin/sh
Files = 'ls/TXT/*. txt'
For TXT in $ files; do
Doc = 'echo $ TXT | sed "s/. txt/. DOC /"'
MV $ TXT $ Doc
Done

4. While and until Loops
#! /Bin/sh
While [-F/var/run/pp1_pid]; do
Killall pppd
Done

#! /Bin/sh
Until [-F/var/run/pp1_pid]; do
Sleep 1
Done

Shell also provides two commands, true and false, to create an infinite loop structure,
Their return statuses are both 0 in total or 0 in total

5. Select case conditions
#! /Bin/sh
Case $1 in
Start | begin)
Echo "start something"
;;
Stop | end)
Echo "stop something"
;;
*)
Echo "ignorant"
;;
Esac
The case expression can also use shell wildcards ("*", "?" , "[]").

6. Unconditional control statements break and continue
Break is used to terminate the execution of the current loop immediately, while contiune is used to stop the statements following the loop.
And immediately start the execution of the next loop. These two statements are valid only when they are placed between do and done.

7. Function Definition
You can also define functions in shell. A function is actually composed of several shell commands,
Therefore, it is similar to a shell program. The difference is that it is not a separate process,
It is part of the shell program. The basic format of the function definition is:
Functionname
{
Several command lines
}
The format of the called function is:
Functionname param1 param2 ......
Shell functions can complete some routine work and have their own exit status,
Therefore, functions can also be used as conditions for the IF, while, and other control structures.
Parameter descriptions are not required when defining a function, but parameters can be included when calling a function.
Shell will assign these parameters to the corresponding location parameters $1, $2,... and $ * respectively *.

8. Command Group
There are two command grouping methods in shell: "()" and "{}". The former is used when shell executes ()
Create a new sub-process, and then execute the commands in the circular arc.
When you execute a command, you do not want the command to run the status set (such as location parameters, environment variables,
When the current working directory, etc.) changes affect the execution of the following statements, these commands should be placed in the circle
In the arc, this ensures that all changes only affect the child process, and the parent process is not disturbed;
{} Is used to use the output results of the commands executed in sequence for the input (pipe mode) of another command ). When we
If you want to use the circular arc and the curly arc (for example, to calculate the priority of an expression), you need to add
Operators (/) to let Shell know that they are not used for command execution control.

9. Signal
The trap command is used to capture signals in the shell program. There are three methods to respond to these signals:
(1) execute a program to process this signal.
(2) default operation for receiving signals
(3) Ignore this signal
Trap provides three basic forms for the above three methods:
When shell receives signals with the same values in the signal list,
The command string in double quotation marks will be executed.
Trap 'commands' signal-list
Trap "commands" signal-list
To restore the default signal, use the second form of trap command:
Trap signal-list
The third form of trap command allows ignoring signals:
Trap "" signal-list
Note:
(1) The signal 11 (segment violation) cannot be captured, because the shell itself needs to capture the signal for memory dump.
(2) In the trap, you can define the processing of the signal 0 (actually there is no such signal), and the shell program terminates it.
(Such as executing the exit Statement.
(3) After capturing the signal specified in the signal-list and executing the corresponding commands, if these commands do not
If the shell program is terminated, the shell program will continue to execute the command following the Command executed when receiving the signal.
The shell program cannot be terminated easily.
In TRAP statements, single quotes and double quotation marks are different. When a shell program encounters a trap statement for the first time,
Scan the commands in commands. If commands is enclosed in single quotes, Shell
The variables and commands in commands are not replaced. Otherwise, the variables and commands in commands are
.

10. How to run the shell program
There are three methods to execute a shell program:
(1) sh shell program file name
Format:
BASH Shell program file name
This is actually to call a new bash command to explain the program, and pass the shell program file name as a parameter to it.
The newly started shell will read the specified file and execute the commands listed in the file. When all the commands are completed.
The advantage of this method is that the shell debugging function can be used.
(2) Sh <shell program file name
Format:
Bash <shell program file name
In this way, the shell command interpreter uses the specified program file to redirect input.
(3) Use the CHMOD command to make the shell program executable

11. Bash program debugging
Bash-select the shell program file name
Several common options are:
-E: Exit immediately if a command fails.
-N: reads commands but does not execute them.
-U: During replacement, unconfigured variables are considered as errors.
-V: displays shell input rows.
-X: displays commands and their parameters when executing commands.
All the options above can also be referenced in the shell program in the form of "Set-select item", while "set + select item"
This option is disabled. If you only want to use some selection items for a part of the program, you can use
The preceding two statements are surrounded.
1. unset variable exit and exit now
The unset variable exit feature allows you to check all variables. If an unassigned variable is referenced, the shell is terminated.
Program execution. Shell usually allows the use of unspecified variables. In this case, the variable value is null. If not
Set the variable to exit the selection item. Once the unspecified variable is used, an error message is displayed and the program is terminated. Unset variable return
The "-U" option is selected ".
When a shell is running, if a command does not exist or cannot be executed, the redirection fails, or the command ends abnormally, such
If this error message is not redirected again, it is printed on the terminal screen and the shell program continues to run. To send an error
Force the shell program to end immediately at the time of creation. You can use the "-e" option to terminate the execution of the shell program immediately.
2. shell program tracking
The main method to debug a shell program is to use shell commands to explain the program's "-V" or "-X" options to track program execution. "-V"
Select items so that the shell will display every command line it reads during the execution of the program, and the "-X" option will make the shell
In the process of executing a program, each command executed by the program is displayed with a "+" and a command name at the beginning of the line. And put every variable
The value obtained from the variable is also displayed. Therefore, the main difference is that the output is printed if "-V" is not specified before the command line is executed.
The original content of the command line, while "-V" prints the content of the replaced command line.
In addition to the "-V" and "-X" Options of shell, you can also take some auxiliary debugging measures within the shell program.
For example, the echo command can be used in some key aspects of the shell program to display the necessary information, which is equivalent to the C language.
In this way, you can know where the program runs and the current state of the program.

12. Bash Internal commands
The bash command interpreter contains some internal commands. Internal commands are invisible in the directory list. They are provided by the shell itself.
Common internal Commands include echo, Eval, exec, export, readonly, read, shift, wait, and point (.).
The following describes the Command Format and functions.
1. Echo
Command Format: Echo ARG
Function: print the string specified by Arg on the screen.
2. Eval
Command Format: eval ARGs
Function: When the shell program runs the eval statement, the shell reads The args parameter and combines them into a new command. Then
Run.
3. Exec
Command Format: EXEC command Command Parameters
Function: When the shell executes the exec statement, it does not create a new sub-process, but instead executes the specified command,
When the specified command is executed, the process, that is, the initial shell, is terminated, so the Exec
The subsequent statements will not be executed.
4. Export
Command Format: Export variable name or: Export variable name = variable value
Function: shell can use export to bring its variables down to the sub-shell so that the sub-process inherits the environment variables in the parent process.
However, the sub-shell cannot use export to bring its variables up to the parent shell.
Note: The export statement without any variable name will display all the current export variables.
5. readonly
Command Format: readonly variable name
Function: identifies a user-defined shell variable as unchangeable. The readonly command without any parameters is displayed.
All read-only shell variables.
6. Read
Command Format:
Read variable table
Function: Read a row from a standard input device, break it into several words, and assign values to variables defined in the shell program.
7. Shift statement
Function: The Shift statement renames all location parameter variables as follows: $2 becomes $1, $3 becomes $2 ...... In the program
Every shift statement is used, all the location parameters are moved to the left one by one, and the location parameter "$ #" is reduced by one,
Until it is reduced to 0.
8. Wait
Function: it refers to the completion of all sub-processes that the shell waits for to start in the background. The return value of wait is always true.
9. Exit
Function: exit the shell program. You can specify a number as the return status after exit.
10. "." (point)
Command Format:. shell program file name
Function: enables shell to read the specified shell program file and execute all statements in the file in sequence.

13. Special parameters:
1. $ *: represents all parameters. The interval is the first character of the parameter set in IFS.
2. $ @: similar to * asterisks. The difference lies in the absence of reference to IFS
3. $ #: number of parameters
4. $? : Execution of the return value of the previous command
5. $-: Option parameter of the recently executed foreground Pipeline
6. $: process ID
7. $! : Execute the PID of the previous background command
8. $ _: display the last executed command

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.