Shell (6) input, output, file, and Command Execution
Sequence: describes how to process input, output, and generated file names in different ways in shell language, followed by command replacement and command line parameters. References. Focus on the command line, discuss various references provided by shell, and finally the command execution sequence, and introduce the commands built in shell. Standard input, standard output, and standard error output: Execution Process: Read standard input, write standard output, and pass error information to standard error output. We call it a filter. Because they filter data streams, every one will execute some operation on the data stream, and then pass it to the next one through the pipeline. Read rows. Syntax: read [-r] variable... Purpose: read information into one or more shell variables. Main options:-r: original read, no processing, and the backslash at the end of the line is not interpreted as a continue character. Behavior mode: after reading a row from a standard input, use the shell field cutting function (using $ IFS) to split the row. The first word is assigned to the first variable. The second word is assigned the second variable, and so on. If the word is more than the variable, all the remaining words are assigned the last variable of the souvenir. When a read volume encounters an end-of-file (end-of-file), it will exit with a failed value. If the input line ends with a backslash, the read will discard the backslash and line feed characters and continue to read the next line of data, if you use the-r option, read reads the last backslash literally. Read can read all values into multiple variables at a time. In this case, the characters in $ IFS will separate the data in the input row and make it an independent word. Example:
------------------------ [Root @ localhost test] # cat readprintf "Enter name, rank, serial number:" read name rank sernoecho $ nameecho $ rankecho $ serno [root @ localhost test] #. /readEnter name, rank, serial number: cwtea 100 1101 # In space mode cwtea1001101 ------------------------
Redirection: <,>, >>, | additional redirection operator set-c explanation: Posix Shell provides the option to prevent file meaning truncation: execute the set-c command to enable the "noclobber" option called by shell. When it is in the open state, simply> redirect the target file already exists,> | The operator invalidates the noclobber option. <And <-format: program <delimiter can provide input data in the Shell script body. By default, shell can replace variables, commands, and arithmetic values in the embedded file body. If the delimiters are enclosed in any form of quotation marks, shell will not process the input text:
------------------------------------------------[root@bogon test]# cat dei=5cat << 'E'OFthis is i: $iHere is $(a,b)EOFi=5echo $i[root@bogon test]# sh dethis is i: $iHere is $(a,b)5----------------------------------------
The second form is to end with a negative sign. All the tabs starting with the sign are deleted from the embedded file and the ending separator before the souvenir program is passed as the input. Use <> open a file as input and output. Use program <> file to read and write data. By default, file is opened on the standard input. <read-only open> is opened only by writing. Exec Syntax: exc [program [arguments...] usage: replace shell with a new program, or change the I/O settings of the shell itself. Behavior: Replace the shell with the specified program to pass the parameter to it. If only the I/O rewrite direction is used, the file descriptor of the shell is changed. Printf Syntax: printf format [string...] purpose: To generate output from Shell scripts, because printf is defined by the posix standard, printf scripts are more portable than echo. Action: printf uses the format string to control the output. All pure characters in the string are printed truthfully, and the echo escape sequence is interpreted. Including % and a letter format indicator, used to display the formatting of the corresponding parameter string.
--------------------------- [Root @ bogon oldboy] # printf "a string, with processing: <% B> \ n" "A \ nB" a string, with processing: <AB> [root @ bogon oldboy] # -------------------------------
According to Posix standards, the floating point formats % e, $ E, % f, % g, and % G are "not required to be supported ". This is because awk supports floating-point arithmetic operations and has its own printf statement. In this way, when the shell program needs to format the floating point value, it can be implemented using a small awk program. However, the printf commands built in bash, ksh93, and zsh both support the floating point format. The printf command can be used to specify the width and alignment of output fields. To achieve this purpose, the format expression following % can use three optional modifiers and front-end format specifiers. % Flags width. precision format-specifier the width of the output field is a numeric value. When the field width is specified, the content of the field is aligned to the right by default, and to the left, the-sign must be specified. ----------------------------------- [Root @ bogon cwtea] # printf "| %-20s | \ n" hello | [root @ bogon cwtea] # printf "| % 20s | \ n" hello | hello | ------------------------------------- ---------------------------- [root @ bogon cwtea] # printf "%. 10o/n "1230000000173 [root @ bogon cwtea] # printf" %. 10s \ n "" abcjaldd ddfds kfakdfal "abcjaldd d [root @ bogon cwtea] # printf" %. 2f \ n "1234.55281234.55 --------------------- -------- ---------------------------- % D, I, o, and u are all numeric types. We can understand the comparison tables one by one.
-----------------------------[root@bogon cwtea]# width=5 prec=5 myvar=43.123456[root@bogon cwtea]# printf "|%${width}.${prec}G|\n" $myvar|43.123|[root@bogon cwtea]# printf "|%5.5G|\n" 42.123456|42.123|----------------------------------------------------------[root@bogon cwtea]# printf "%s %#s\n" 15 1515 15[root@bogon cwtea]# printf "%05d\n" 1500015[root@bogon cwtea]# printf "|%10s|\n|%10s|\n" hello world| hello|| world|-----------------------------
The wave number expansion and the wildcard shell have two types of expansion related to the file name. The first is the wave number expansion, and the other is called the wildcard expansion, some people call it global expansion or path expansion. Wave Number expansion: if the first character of the command line string is a wave number (~), Or the first character after any colon that is not enclosed by quotation marks in the value specified by the variable is the Tilde (~) The shell will execute the wave number expansion. # Cat ~ /. Profile shell will ~ Change to $ HOME, that is, the root directory of the current user. # Cat ~ Cwtea/. profile shell searches for the user cwtea in the password database of the system, and then ~ Change cwtea to the root directory of cwtea. Note: Some commercial unix systems do not support wave numbers. Basic wildcard :? Any single character * any character string [set] any character in the set [! Set] any character not in the set ------------------------- use the wildcard [abc] a, B, c [.,;] of the set Structure a comma or semicolon [-_] periods or underscores [a-c] a to c include a, B, c [a-z] Any lowercase letter [! 0-9] any non-digit [0-9!] Any digit or exclamation point [a-zA-Z] Any lowercase or upper case letter [a-zA-Z0-9 _-] Any lowercase upper case Data Dash and underline accumulation process. You can understand it. Note: The first file named. is a hidden file. Command replacement: refers to the result after the shell executes the command and replaces the command with the command. There are two methods to replace commands. The first method is to use backquotes and the second method is to use the accent symbol ('...') to box the command to be executed. [Root @ bogon ~] # Cat 'ls ~ /Yes 'use a function in the korn shell for fear of confusion and difficulty reading $ (...) [root @ bogon ~] # Cat $ (ls ~ /Yes) Reference: backslash escape: \ single quotes ('...') ----------------------------- [root @ bogon ~] # Echo * 314494.htm 314590.htm 51 51cto 51cto.tar.gz 51 test a anaconda-ks.cfg cwtea Desktop down ~ Install. log install. log. syslog oldboy test title.htm yes yes.htm [root @ bogon ~] # Echo '* ------------------------------- Note: single quotes in single quotes do not have any special meaning. Use double quotation marks in combination if necessary. Figure [shell execution process] a = "ls | more" $ a shell regards | and more as ls parameters, rather than directly generating a page-based file list. This is because when the shell executes the variable, the pipe character appears in step 5, that is, after it does find the pipe character, the variable expansion will not be parsed until step 8, shell regards | and more as ls parameters, so that ls will try to find a file named | and more in the current directory! -------------------- [Root @ bogon x] # a = "ls | more" [root @ bogon x] # $ als: |: No file or directory ls: more: there is no file or directory -------------------------- [root @ bogon x] # eval $ af1f2out ----------------------------- when the shell reaches the last step, it will execute the parameter eval with ls | more, this causes the shell to return to step 1. [Figure shell built-in command] command syntax: command [-p] program [arguments...] purpose: when searching for the command to be executed, in order to avoid the shell containing functions, this allows the function to access the built-in version of the command with the same name as the built-in command. Main options: use the default value of $ PATH when you use-p to find the system tool. Conclusion: The read command reads the row and splits the data into various fields, which are assigned to the specified shell variable and used with the-r option to control how the data is read. I/O redirection allows you to change the source and destination of a program, or execute multiple programs together in subshell or code block. In addition to redirecting to files and from files, pipelines can also be used to connect multiple programs. embedded files provide in-row input. The processing of file descriptors is a basic operation. In particular, file descriptors 1 and 2 are repeatedly used in routine script writing. Printf is a deep and flexible command, but it is a bit complicated. It is used to generate output. Most of the time, it can be used in a simple way, but in fact, its strength is sometimes necessary and has profound value. Shell will execute a lot of expansion or replacement on the text of each command line, wave number expansion and wildcards, variable expansion, arithmetic expansion and command replacement. $ () Is a relatively new and well-written form. References are used to protect different source code components from special processing by shell. \ ''" "is used in combination. The eval command is used to replace the General Command Line replacement and execution sequence, so that shell scripts can dynamically build commands. Built-in commands exist because they need to change the internal state of the shell and must be built-in. Some are for efficiency.