Bash 涓娇鐢ㄦ帶鍒惰繍绠楃杩炴帴鍛戒护 | Linux 涓浗

Posted Linux寮€婧愮ぞ鍖?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bash 涓娇鐢ㄦ帶鍒惰繍绠楃杩炴帴鍛戒护 | Linux 涓浗相关的知识,希望对你有一定的参考价值。

鍦ㄥ懡浠よ涓紝浣跨敤鎺у埗杩愮畻绗︿负澶嶅悎鍛戒护娣诲姞閫昏緫銆?
-- David Both

鑷磋阿
缂栬瘧鑷€€|銆€
https://opensource.com/article/18/11/control-operators-bash-shell
 
銆€浣滆€呫€€|銆€David Both
銆€璇戣€呫€€|銆€Jamskr 馃専馃専馃専鍏辫缈昏瘧锛?span class="mq-17">18.0 绡?璐$尞鏃堕棿锛?563 澶?/span>

鍦ㄥ懡浠よ涓紝浣跨敤鎺у埗杩愮畻绗︿负澶嶅悎鍛戒护娣诲姞閫昏緫銆?/p>

缁忓父浼氫娇鐢ㄤ竴浜涚畝鍗曠殑澶嶅悎鎸囦护锛屾瘮濡傝鍦ㄤ竴涓懡浠よ涓繛鎺ュ嚑涓懡浠ゃ€傝繖浜涘懡浠や娇鐢ㄥ垎鍙峰垎闅旓紝琛ㄧず涓€涓懡浠ょ粨鏉熴€備负浜嗗湪涓€涓懡浠よ涓垱寤轰竴绯诲垪绠€鍗曠殑 shell 鍛戒护锛屽彧闇€瑕佷娇鐢ㄥ垎鍙锋妸姣忎竴鏉″懡浠ゅ垎闅斿紑锛屽氨鍍忎笅闈㈣繖鏍凤細

  
    
    
  
  1. command1 ; command2 ; command3 ; command4 ;

鏈€鍚庝竴涓垎鍙蜂綘鍙互涓嶇敤娣诲姞锛屽洜涓哄綋浣犳寜涓嬪洖杞﹂敭鏃跺氨琛ㄧず涓€涓懡浠ょ殑缁撴潫锛屼絾鏄负浜嗗拰鍏跺畠鐨勪繚鎸佷竴鑷达紝杩樻槸寤鸿鍔犱笂姣旇緝濂姐€?/p>

鎵€鏈夌殑鍛戒护鎵ц閮芥病鏈変粈涔堥棶棰?鈥斺€?鍙娌℃湁浠€涔堟剰澶栧彂鐢熴€備絾鏄綋鍑洪棶棰樻椂鍒板簳鍙戠敓浜嗕粈涔堝憿锛熸垜浠彲浠ラ娴嬶紝骞朵笖閫氳繃 Bash 涓唴缃殑 && 鍜?nbsp;|| 杩愮畻绗﹁窡韪繖浜涢敊璇€傝繖涓や釜鎺у埗杩愮畻绗︽彁渚涗簡涓€浜涙祦鎺у埗锛屽彲浠ヨ鎴戜滑鏀瑰彉浠g爜鎵ц闃熷垪鐨勯『搴忋€傚垎鍙峰拰鎹㈣绗︿篃琚涓烘槸 Bash 鐨勬帶鍒惰繍绠楃銆?/p>

&& 杩愮畻绗︽剰涔夌畝鍗曟潵璇村氨鏄€滃鏋?nbsp;command1 鎵ц鎴愬姛锛屽氨鎺ョ潃鎵ц command2銆傗€濆鏋?nbsp;command1 鍥犱负浠讳綍鍘熷洜鎵ц澶辫触锛岄偅涔?nbsp;command2 灏嗕笉鎵ц銆傝繖涓娉曠湅涓嬫潵鍍忚繖鏍凤細

  
    
    
  
  1. command1 && command2

杩欐牱鍐欐槸鍏佽鐨勶紝鍥犱负姣忎竴涓懡浠ら兘浼氳繑鍥炰竴涓€硷紙RC锛夌粰 shell 鏉ヨ〃绀鸿繖涓懡浠ゅ湪鎵ц鐨勮繃绋嬩腑鏄惁鎵ц鎴愬姛鎴栬€呭け璐ャ€傞€氬父锛岃繑鍥炲€兼槸 0 琛ㄧず鎴愬姛锛岃€屼竴涓鏁板€艰〃绀轰笉鍚岀绫荤殑閿欒銆傛湁涓€浜涚郴缁熺鐞嗗伐鍏蜂粎浠呰繑鍥炰竴涓?nbsp;1 鏉ヨ〃绀烘墍鏈夌殑閿欒锛屼絾鏄篃鏈夊緢澶氬伐鍏蜂娇鐢ㄥ叾瀹冪殑姝f暟鐨勮繑鍥炲€兼潵琛ㄧず鍚勭绫诲瀷閿欒銆?/p>

