iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 7
2
自我挑戰組

Linux學習系列 第 7

Linux輸入多重指令

一次輸入執行命令,而不想要分次執行命令時,該如何?有兩個選擇,一個是透過shell script撰寫腳本去執行,一種則是透過一些符號(|、||、&&、;)來分隔多種指令

1不考慮指令相關性的連續指令下達(指令;指令;指令)
https://ithelp.ithome.com.tw/upload/images/20181020/20111994807mLDtgkS.png
2依據正確執行($?=0)或是錯誤 ($?≠0)的多重指令,從左而右
||代表錯誤 ($?≠0)時,會執行接下來的指令
&&代表正確執行($?=0)時,會執行接下來的指令
第一次輸入指令錯誤,因為touch後沒空一格
指令先列出/tmp/123資料夾有什麼,如果沒有123這個資料夾($?≠0),接著就會執行
創建123資料夾,創建123資料夾正確執行($?=0)後,就會執行創建1234檔案
https://ithelp.ithome.com.tw/upload/images/20181020/20111994td1YfverJ7.png
3 pipe
1管線命令與『連續下達命令』是不一樣的
2管線命令『 | 』僅能處理經由前面一個指令傳來的正確資訊,也就是 standard output的資訊,對於stdandard error並沒有直接處理的能力。

管線命令就如這張圖
第一個指令的輸出當成第二個指令的輸入,第二個指令的輸出當成第三個指令的輸入
http://linux.vbird.org/linux_basic/0320bash//0320bash_3.png

那什麼是filter指令
就是內容會疊加的指令

A pipe can pass the standard output of one operation to the standard input of another, but a filter can modify the stream. A filter takes the standard input, does something useful with it, and then returns it as a standard output. Linux has a large number of filters. Some useful ones are the commands awk, grep, sed, spell.

來源:https://www.linux.com/news/pipes-and-filters
如圖:第一個指令是動物,第二個指令是國內(就變成國內的動物),第三個指令是小(就會變成國內的小動物)
https://www.guru99.com/images/Filter(1).png
所以就是|(管線命令)+ awk, grep, sed, spell…等等(filter指令)
網址:
http://linux.vbird.org/linux_basic/0320bash.php#redirect

網址:
https://www.guru99.com/linux-pipe-grep.html
http://linux.vbird.org/linux_basic/0320bash.php#
http://linux.vbird.org/linux_basic/0330regularex.php#awk
這種filter指令很多,這邊只有練習一些

以下是一些練習:
一開始先輸入cat /etc/protocols(關於協定的檔案)
https://ithelp.ithome.com.tw/upload/images/20181020/20111994Hv9hoiNyWX.png

因為檔案文字太多,所以畫面會直接跳到最後一行,而看不到前面的內容
https://ithelp.ithome.com.tw/upload/images/20181020/20111994OW6jKiuecp.png

但是指令如果用 cat /etc/protocols | less 就會跳到這個畫面,可以上下捲動像記事本一樣,按q退出畫面
https://ithelp.ithome.com.tw/upload/images/20181020/20111994eerFOoyHZ9.png

cat /etc/protocols | pg和cat /etc/protocols | more
pg找不到指令,more也是換個畫面,然後是按enter看內容
pg deprecated
https://en.wikipedia.org/wiki/Pg_(Unix)
https://ithelp.ithome.com.tw/upload/images/20181020/20111994hH5G88yP02.png

尋找ip關鍵字,如果沒有區分大小寫前面加-i
https://ithelp.ithome.com.tw/upload/images/20181020/20111994LPtZSV1yUd.png

前面加-v,排除ip
https://ithelp.ithome.com.tw/upload/images/20181020/20111994IMgtpqpk7W.png
前面加-n,顯示第幾行
https://ithelp.ithome.com.tw/upload/images/20181020/20111994QBH2ldQoVR.png

sort:按字母排序
https://ithelp.ithome.com.tw/upload/images/20181020/201119943CzZTEwUT7.png

沒有ip這個關鍵字和顛倒排序
https://ithelp.ithome.com.tw/upload/images/20181020/20111994a2YXInal7r.png

Cut指令:-d '#' –f 1 代表用#分隔範圍,1代表第一個被分開的範圍
https://ithelp.ithome.com.tw/upload/images/20181020/20111994J8EyK3l0nG.png
-c 12-(代表由左而右數前11個字會不見)
https://ithelp.ithome.com.tw/upload/images/20181020/20111994Ib2LSBdqZI.png
但是像這種cut就沒用,因為是多個字元的空白鍵,而不是單一字元的空邊鍵
這種狀況是不能用cut的
https://ithelp.ithome.com.tw/upload/images/20181020/20111994I43ozZxlxM.png
這種情況要使用awk
awk主要是處理『欄位內的資料』,而預設的『欄位的分隔符號為 "空白鍵" 或 "[tab]鍵"』
這樣就可以取的第一和第四個欄位了,因為預設的分隔符號是空白鍵
https://ithelp.ithome.com.tw/upload/images/20181020/20111994llpYWEYMfw.png


上一篇
Linux資料流重導向指令
下一篇
Linux Shell Scripting
系列文
Linux學習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言