免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
shell基礎(chǔ)五:輸入和輸出(echo,read,cat,管道,tee,重定向等) Shell編程 卓越資源 - 專業(yè)Linux/Unix資料站

shell基礎(chǔ)五:輸入和輸出(echo,read,cat,管道,tee,重定向等)

下面的所有環(huán)境都在在REDHAT LINUX9下試驗的
在LINUX中,要使轉(zhuǎn)義符生效,需加參數(shù)-e


從echo的變量開始說起
如:e c h o命令輸出轉(zhuǎn)義符以及變量。

                                                                     
# echo -e "\007your home is $HOME , you are connected on `tty`"
your home is /root , you are connected on /dev/pts/1
# echo -e "\ayour home is $HOME , you are connected on `tty`"
your home is /root , you are connected on /dev/pts/1
#


 
本例中
\007或\a你可以讓終端鈴響一聲
顯示出$ H O M E目錄,
并且可以讓系統(tǒng)執(zhí)行t t y命令(注意,該命令用鍵盤左上角的符號,法語中的抑音符引起來,不是單引號 )。
在e c h o命令輸出之后附加換行,可以使用\ n選項:

                                                                     
$ cat echod
#!/bin/sh
echo -e "this echo's 3 new lines\n\n\n"
echo "OK"
編輯一個新echod,如上內(nèi)容,然后運行輸出如下:

                                                                     
$ ./echod
this echo's 3 new lines



OK
$
在e c h o語句中使用跳格符,記住別忘了加反斜杠\:

                                                                     
$ echo -e "here is a tab\there are two tabs\t\tok"
here is a tab   here are two tabs               ok
$
把一個字符串輸出到文件中,使用重定向符號>。
在下面的例子中一個字符串被重定向到一個名為m y f i l e的文件中:


                                                                     
$ echo "The log files have all been done"> myfile
或者可以追加到一個文件的末尾,這意味著不覆蓋原有的內(nèi)容:

                                                                     
$ echo "$LOGNAME carried them out at `date`">>myfile
現(xiàn)在讓我們看一下m y f i l e文件中的內(nèi)容:

 
The log files have all been done
sam carried them out at 六 11月 13 12:54:32 CST 2004
引號是一個特殊字符,所以必須要使用反斜杠\來使s h e l l忽略它的特殊含義。
假設(shè)你希望使用e c h o命令輸出這樣的字符串:“/ d e v / r m t 0”,那么我們只要在引號前面加上反斜杠\即可:

                                                                     
$ echo "\"/dev/rmt0"\"
"/dev/rmt0"
$
read的用法,這是在"man bash"中的一段

                                                                     
read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
    One  line  is  read  from  the  standard input, or from the file descriptor fd supplied as an argument to the -u option, and  the first word is assigned to the first name, the second word to the second name, and so on, with leftover words and their  intervening  separators  assigned  to the last name.  If there are fewer words read from the input stream than names, the remaining names are  assigned  empty  values.  The characters in IFS are used to split the line into words.  The backslash character (\)  may  be used  to  remove any special meaning for the next character read and for line continuation.  Options, if supplied, have the  following meanings:
    -a aname
    The words are assigned to sequential indices of the array variable aname, starting at 0.  aname is unset before any new  values  are  assigned.   Other  name  arguments  are ignored.
    -d delim
    The first character of delim is  used  to  terminate  the input line, rather than newline.
    -e     If the standard input is coming from a terminal, readline (see READLINE above) is used to obtain the line.
    -n nchars
    read returns after reading nchars characters rather  than waiting for a complete line of input.
    -p prompt
    Display prompt on standard error, without a trailing newline, before attempting to read any input.  The prompt is displayed only if input is coming from a terminal.
   -r     Backslash does not act as an escape character.  The backslash is considered to be part of the line.  In  particular,  a  backslash-newline pair may not be used as a line continuation.
   -s     Silent mode.  If input is coming from a terminal, characters are not echoed.
   -t timeout
   Cause  read  to time out and return failure if a complete line of input is not read within timeout  seconds.  This option  has  no  effect if read is not reading input from the terminal or a pipe.
    -u fd  Read input from file descriptor fd.

    If no names are supplied, the line read is assigned to the ariable  REPLY.   The  return  code  is zero, unless end-of-file is ncountered, read times out, or an invalid  file  descriptor  is supplied as the argument to -u.
