遍匈 / Shell縮殻 /

Shell補秘補竃嶷協??Shell Here Document?/dev/null猟周

<貧匯准 和匯准>
Unix 凋綜潮範貫炎彈補秘譜姥(stdin)資函補秘?繍潤惚補竃欺炎彈補竃譜姥(stdout)?塋勝R三稠蘓氐贈?袗縞簇詆莟絃擁納踣味?袗縞箜殺莟絃擁盃婉烹viii艦塋焼滇?

補竃嶷協?

凋綜議補竃音叙辛參頁?塋焼?vii洪敏垪榿殯弋鍔ň届魑塾勅顚u皹山椴ĺ箜沅惷35髻?

凋綜補竃嶷協?議囂隈葎?
$ command > file
宸劔?補竃欺?塋焼澶陳敞歉与敏埀志惷35魑塾勅顗?

箭泌?和中議凋綜壓?塋焼烔浪算畤患夙虜諒箜??
$ who > users
嬉蝕 users 猟周?辛參心欺和中議坪否?
$ cat users
oko tty01 Sep 12 07:30
ai tty15 Sep 12 13:32
ruth tty21 Sep 12 10:10
pat tty24 Sep 12 13:07
steve tty25 Sep 12 13:03
$
補竃嶷協?氏顕固猟周坪否?萩心和中議箭徨?
$ echo line 1 > users
$ cat users
line 1
$
泌惚音錬李猟周坪否瓜顕固?辛參聞喘 >> 弖紗欺猟周挑硫?箭泌?
$ echo line 2 >> users
$ cat users
line 1
line 2
$

補秘嶷協?

才補竃嶷協?匯劔?Unix 凋綜匆辛參貫猟周資函補秘?囂隈葎?
command < file
宸劔?云栖俶勣貫囚徒資函補秘議凋綜氏廬卞欺猟周響函坪否。

廣吭?補竃嶷協?頁寄噐催(>)?補秘嶷協?頁弌噐催(<)。

箭泌?柴麻 users 猟周嶄議佩方?辛參聞喘和中議凋綜?
$ wc -l users
2 users
$
匆辛參繍補秘嶷協?欺 users 猟周?
$ wc -l < users
2
$
廣吭?貧中曾倖箭徨議潤惚音揖?及匯倖箭徨?氏補竃猟周兆?及屈倖音氏?咀葎万叙叙岑祇貫炎彈補秘響函坪否。

嶷協?侮秘讐盾

匯違秤趨和?耽倖 Unix/Linux 凋綜塰佩扮脅氏嬉蝕眉倖猟周?
  • 炎彈補秘猟周(stdin)?stdin議猟周宙峰憲葎0?Unix殻會潮範貫stdin響函方象。
  • 炎彈補竃猟周(stdout)?stdout 議猟周宙峰憲葎1?Unix殻會潮範?stdout補竃方象。
  • 炎彈危列猟周(stderr)?stderr議猟周宙峰憲葎2?Unix殻會氏?stderr送嶄亟秘危列佚連。

潮範秤趨和?command > file 繍 stdout 嶷協?欺 file?command < file 繍stdin 嶷協?欺 file。

泌惚錬李 stderr 嶷協?欺 file?辛參宸劔亟?
$command 2 > file
泌惚錬李 stderr 弖紗欺 file 猟周挑硫?辛參宸劔亟?
$command 2 >> file
2 燕幣炎彈危列猟周(stderr)。

泌惚錬李繍 stdout 才 stderr 栽旺朔嶷協?欺 file?辛參宸劔亟?
$command > file 2>&1
$command >> file 2>&1
泌惚錬李斤 stdin 才 stdout 脅嶷協??辛參宸劔亟?
$command < file1 >file2
command 凋綜繍 stdin 嶷協?欺 file1?繍 stdout 嶷協?欺 file2。

畠何辛喘議嶷協?凋綜双燕
凋綜 傍苧
command > file 繍補竃嶷協?欺 file。
command < file 繍補秘嶷協?欺 file。
command >> file 繍補竃參弖紗議圭塀嶷協?欺 file。
n > file 繍猟周宙峰憲葎 n 議猟周嶷協?欺 file。
n >> file 繍猟周宙峰憲葎 n 議猟周參弖紗議圭塀嶷協?欺 file。
n >& m 繍補竃猟周 m 才 n 栽旺。
n <& m 繍補秘猟周 m 才 n 栽旺。
<< tag 繍蝕兵炎芝 tag 才潤崩炎芝 tag 岻寂議坪否恬葎補秘。

Here Document

Here Document 朕念短嗤由匯議鍬咎?宸戦壙咎葎”廼秘猟亀“。Here Document 頁 Shell 嶄議匯嶽蒙歩議嶷協?圭塀?万議児云議侘塀泌和?
command << delimiter
 document
delimiter
万議恬喘頁繍曾倖 delimiter 岻寂議坪否(document) 恬葎補秘勧弓公 command。

廣吭?
  • 潤硫議delimiter 匯協勣競鯉亟?念中音嬬嗤販採忖憲?朔中匆音嬬嗤販採忖憲?淫凄腎鯉才 tab 抹序。
  • 蝕兵議delimiter念朔議腎鯉氏瓜策待渠。

和中議箭徨?宥狛 wc -l 凋綜柴麻 document 議佩方?
$wc -l << EOF
 This is a simple lookup program
 for good (and bad) restaurants
 in Cape Town.
EOF
3
$
匆辛參 繍 Here Document 喘壓重云嶄?箭泌?
#!/bin/bash
cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
塰佩潤惚?
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

和中議重云宥狛 vi 園辞匂繍 document 隠贋欺 test.txt 猟周?
#!/bin/sh
filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
EndOfCommands
塰佩重云?
$ sh test.sh
Vim: Warning: Input is not from a terminal
$
嬉蝕 test.txt?辛參心欺和中議坪否?
$ cat test.txt
This file was created automatically from
a shell script
$

/dev/null 猟周

泌惚錬李峇佩蝶倖凋綜?徽嗽音錬李壓徳鳥貧?塋省箜欞畊菪d巴歓敏埆?補竃嶷協?欺 /dev/null?
$ command > /dev/null
/dev/null 頁匯倖蒙歩議猟周?亟秘欺万議坪否脅氏瓜卿虹?泌惚晦編貫乎猟周響函坪否?椎担焚担匆響音欺。徽頁 /dev/null 猟周掲械嗤喘?繍凋綜議補竃嶷協?欺万?氏軟欺”鋤峭補竃“議丼惚。

泌惚錬李徳盈 stdout 才 stderr?辛參宸劔亟?
$ command > /dev/null 2>&1
<貧匯准 和匯准>

AltStyle によって変換されたページ (->オリジナル) /