一、說明
大多數 UNIX 系統命令從你的終端接受輸入并將所產生的輸出發送回??到您的終端。
一個命令通常從一個叫標準輸入的地方讀取輸入,默認情況下,這恰好是你的終端。
同樣,一個命令通常將其輸出寫入到標準輸出,默認情況下,這也是你的終端。
二、輸出重定向
重定向一般通過在命令間插入特定的符號來實現。
關于輸出重定向,沒有多少可說的,比較好理解,不是本文的重點。
如果不熟悉的讀者,可以參考:
shell 輸入/輸出重定向
http://www.runoob.com/linux/linux-shell-io-redirections.html
shell中 1>&2 2>&1 &> 重定向的含義和區別是什么?
http://www.lxweimin.com/p/41c304016185
三、輸入重定向
輸入重定向和輸出重定向一樣,Unix 命令也可以從文件獲取輸入。
1.關于 "<"?
語法:?
#? ?command? ?<? /path/to/file
注意:輸出重定向是大于號(>),輸入重定向是小于號(<),這樣,本來需要從鍵盤獲取輸入的命令會轉移到文件讀取內容,注意 "<"只能從文件中讀取輸入。
#? mysql? -u? root? ?-h 127.0.0.1? -p"MySQL@123"? ?<? dump.sql?
#? echo "123456"? > passwd.txt
# passwd? --stdin root? < passwd.txt
2.? 關于 "<<"
"<<" 在BASH文檔中,稱之為 "Here Documents"。
Here Documents 是 shell 中的一種特殊的重定向方式,用來將輸入重定向到一個交互式 Shell 腳本或程序。
將兩個 delimiter(此處是OF,end of file 之意) 之間的內容(document) 作為輸入傳遞給 command。
如果想將輸入保存到文件文件中,可以結合輸出重定向:
#? cat? ? << EOF? ? ?>>? /etc/profile
export http_proxy=http://127.0.0.1:8123
export https_proxy=http://127.0.0.1:8123
export ftp_proxy=http://127.0.0.1:8123?
EOF? ?
3. 關于 "<<<"
"<<<" 在BASH文檔中,稱之為 "Here Strings"。
Here String是Here Documents 的一個變種。
它由操作符"<<<"和作為標準輸入的字符串構成,here-string是一個用于輸入重定向的普通字符串。
語法:
#? command <<<? "WORD"
注意:單個單詞不需要引號引用,中間如果有空格的字符串,則需要引號引用起來。
# echo "123456" | passwd? --stdin? root
# passwd --stdin root? <<< "123456"
# mysql -u root? ?-e? ? ? "select user,host from mysql.user;"
# mysql -u root? ?<<<? ? "select user,host from mysql.user;"
# while read line ; do echo $line; done <<< "111 222 333"
4. 關于 "<? ?<"
應該說??"<? ?<" 并不是一個單獨的輸入重定向符號,兩個< 至少有一個空格,它是兩個符號的組合:
左邊 < 代表左邊接受從右邊輸入,右邊 <(command) 代表右邊shell(子shell)命令的輸出,將輸出輸出到左邊。
語法:
#? ?command? <? ? ?<(command)
注意:右邊的<和(之間不能有空格。
# while read var; do unset $var; done < <(env | grep -i proxy | awk -F= '{print $1}')
# passwd --stdin root <? ? <(echo "123456")
# mysql -u root < <(echo "select user,host from mysql.user;")
四、參考
Shell 輸入/輸出重定向
http://www.runoob.com/linux/linux-shell-io-redirections.html
shell中 1>&2 2>&1 &> 重定向的含義和區別是什么?
http://www.lxweimin.com/p/41c304016185
Linux 輸入輸出(I/O)重定向
https://www.cnblogs.com/divent/p/5773861.html
shell重定向
https://blog.csdn.net/fsx2550553488/article/details/80994277
linux shell輸入輸出重定向
https://www.xuebuyuan.com/745014.html
shell中while循環的陷阱
https://www.cnblogs.com/f-ck-need-u/p/7431578.html
What is the meaning of? < and << in UNIX /Linux?
https://unix.stackexchange.com/questions/114514/and-in-unix-linux
Correct textual name for <<
https://unix.stackexchange.com/questions/2888/correct-textual-name-for
Here Documents
https://en.wikipedia.org/wiki/Here_document
Here Strings
https://bash.cyberciti.biz/guide/Here_strings
What does "<<<" mean in linux shell script?
https://www.quora.com/What-does-mean-in-linux-shell-script
What does <<< mean?
https://unix.stackexchange.com/questions/80362/what-does-mean
How to execute multiple multiline mysql queries with a shell script?
https://superuser.com/questions/811973/how-to-execute-multiple-multiline-mysql-queries-with-a-shell-script
What are the shell's control and redirection operators?
https://my.oschina.net/u/553266/blog/2935635
https://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators
https://www.gnu.org/software/bash/manual/bashref.html#Redirections
https://unix.stackexchange.com/questions/114514/and-in-unix-linux
https://github.com/qinjx/30min_guides
https://stackoverflow.com/questions/1602904/how-do-you-run-a-single-query-through-mysql-from-the-command-line