經(jīng)測試發(fā)現(xiàn)許多shell資料中沒有介紹過的新內(nèi)容,或許地球人都知道了^_^,不管怎樣希望有興趣的一起琢磨一下。
以下是我用來測試的代碼,結(jié)果已經(jīng)略去,請大家自行測試。

                                                                     
read -p "how old r u? " age
echo $age
read -p "some words? " -a words
echo ${words[*]}
read -p "Password: " -s passwd                                 
echo $passwd
read -t 5 auth
echo $auth
read -n 1 key
read -dq -p "input something end with q: " menu
read -e file #在這試試命令歷史和補齊功能
其它:可以自己練習(xí)

                                                                     
[sam@chenwy sam]$ read name
sam
[sam@chenwy sam]$ echo $name
sam
[sam@chenwy sam]$ read name surname
sam ch
[sam@chenwy sam]$ echo $name surname
sam surname
[sam@chenwy sam]$ read name surname
sam ch yiir
[sam@chenwy sam]$ echo $name
sam
[sam@chenwy sam]$ echo $surname
ch yiir


                                                                     
[sam@chenwy sam]$ cat var_test
#!/bin/sh
#var_test
echo -e "First Name :\c"
read name
echo -e "Middle Name :\c"
read middle
echo -e "Last name :\c"
read surname
var_test文件內(nèi)容如上

                                                                     
[sam@chenwy sam]$ ./var_test
First Name :wing
Middle Name :er
Last Name:chenwy
運行var_test文件

請問上面是不是把三個值賦給name,middle,surname三個變量了???
test:

 
/home/lee#read a b c d
1 2 3 4
/home/lee#echo $a
1
/home/lee#echo $b
2
/home/lee#echo $c
3
/home/lee#echo $d
4
/home/lee#
cat:顯示文件內(nèi)容,創(chuàng)建文件,還可以用它來顯示控制字符。

注意:在文件分頁符處不會停下來;會一下顯示完整個文件。因此,可以使用m o r e命令或把c a t命令的輸出通過管道傳遞到另外一個具有分頁功能的命令中,使用命令less file可實現(xiàn)相同的功能。

如下形式

                                                                     
$ cat myfile | more

$ cat myfile | pg
c a t命令的一般形式為:

                                                                     
cat [options] filename1 ... filename2 ...
1、顯示名為m y f i l e的文件:

                                                                     
$ cat myfile
2、顯示m y f i l e 1、m y f i l e 2、m y f i l e 3這三個文件,可以用:

                                                                     
$ cat myfile1 myfile2 myfile3
3、創(chuàng)建一個包含上述三個文件的內(nèi)容,名為b i g f i l e的文件,可以用輸出重定向到新文件中:

                                                                     
$ cat myfile1 myfile2 myfile3 > bigfile
4、如果cat的命令行中沒有參數(shù),輸入的每一行都立刻被cat命令輸出到屏幕上,輸入完畢后按< C T R L - D >結(jié)束

                                                                     
$ cat
Hello world
Hello world  
<ctrl+d>
$
5、新建文件

                                                                     
$cat >myfile
This is great
<ctrl-d>
$cat myfile
This is great
cat:參數(shù)選項

使用方式:

                                                                     
cat [-AbeEnstTuv] [--help] [--version] fileName
說明:把檔案串連接后傳到基本輸出(螢?zāi)换蚣?> fileName 到另一個檔案)

參數(shù):

 
-n 或 --number 由 1 開始對所有輸出的行數(shù)編號
-b 或 --number-nonblank 和 -n 相似,只不過對于空白行不編號
-s 或 --squeeze-blank 當(dāng)遇到有連續(xù)兩行以上的空白行,就代換為一行的空白行
-v 或 --show-nonprinting 顯示非打印字符
例:
顯示時加上行號

                                                                     
$cp /etc/httpd/conf/httpd /usr/sam
$ cat -n httpd.conf
把 httpd.conf 的內(nèi)容加上行號后輸入 httpd1.conf 這個文件里

                                                                     
$cat -n httpd.conf > httpd1.conf
對文件httpd.conf加上行號(空白不加)后顯示

                                                                     
$ cat -b httpd.conf
把 textfile1 和 textfile2 的檔案內(nèi)容加上行號(空白行不加)之后將內(nèi)容附加到 textfile3 里。

                                                                     
$ cat -b textfile1 textfile2 >> textfile3
清空/etc/test.txt檔案內(nèi)容

                                                                     
$cat /dev/null > /etc/test.txt
使用 sed 與 cat 除去空白行

                                                                     
$ cat -s /etc/X11/XF86Config | sed '/^[[:space:]]*$/d'
-s項我試了一下,不成功,不知是不是用錯了


其它參數(shù)來自: (這個我沒試)
http://bbs.chinaunix.net/forum/viewtopic.php?t=438463&highlight=cat

cat 還可以在您查看包含如制表符這樣的非打印字符的文件時起幫助作用。您可以用以下選項來顯示制表符:

 
* -T 將制表符顯示為 ^I

