実験棟Linux学習ノート(九)の命令実行順序制御とパイプ

2516 ワード


実験棟Linux学習ノート(九)の命令実行順序制御とパイプ
一、命令実行順序の制御
1.複数のコマンドを順次実行する
簡単な手順で実行するには、";"を使用して完了します.
2.選択された実行コマンド
whichを使用してコマンドをインストールするかどうかを検索し、見つけたらコマンドを実行します.そうしないと、何もしません.
$ which cowsay>/dev/null || echo "cowsay has not been install, please run 'sudo apt-get install cowsay' to install" 
#   cowsay     ,       

二、パイプ
 
パイプは、通常、プロセス間の通信(socketによるネットワーク通信も可能)に用いられる通信メカニズムであり、前の各プロセスの出力(stdout)を直接次のプロセスの入力(stdin)として表現する形式である.パイプは匿名パイプと匿名パイプに分かれています.
いくつかのフィルタを使用するときによく使用されるのは匿名のパイプです.コマンドラインでは|区切り記号で表され、名前付きパイプは簡単には名前付きパイプで、通常はソースプログラムで名前付きパイプしか使用されません.
 
#   /etc           
$ ls -al /etc
 
#               (ls)          (less)   ,           。
$ ls –al /etc | less
 
Cut  
          
#   /etc/passwd    :      1     6               。
$ cut /etc/passwd -d ':' -f 1,6
 
#   /etc/passwd        N   。
#    (     )
$ cut /etc/passwd -c -5
#       (     )
$ cut /etc/passwd -c 5-
#    
$ cut /etc/passwd -c 5
# 2 5   (     )
$ cut /etc/passwd -c 2-5
 
grep   
      stdin         
#   /home/shiyanlou       "shiyanlou"        
$ grep -rnI "shiyanlou" ~ 
# -r                ,-n         ,-I         。
 
#         "yanlou"      
$ export | grep ".*yanlou$" # $        。
 
 wc   
         
wc                、        。
 
#   /etc/passwd       
$ wc /etc/passwd
 
#   
$ wc -l /etc/passwd
#    
$ wc -w /etc/passwd
#    
$ wc -c /etc/passwd
#    
$ wc -m /etc/passwd
#       
$ wc -L /etc/passwd
 
   /etc        
$ ls -dl /etc/*/ | wc –l 
 
sort     
#        
$ cat /etc/passswd | sort
    :
$ cat /etc/passwd | sort -r
       :
$ cat /etc/passwd | sort -t':' -k 3
#    -t             ,    ":"     ;
# -k                  。  /etc/passwd           ,             ,
#              -n  :
$ cat /etc/passwd | sort -t':' -k 3 -n

Uniqデリバリーコマンド
uniq コマンドは、重複行をフィルタまたは出力するために使用できます.
重複行のフィルタ
historyコマンドを使用して、最近実行されたコマンド(実際には${SHELL}_historyファイル、私たちの環境の~/.zsh_historyファイルなど)を表示し、そのコマンドを使用して具体的に何をしたのかを知る必要がなく、コマンドの後のパラメータを削除して重複するコマンドを削除することができます.
$ history | cut -c 8- | cut -d ' ' -f 1 | uniq
# uniq           ,      
#           ,      
$ history | cut -c 8- | cut -d ' ' -f 1 | sort | uniq
#   $ history | cut -c 8- | cut -d ' ' -f 1 | sort -u