1, numeric and numeric expressions
AWK does not provide a string-to-number function, but awk is simple: Add a 0 to the string, such as S = "123", then N = 0 + S, and assign the number 123 to N.
2. How to print the first few lines
Head-n n filename
# head-n 3 passwd
Head-n filename
# head-3 passwd
awk ' FNR <= n ' filename
# awk ' FNR <= 3 ' passwd
SED-E NQ FileName
# sed-e 3q passwd
Sed NQ
# sed 3q passwd
3, limited temporary files only we can access
Umask 077 filename
Mask: The default permissions for new files and directories are determined by mask
Umask: Affects the permissions that Mask,umask represents to be lost
Represents the rwx------, which indicates that owner has no permissions and that group and other do not have full operational permissions
4. Regular
# echo "Jones:adrian W. jones/osd211/555-0123" | Sed-e ' s=^\ ([^:]*\): [^/]*/\ ([^/]*\]/.*$=\1:\2= ')
jones:osd211
^\ ([^:]*\) matching user name Jones
[^/]*/matches Adrian W. Jones
\ ([^/]*\) match OSD211
. * Match $555-0123
\1:\2 matches the two groups enclosed in front ().
# echo "Jones:adrian W. jones/osd211/555-0123" | Sed-e ' s=^\ ([^:]*\): [^/]*/[^/]*/\ ([^/]*\] =\1:\2= ')
Jones:555-0123
5. Variable assignment and environment
5.1, ReadOnly and Export
Two similar commands provide management of face-changing, one is readonly, it can make variable read-only mode, and assignment to them is forbidden. In shell programs, this is a good way to create symbolic constants.
hour_per_day=24 Assignment value
ReadOnly Hour_per_day is set to read-only mode
Export,readonly
Use:
Export is used to modify or print environment variables, and readonly makes variables non-modifiable.
The more common is export, which is used to put variables into the environment (environment). An environment is a simple list of names and values. Can be used by all executing programs. The new process inherits the environment from its parent process, or it can modify it before establishing a new child process. The Export command can add new variables to the environment:
Path= $PATH:/usr/local/bin update PATH
Export PATH Exports it
Simplification can also be written like this:
Export path= $PATH:/usr/local/bin
Export-p shows the current environment
The export command only adds variables to the environment, and if you want to remove variables from the program's environment variables, use the ENV command, which can also temporarily change the value of the variable:
Env-i path= $PATH home= $HOME lc_all=c awk ' ... ' file1 file2
The-i option is used to initialize environment variables, discarding any inherited values, passing only the variables specified on the command line to the program's use.
5.2, unset
The unset command removes variables and functions from the executing shell. By default, it either unlocks the variable setting or it can be done with-V:
unset full_name Delete variable full_name
Unset-v First Middle Delete other variables
Use-f to delete a function
Who_is_on () { define function
W.H.O. | awk ' {print '} ' | Sort-u Generating a sorted list of users
}
...
Unset-f who_is_on Delete function
5.3. Assignment is null and delete variable
The myvar= assignment does not delete the MyVar, except to set it to a null string. Relative: unset MyVar will completely delete it.
5.4. Parameter expansion
Parameter unwinding (parameter expansion) is the procedure that the shell uses to provide variable values in a program.
Under the shell, more complex forms can be used in more specific situations. These are the forms that enclose the variable name in curly braces (${variable}), and then add extra syntax to tell the shell what to do. Curly braces are used when you need to immediately follow a character that might be interpreted as part of a name after the variable name:
Reminder= "Time to go to the dentist!" Store values in reminder
Sleep for two minutes
echo _${reminder}_ with underlined symbols to emphasize the reality of the information
Warning:
By default, undefined variables are expanded to a null (empty) string. Random writing of programs can lead to disaster:
rm-rf/$MYPROGRAM If Mtprogram is not set, it can lead to catastrophe!
So write the program must be very careful!
6. Arithmetic arithmetic
i++ and ++i
# i=5
# echo $ ((i++)) $i
5 6
# echo $ ((++i)) $i
7 7
How can there be different results? Is the value of I plus 1, but the value returned by the operator depends on its relative position with the variable. The Postfix (postfix) operator (operator appears after the variable), returns the old value to the variable after the result has been generated, and then executes the variable plus 1 operation. In contrast, in the prefix (prefix), the operand is placed in front of the variable, the variable is added 1, and then return the new value to the variable,--the working method and + + is similar, but its operation is to reduce the variable by 1.
7. Exit status
Each command exit returns a small integer value to the program that refers to it, which is the well-known exit status (Exit Statu).
7.1. Exit Status value
An exit status of 0 means "success", and any other exit status is unsuccessful.
Built-in variables? (with $? access to it) includes the exit status of a program that was last executed by the shell.
7.2. POSIX End Status
Value meaning
0 Successful exit of the command
>0 failure during redirection or Word expansion (~, variables, commands, arithmetic expansion, and word cutting)
The 1-125 command did not exit successfully. A specific exit is worth meaning and is defined by individual commands.
The 126 command could not be found, but the file could not be executed.
127 Command not found
>128 Command died from a signal.
Excerpt from the Shell Scripting Learning Guide---1