一、說明
大多數(shù) UNIX 系統(tǒng)命令從你的終端接受輸入并將所產(chǎn)生的輸出發(fā)送回??到您的終端。
一個(gè)命令通常從一個(gè)叫標(biāo)準(zhǔn)輸入的地方讀取輸入,默認(rèn)情況下,這恰好是你的終端。
同樣,一個(gè)命令通常將其輸出寫入到標(biāo)準(zhǔn)輸出,默認(rèn)情況下,這也是你的終端。
二、輸出重定向
重定向一般通過在命令間插入特定的符號(hào)來實(shí)現(xiàn)。
關(guān)于輸出重定向,沒有多少可說的,比較好理解,不是本文的重點(diǎn)。
如果不熟悉的讀者,可以參考:
shell 輸入/輸出重定向
http://www.runoob.com/linux/linux-shell-io-redirections.html
shell中 1>&2 2>&1 &> 重定向的含義和區(qū)別是什么?
http://www.lxweimin.com/p/41c304016185
三、輸入重定向
輸入重定向和輸出重定向一樣,Unix 命令也可以從文件獲取輸入。
1.關(guān)于 "<"?
語法:?
#? ?command? ?<? /path/to/file
注意:輸出重定向是大于號(hào)(>),輸入重定向是小于號(hào)(<),這樣,本來需要從鍵盤獲取輸入的命令會(huì)轉(zhuǎn)移到文件讀取內(nèi)容,注意 "<"只能從文件中讀取輸入。
#? mysql? -u? root? ?-h 127.0.0.1? -p"MySQL@123"? ?<? dump.sql?
#? echo "123456"? > passwd.txt
# passwd? --stdin root? < passwd.txt
2.? 關(guān)于 "<<"
"<<" 在BASH文檔中,稱之為 "Here Documents"。
Here Documents 是 shell 中的一種特殊的重定向方式,用來將輸入重定向到一個(gè)交互式 Shell 腳本或程序。
將兩個(gè) delimiter(此處是OF,end of file 之意) 之間的內(nèi)容(document) 作為輸入傳遞給 command。
如果想將輸入保存到文件文件中,可以結(jié)合輸出重定向:
#? 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. 關(guān)于 "<<<"
"<<<" 在BASH文檔中,稱之為 "Here Strings"。
Here String是Here Documents 的一個(gè)變種。
它由操作符"<<<"和作為標(biāo)準(zhǔn)輸入的字符串構(gòu)成,here-string是一個(gè)用于輸入重定向的普通字符串。
語法:
#? command <<<? "WORD"
注意:?jiǎn)蝹€(gè)單詞不需要引號(hào)引用,中間如果有空格的字符串,則需要引號(hào)引用起來。
# 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. 關(guān)于 "<? ?<"
應(yīng)該說??"<? ?<" 并不是一個(gè)單獨(dú)的輸入重定向符號(hào),兩個(gè)< 至少有一個(gè)空格,它是兩個(gè)符號(hào)的組合:
左邊 < 代表左邊接受從右邊輸入,右邊 <(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 &> 重定向的含義和區(qū)別是什么?
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循環(huán)的陷阱
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