鎴戜滑鍙互寰堝鏄撶殑浣跨敤鑴氭湰鏉ユ鏌?shell 鍙橀噺 $?锛屽彲浠ラ€氳繃鍛戒护鍒楄〃涓殑涓嬩竴涓懡浠わ紝鎴栬€呭彲浠ョ洿鎺ヤ娇鐢ㄧ郴缁熺鐞嗗伐鍏锋鏌ャ€傛垜浠竴璧锋潵鐪嬭繖浜涜繑鍥炲€笺€傝繍琛屼竴涓畝鍗曠殑鍛戒护鐒跺悗绔嬪嵆妫€鏌ュ畠鐨勮繑鍥炲€硷紝杩欎釜杩斿洖鍊煎缁堟槸灞炰簬鏈€鍚庝竴涓繍琛岀殑鍛戒护銆?/p>

  
    
    
  
  1. [student@studentvm1 ~]$ ll ; echo "RC = $?"

  2. total 284

  3. -rw-rw-r--  1 student student   130 Sep 15 16:21 ascii-program.sh

  4. drwxrwxr-x  2 student student  4096 Nov 10 11:09 bin

  5. <snip>

  6. drwxr-xr-x. 2 student student  4096 Aug 18 10:21 Videos

  7. RC = 0

  8. [student@studentvm1 ~]$

杩欎釜杩斿洖鍊兼槸 0锛岃〃绀鸿繖涓懡浠ゆ墽琛屾垚鍔熶簡銆傜幇鍦ㄥ皾璇曚娇鐢ㄥ悓鏍风殑鍛戒护鍦ㄤ竴浜涙垜浠病鏈夋潈闄愮殑鐩綍涓娿€?/p>

  
    
    
  
  1. [student@studentvm1 ~]$ ll /root ; echo "RC = $?"

  2. ls: cannot open directory '/root': Permission denied

  3. RC = 2

  4. [student@studentvm1 ~]$

杩欎釜杩斿洖鍊肩殑鍚箟鍙互鍦?nbsp;ls 鍛戒护鐨?man 椤甸潰[1] 涓壘鍒般€?/p>

鐜板湪鎴戜滑鏉ヨ瘯璇?nbsp;&& 杩欎釜鎺у埗杩愮畻绗︼紝鍥犱负瀹冧篃鍙兘浼氳鐢ㄥ湪涓€涓懡浠よ绋嬪簭涓€傛垜浠皢浠庝竴涓畝鍗曠殑绀轰緥寮€濮嬶細鍒涘缓涓€涓柊鐩綍锛屽鏋滃垱寤烘垚鍔熷氨鍦ㄨ繖涓洰褰曚腑鍒涘缓涓€涓枃浠躲€?/p>

鎴戜滑闇€瑕佷竴涓洰褰曞彲浠ュ垱寤哄叾瀹冪殑鐩綍銆傞鍏堬紝鍦ㄤ綘鐨勫鐩綍涓垱寤轰竴涓复鏃剁殑鐩綍鐢ㄦ潵鍋氭祴璇曘€?/p>

  
    
    
  
  1. [student@studentvm1 ~]$ cd ; mkdir testdir

鍦?nbsp;~/testdir 涓柊寤轰竴涓洰褰曪紝杩欎篃搴旇鏄竴涓┖鐩綍锛屽洜涓烘槸浣犲垰鍒氬垱寤虹殑锛岀劧鍚庡垱寤轰竴涓柊鐨勭┖鏂囦欢鍦ㄨ繖涓柊鐩綍涓€備笅闈㈢殑鍛戒护鍙互鍋氳繖浜涗簨鎯呫€?/p>

  
    
    
  
  1. [student@studentvm1 ~]$ mkdir ~/testdir/testdir2 && touch ~/testdir/testdir2/testfile1

  2. [student@studentvm1 ~]$ ll ~/testdir/testdir2/

  3. total 0

  4. -rw-rw-r-- 1 student student 0 Nov 12 14:13 testfile1

  5. [student@studentvm1 ~]$

鎴戜滑鐪嬪埌涓€鍒囬兘杩愯寰楀緢濂斤紝鍥犱负 testdir 鐩綍鏄闂笖鍙啓鐨勩€傜劧鍚庢垜浠敼鍙?nbsp;testdir鐩綍鐨勬潈闄愶紝璁╃敤鎴?nbsp;student 涓嶅啀鍏锋湁璁块棶鐨勬潈闄愩€傛搷浣滃涓嬶細

  
    
    
  
  1. [student@studentvm1 ~]$ chmod 076 testdir ; ll | grep testdir

  2. d---rwxrw-. 3 student student  4096 Nov 12 14:13 testdir

  3. [student@studentvm1 ~]$