* -v 顯示非打印字符,除了換行符和制表符,它們使用各自效果相當(dāng)?shù)?#8220;控制序列”。例如,當(dāng)您處理一個在 Windows 系統(tǒng)中生成的文件時,這個文件將使用 Control-M(^M)來標(biāo)記行的結(jié)束。對于代碼大于 127 的字符,它們的前面將會被加上 M-(表示“meta”),這與其它系統(tǒng)中在字符前面加上 Alt- 相當(dāng)。

* -E 在每一行的結(jié)束處添加美元符($)。
顯示非打印字符

                                                                     
$ cat -t /etc/X11/XF86Config
...
# Multiple FontPath entries are allowed (they are concatenated together)
# By default, Red Hat 6.0 and later now use a font server independent of
# the X server to render fonts.
^IFontPath^I"/usr/X11R6/lib/X11/fonts/TrueType"
^IFontPath^I"unix/:7100"
EndSection
...


                                                                     
$ cat -E /etc/X11/XF86Config
...
# Multiple FontPath entries are allowed (they are concatenated together)$
# By default, Red Hat 6.0 and later now use a font server independent of$
# the X server to render fonts.$
$
FontPath "/usr/X11R6/lib/X11/fonts/TrueType"$
FontPath "unix/:7100"$
$
EndSection$
...


                                                                     
$ cat -v /etc/X11/XF86Config
...
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-|M-8^X^@^@^@
P^@^O"M-X^O M-@^M^@^@^@M-^@^O"M-@M-k^@M-8*^@
@M-^H$M-@M-9|A(M-@)M-yM-|M-sM-*M-hW^A^@^@j^@
M-|M-sM-%1M-@M-9^@^B^@^@M-sM-+fM-^A= ^@ ^@
F^@^@ ^@M-9^@^H^@^@M-sM-$M-G^E(l!M-@M-^?
^IM-A5^@^@^D^@PM-^]M-^\X1M-H%^@^@^D^@tyM-G
...

 
tee:讀取標(biāo)準(zhǔn)輸入的數(shù)據(jù),并將其內(nèi)容輸出成文件。

語   法:tee [-ai][--help][--version][文件…]
補充說明:tee指令會從標(biāo)準(zhǔn)輸入設(shè)備讀取數(shù)據(jù),將其內(nèi)容輸出到標(biāo)準(zhǔn)輸出設(shè)備,同時保存成文件。我們可利用tee把管道導(dǎo)入的數(shù)據(jù)存成文件,甚至一次保存數(shù)份文件。

參   數(shù):-a 附加到既有文件的面,而非覆蓋它。如果給予tee指令的文件名稱已經(jīng)存在,預(yù)設(shè)會覆蓋該文件的內(nèi)容。加上此參數(shù),數(shù)據(jù)會新增在該文件內(nèi)容的最面,而不會刪除原先之內(nèi)容。
-i 忽略中斷信號
--help 在線幫助
--version 顯示版本信息

例一:
列出文本文件slayers.story的內(nèi)容,同時復(fù)制3份副本,文件名稱分別為ss-copy1、ss-copy2、ss-copy3:


                                                                     
$ cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3
例一: 把列出當(dāng)前目錄,并把結(jié)果結(jié)到myfile里


                                                                     
$ls -l |tee myfile
管道:可以通過管道把一個命令的輸出傳遞給另一個命令作為輸入。管道用豎杠|表示。它的一般形式為:

                                                                     
命令1 |命令2
其中|是管道符號。
上例就是
標(biāo)準(zhǔn)輸入、輸出和錯誤

當(dāng)我們在s h e l l中執(zhí)行命令的時候,每個進(jìn)程都和三個打開的文件相聯(lián)系,并使用文件描述符來引用這些文件。由于文件描述符不容易記憶, s h e l l同時也給出了相應(yīng)的文件名。
下面就是這些文件描述符及它們通常所對應(yīng)的文件名:

 
文件文件描述符
輸入文件—標(biāo)準(zhǔn)輸入0:它是命令的輸入,缺省是鍵盤,也可以是文件或其他命令的輸出。
輸出文件—標(biāo)準(zhǔn)輸出1:它是命令的輸出,缺省是屏幕,也可以是文件。
錯誤輸出文件—標(biāo)準(zhǔn)錯誤2:這是命令錯誤的輸出,缺省是屏幕,同樣也可以是文件。
如果沒有特別指定文件說明符,命令將使用缺省的文件說明符(你的屏幕,更確切地說是你的終端)。

系統(tǒng)中實際上有1 2個文件描述符,但是正如我們在上表中所看到的, 0、1、2是標(biāo)準(zhǔn)輸入、輸出和錯誤??梢匀我馐褂梦募枋龇?到9。


