Input and output in the shell
Standard input: Keyboard
Standard output: Terminal display
The >> append is a newline append
Echo
- n do not trailing line breaks
-e enable escape function to interpret backslashes
-e disables the escape function that interprets backslashes (default)
--HELP Display this help message and exit
--version display version information and exit
Only with Cat
Figure 8
Cat<filename1>filename2
Inline input redirection
Command<<eof (identifier)
Data
EOF (identifier)
Redirecting input and output in scripts
Temporary redirection
Permanent redirection
1. Temporary redirection
command>& File Descriptor
[email protected] ~]# cat h.sh
#! /bin/bash
echo ' Test error ' >&2
echo ' normal output '
[Email protected] ~]# chmod u+x h.sh
[Email protected] ~]#./h.sh
Test error
Normal output
[Email protected] ~]#/h.sh 2>errlog
Normal output
[email protected] ~]# cat ErrLog
Test error
2. Permanent redirection
EXEC file descriptor > file name
[email protected] ~]# cat h.sh
#! /bin/bash
EXEC 1>output
echo ' Test error ' >&2
Echo ' OUTPUT1 '
echo "Output2"
[Email protected] ~]#./h.sh
Test error
[[email protected] ~]# cat output
Output1
Output2
h.sh Content
Figure 9
[Email protected] ~]#./h.sh
[[email protected] ~]# cat output
Output1
Output2
[email protected] ~]# cat Output_err
Test error
Test Error2
Input redirect
EXEC 0<filename
h1.sh Content
See Figure 10
[Email protected] ~]# chmod u+x h1.sh
[Email protected] ~]#./h1.sh
Line #1: Test error
Line #2: Test Error2
Pipeline
Redirect the output of one command to the input of another command
Command1|command2
2 commands are performed at the same time instead of executing one before continuing the next
Tee Output to File
[[Email protected] ~]# Date
Sunday, October 16, 2016 09:52:05 EDT
[Email protected] ~]# date|tee datefile
Sunday, October 16, 2016 09:52:17 EDT
[email protected] ~]# cat Datefile
Sunday, October 16, 2016 09:52:17 EDT
Variables in shell programming
User-defined variables
System Environment variables
by scope of action
Custom variables in the script
Environment variables
Local Environment variables
Global environment variables
Custom variable rules in shell scripts
Legal characters: Letters, numbers, underscores
Number of legal characters: <=20
Special note: Case sensitive
Variable declaration
Variable name = variable value such as Var=1
Type of variable value: Auto-assign number, string date, array
Call Mode: $ variable name such as $var
Scope: Within the lifetime of the script
' Can assign the result of the command execution to the variable
[[Email protected] ~]# Date
Sunday, October 16, 2016 10:10:02 EDT
[[email protected] ~]# mdate= ' Date '
[Email protected] ~]# echo $mdate
Sunday, October 16, 2016 10:10:23 EDT
Mdate= ' Date +%y-%m-%d '
Echo $mdate
2016-10-16
Input and output in the shell (Bash Shell 03)