鍦ㄩ暱鍒楄〃锛?code class="mq-305">ll锛夊懡浠ゅ悗闈娇鐢?nbsp;grep 鍛戒护鏉ュ垪鍑?nbsp;testdir 鐩綍銆備綘鍙互鐪嬪埌鐢ㄦ埛 student 涓嶅啀鏈?nbsp;testdir 鐩綍鐨勮闂潈闄愩€傜幇鍦ㄦ垜浠儚涔嬪墠涓€鏍疯繍琛屽悓鏍风殑鍛戒护锛屼絾鏄湪 testdir 鐩綍涓垱寤虹殑鏄竴涓笉鍚岀殑鐩綍銆?/p>

  
    
    
  
  1. [student@studentvm1 ~]$ mkdir ~/testdir/testdir3 && touch ~/testdir/testdir3/testfile1

  2. mkdir: cannot create directory 鈥?home/student/testdir/testdir3鈥? Permission denied

  3. [student@studentvm1 ~]$

灏界鎴戜滑涔熷悓鏍峰緱鍒颁簡涓€涓敊璇殑娑堟伅锛屼絾 && 鎺у埗杩愮畻绗﹂樆姝簡 touch 鍛戒护鐨勮繍琛岋紝鍥犱负鍦ㄥ垱寤?nbsp;testdir3 鐩綍鐨勬椂鍊欏彂鐢熶簡閿欒銆傞€氳繃杩欑澶嶅悎鐨勬祦鎺у埗鍙互闃绘涓€浜涢敊璇殑鍙戠敓浣夸簨鎯呭彉涔便€備絾鏄繖鏍风湅璧锋潵鍙樺緱绋嶅井澶嶆潅浜嗕竴浜涖€?/p>

|| 鎺у埗杩愮畻绗﹀厑璁告坊鍔犲彟涓€涓懡浠わ紝杩欎釜鍛戒护鍦ㄥ垵濮嬬▼搴忚鍙ヨ繑鍥炲€煎ぇ浜?0 鏃舵墽琛屻€?/p>

  
    
    
  
  1. [student@studentvm1 ~]$ mkdir ~/testdir/testdir3 && touch ~/testdir/testdir3/testfile1 || echo "An error occurred while creating the directory."

  2. mkdir: cannot create directory 鈥?home/student/testdir/testdir3鈥? Permission denied

  3. An error occurred while creating the directory.

  4. [student@studentvm1 ~]$

褰撴垜浠娇鐢?nbsp;&& 鍜?nbsp;|| 鎺у埗杩愮畻绗︽椂锛屼娇鐢ㄦ祦鎺у埗鐨勫鍚堝懡浠ょ殑璇硶鏍煎紡閫氬父鏄笅闈㈣繖鏍风殑褰㈠紡銆?/p>

  
    
    
  
  1. preceding commands ; command1 && command2 || command3 ; following commands

浣跨敤鎺у埗杩愮畻绗︾殑澶嶅悎鍛戒护鍙互鍦ㄥ叾瀹冨懡浠や箣鍓嶆垨鑰呬箣鍚庯紝杩欎簺鍜屾帶鍒惰繍绠楃娴佹帶鍒舵湁鍏崇郴锛屼絾鏄笉鍙楁帶鍒惰繍绠楃娴佹帶鍒剁殑褰卞搷銆傚鏋滀笉鑰冭檻澶嶅悎鍛戒护鐨勬祦鎺у埗涓彂鐢熺殑浠讳綍浜嬫儏閭d箞鎵€鏈夌殑鍛戒护閮藉皢鎵ц銆?/p>

褰撶▼搴忓嚭闂鏃讹紝杩欎簺娴佹帶鍒惰繍绠楃浣垮緱鍦ㄥ懡浠や腑澶勭悊鍑洪敊鍜岄€氱煡鎴戜滑鍙樺緱鏇存湁鏁堢巼銆傛垜鐩存帴鍦ㄥ懡浠よ涓娇鐢ㄥ畠浠紝涔熷湪鑴氭湰涓娇鐢ㄣ€?/p>

浣犲彲浠ヤ互 root 鐢ㄦ埛鐨勮韩浠芥潵鍒犻櫎杩欎釜鐩綍鍜屽畠閲岄潰鐨勫唴瀹广€?/p>

  
    
    
  
  1. [root@studentvm1 ~]# rm -rf /home/student/testdir

浣犳槸鎬庢牱浣跨敤 Bash 鎺у埗杩愮畻绗︾殑鍛紵鍦ㄨ瘎璁哄尯涓憡璇夋垜浠€?/p>


via: https://opensource.com/article/18/11/control-operators-bash-shell

鏈枃鐢?nbsp;LCTT 鍘熷垱缂栬瘧锛?span class="mq-453">Linux涓浗 鑽h獕鎺ㄥ嚭


以上是关于Bash 涓娇鐢ㄦ帶鍒惰繍绠楃杩炴帴鍛戒护 | Linux 涓浗的主要内容,如果未能解决你的问题,请参考以下文章

UDP socket涔熷彲浠ヤ娇鐢╟onnect绯荤粺璋冪敤

涓€瀛﹀氨浼氱殑git鍛戒护

HTTP keepalive璇﹁В

Ansible 鏁欑▼锛氱畝鍗?Ansible 鍛戒护浠嬬粛 | Linux 涓浗

koa,express,node閫氱敤鏂规硶杩炴帴MongoDB