在執(zhí)行命令時,可以指定命令的標(biāo)準(zhǔn)輸入、輸出和錯誤,要實現(xiàn)這一點就需要使用文件重定向。表5 - 1列出了最常用的重定向組合,并給出了相應(yīng)的文件描述符。
在對標(biāo)準(zhǔn)錯誤進(jìn)行重定向時,必須要使用文件描述符,但是對于標(biāo)準(zhǔn)輸入和輸出來說,這不是必需的。

                                                                     
常用文件重定向命令
command > filename 把把標(biāo)準(zhǔn)輸出重定向到一個新文件中
command >> filename 把把標(biāo)準(zhǔn)輸出重定向到一個文件中(追加)
command 1 > fielname 把把標(biāo)準(zhǔn)輸出重定向到一個文件中
command > filename 2>&1 把把標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤一起重定向到一個文件中
command 2 > filename 把把標(biāo)準(zhǔn)錯誤重定向到一個文件中
command 2 >> filename 把把標(biāo)準(zhǔn)輸出重定向到一個文件中(追加)
command >> filename 2>&1 把把標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤一起重定向到一個文件中(追加)
command < filename >filename2 把c o m m a n d命令以f i l e n a m e文件作為標(biāo)準(zhǔn)輸入,以f i l e n a m e 2文件
作為標(biāo)準(zhǔn)輸出
command < filename 把c o m m a n d命令以f i l e n a m e文件作為標(biāo)準(zhǔn)輸入
command << delimiter 把從標(biāo)準(zhǔn)輸入中讀入,直至遇到d e l i m i t e r分界符
command <&m 把把文件描述符m作為標(biāo)準(zhǔn)輸入
command >&m 把把標(biāo)準(zhǔn)輸出重定向到文件描述符m中
command <&- 把關(guān)閉標(biāo)準(zhǔn)輸入
exec:

e x e c命令可以用來替代當(dāng)前s h e l l;換句話說,并沒有啟動子s h e l l。使用這一命令時任何現(xiàn)有環(huán)境都將會被清除,并重新啟動一個s h e l l。它的一般形式為:
exec command
其中的c o m m a n d通常是一個s h e l l腳本。

我所能夠想像得出的描述e x e c命令最貼切的說法就是:當(dāng)這個腳本結(jié)束時,相應(yīng)的會話可能就結(jié)束了。e x e c命令的一個常見用法就是在用戶的. p r o f i l e最后執(zhí)行時,用它來執(zhí)行一些用于增強安全性的腳本。如果用戶的輸入無效,該
s h e l l將被關(guān)閉,然后重新回到登錄提示符。e x e c還常常被用來通過文件描述符打開文件。
e x e c在對文件描述符進(jìn)行操作的時候(也只有在這時),它不會覆蓋你當(dāng)前的s h e l l。


可以看網(wǎng)中人《shell十三問》第六節(jié):
6) exec 跟 source 差在哪?

能把十三問一次性看完最好,不過對我來說還是有些難度,今天才弄清楚第四問,看了好久才明白,目前為止,看完1,2,3,4,及11

exec:

e x e c命令可以用來替代當(dāng)前s h e l l;換句話說,并沒有啟動子s h e l l。使用這一命令時任何現(xiàn)有環(huán)境都將會被清除,并重新啟動一個s h e l l。它的一般形式為:
exec command

其中的c o m m a n d通常是一個s h e l l腳本。

e x e c在對文件描述符進(jìn)行操作的時候,它不會覆蓋你當(dāng)前的s h e l l。
source和exec的區(qū)別
1,我認(rèn)為他們帶的參數(shù)是不一樣的
source通常是shell腳本,而exec不但可以把一個腳本當(dāng)成參數(shù),而且還可以把一個系統(tǒng)命令當(dāng)參數(shù),例如: exec ls
2,另外一個不同就是,exec任務(wù)執(zhí)行完畢后,會執(zhí)行類似logout的操作,而source執(zhí)行完一個任務(wù)后返回當(dāng)前的shell.
3,還有,他們的用途也不是一樣的
在輸入輸出概念里,內(nèi)聯(lián)輸入重定向也是個很常用的用法,在自動FTP腳本里、SHELL的生成器里,wingger可以把它再加進(jìn)來,搜集的過程就是一個非常好的學(xué)習(xí)過程?。?! :P

如:

                                                                     
ftp -ni <<ENDOFSCRIPT
open 192.168.20.100
user username  pwd
bin
prom
cd /u
mput *
close
bye
ENDOFSCRIPT


 
cat > mvfile.sh <<\END

for i in `ls *.tar|sed -e 's/\.tar//'`
do
      mkdir $i
      mv ${i}.tar $i
done

END

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
《shell編程指南》讀書筆記(二)——通配符,標(biāo)準(zhǔn)輸入,輸出及重定向
shell編程-基礎(chǔ)
shell學(xué)習(xí)系列(四).輸入輸出
linux shell 學(xué)習(xí)筆記(四)
玩轉(zhuǎn)Linux文件描述符和重定向
04LinuxShell文件描述符及stdinstdoutstderr重定向
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服