3.Shell Input and output
3.1.echo
The echo command can display a line of text or a variable, or output a string to a file
echo [option] string
-E: Parsing escape characters
-N: Carriage return is not newline, Linux system default carriage return line
Escape character: \c,\f,\t,\n ...
Example Program
#!/bin/bash#echoecho -e " this Echo ' s 3 new lines\n\n\n echo ok " echoecho " this Echo ' s 3 new lines \n\n\n echo this log files has all been done Span style= "color: #800000;" > " >mylogfile.txt
3.2.read
read statements can read information from a keyboard or a line of text in a file and assign it to a variable
read varible1 varible2 ...
If only one variable is specified, read will assign all inputs to the variable until the first file terminator or carriage return is encountered If multiple variables are given, they are assigned different variables in order. The shell will use spaces as delimiters between variables
Example Programs
#!/bin/bash#readecho -n " first Name: read Firstnameecho -N sub name and Last name: " read subname lastnameecho -e " Span style= "color: #800000;" >your First Name is: ${firstname}\n echo -E your Sub Name is: ${subname}\n " echo -e " your last Name is: ${lastname}\n
3.3.cat
Cat is a simple and versatile command that you can use to display the contents of a file, create a file, and use it to display control characters
Cat [Options] filename1 ... filename2 ...
-V: Display control characters
Using the Cat command note that it does not stop at the file page break at 2; it displays the entire file. If you want to display one page at a time, you can use the more command or pass the output of the cat command through a pipeline to another command with paging capability (more, less)
3.4. Piping |
You can pass the output of one command to another command as input through a pipeline. Pipe with vertical bar |
Format: Command 1 | Command 2
Example: Ls-l | grep "MyFile"
3.5.tee
The tee command sends one copy of the output to the standard output, and the other copy to the corresponding file
tee-a files
If you want to see this output and also put it in a file, then this command is more appropriate.
Typically used after piping. Example: LS | Tee-a ls.txt Both output the command result of LS to standard output and output to the Ls.txt file.
3.6. Standard inputs, outputs, and errors
When you execute a command in the shell, each process is associated with three open files and uses file descriptors to refer to the files. Because the file descriptor is not easy to remember, the shell also gives the corresponding file name
Input file-Standard input: 0 (default is keyboard, can also be the output of files or other commands)
Output file--Standard output: 1 (the default is the screen, or it can be a file)
Error output file--standard error: 2 (the default is the screen, or it can be a file)
There are actually 12 file descriptors in the system, you can use the file descriptor arbitrarily 3~9
3.7. File redirection
Change the input source and output location of the program run
Command > FileName: redirect standard output to a new file
Command >> FileName: redirect standard output to a file (append)
Command 1> FileName: redirect standard output to a file
Command > FileName 2>&1: redirect standard output and standard error to a file
Command 2> FileName: Redirect standard error to a file
Command 2>> FileName: Redirect standard error to a file (append)
Command >> filename 2>&1: redirect standard output and standard error to a file (append)
Command < filename1 > Filename2:command commands with filename1 file as standard input, filename2 as standard output
Command < Filename:command commands with the filename file as standard input
Command << delimiter: read from standard input until delimiter decomposition is encountered
Command <&m: The file descriptor m as the standard input
Command >&m: Redirect standard output to file descriptor M
Command <&-: Turn off standard input
Example:
Cat file} sort > Sort.out sorts the string of file (per line) and outputs the sort results to the Sort.out file
>nullfile Create an empty file Nullfile
Sort < Name.txt > name.out the contents of name.txt as input, and after sequencing, outputs the results to Name.out
The cat file1 file2 1> file.txt 2> file.err outputs the standard output of the cat file1 file2 command to file.txt, outputting the output of the standard error of the command to File.err, separated output (f Ile1 exists, File2 does not exist)
3.8. Merge standard output and standard errors
When merging standard output and standard errors, remember that Shel is analyzing the corresponding command from left to right.
Example: grep "Example" Example.txt > Grep.out 2>&1 Find the contents of the "Example.txt" string in example, Standard output to grep.out file, and standard error output to grep.out file
3.9.exec
The EXEC command can be used to replace the current shell, in other words, the child shell is not started, and any existing environment will be cleared when using this command, and a shell should be restarted.
EXEC command: Where the command is usually a shell script
When you manipulate a file descriptor (and only at this time), it does not overwrite your current shell
Shell Programming Basics Tutorial 3--shell input and output