日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句

發布時間:2023/12/9 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Shell echo命令

echo是Shell的一個內部指令,用于在屏幕上打印出指定的字符串。命令格式: echo arg您可以使用echo實現更復雜的輸出格式控制。

顯示轉義字符

echo "\"It is a test\"" 結果將是:
"It is a test"

雙引號也可以省略。

顯示變量

name="OK" echo "$name It is a test" 結果將是:
OK It is a test

同樣雙引號也可以省略。

如果變量與其它字符相連的話,需要使用大括號({ }): mouth=8 echo "${mouth}-1-2009" 結果將是:
8-1-2009

顯示換行

echo "OK!\n" echo "It is a test" 輸出:
OK!
It is a test

顯示不換行

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"OK!</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\c</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"It is a test"</span></li></ol> 輸出:
OK!It si a test

顯示結果重定向至文件

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"It is a test"</span> <span class="sh_symbol" style="color: rgb(48, 48, 238);">></span> myfile</li></ol>

原樣輸出字符串

若需要原樣輸出字符串(不進行轉義),請使用單引號。例如: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">'$name</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\"</span><span class="sh_string" style="color: rgb(24, 97, 167);">'</span></li></ol>

顯示命令執行結果

<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo `date`</li></ol> 結果將顯示當前日期

二、shell printf命令:格式化輸出語句

printf 命令用于格式化輸出, 是echo命令的增強版。它是C語言printf()庫函數的一個有限的變形,并且在語法上有些不同。

注意:printf 由 POSIX?標準所定義,移植性要比 echo 好。

如同 echo 命令,printf 命令也可以輸出簡單的字符串:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">$printf</span> <span class="sh_string" style="color: rgb(24, 97, 167);">"Hello, Shell</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">Hello<span class="sh_symbol" style="color: rgb(48, 48, 238);">,</span> Shell</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">$</li></ol> printf 不像 echo 那樣會自動換行,必須顯式添加換行符(\n)。

printf 命令的語法:printf format-string [arguments...]format-string 為格式控制字符串,arguments 為參數列表。

printf()在C語言入門教程中已經講到,功能和用法與 printf 命令類似,請查看:C語言格式輸出函數printf()詳解

這里僅說明與C語言printf()函數的不同:
  • printf 命令不用加括號
  • format-string 可以沒有引號,但最好加上,單引號雙引號均可。
  • 參數多于格式控制符(%)時,format-string?可以重用,可以將所有參數都轉換。
  • arguments 使用空格分隔,不用逗號。

請看下面的例子: [cpp]?view plaincopy
  • #?format-string為雙引號??
  • $?printf?"%d?%s\n"?1?"abc"??
  • 1?abc??
  • #?單引號與雙引號效果一樣???
  • $?printf?'%d?%s\n'?1?"abc"???
  • 1?abc??
  • #?沒有引號也可以輸出??
  • $?printf?%s?abcdef??
  • abcdef??
  • #?格式只指定了一個參數,但多出的參數仍然會按照該格式輸出,format-string?被重用??
  • $?printf?%s?abc?def??
  • abcdef??
  • $?printf?"%s\n"?abc?def??
  • abc??
  • def??
  • $?printf?"%s?%s?%s\n"?a?b?c?d?e?f?g?h?i?j??
  • a?b?c??
  • d?e?f??
  • g?h?i??
  • j??
  • #?如果沒有?arguments,那么?%s?用NULL代替,%d?用?0?代替??
  • $?printf?"%s?and?%d?\n"???
  • and?0??
  • #?如果以?%d?的格式來顯示字符串,那么會有警告,提示無效的數字,此時默認置為?0??
  • $?printf?"The?first?program?always?prints'%s,%d\n'"?Hello?Shell??
  • -bash:?printf:?Shell:?invalid?number??
  • The?first?program?always?prints?'Hello,0'??
  • $??
  • 注意,根據POSIX標準,浮點格式%e、%E、%f、%g與%G是“不需要被支持”。這是因為awk支持浮點預算,且有它自己的printf語句。這樣Shell程序中需要將浮點數值進行格式化的打印時,可使用小型的awk程序實現。然而,內建于bash、ksh93和zsh中的printf命令都支持浮點格式。

    三、Shell if else語句

    if 語句通過關系運算符判斷表達式的真假來決定執行哪個分支。Shell 有三種 if ... else 語句:
    • if ... fi 語句;
    • if ... else ... fi 語句;
    • if ... elif ... else ... fi 語句。

    1) if ... else 語句

    if ... else 語句的語法:if [ expression ] thenStatement(s) to be executed if expression is true fi如果?expression 返回 true,then 后邊的語句將會被執行;如果返回 false,不會執行任何語句。

    最后必須以 fi 來結尾閉合 if,fi 就是 if 倒過來拼寫,后面也會遇見。

    注意:expression 和方括號([ ])之間必須有空格,否則會有語法錯誤。

    舉個例子:
    [cpp]?view plaincopy
  • #!/bin/sh??
  • a=10??
  • b=20??
  • if?[?$a?==?$b?]??
  • then??
  • ???echo?"a?is?equal?to?b"??
  • fi??
  • if?[?$a?!=?$b?]??
  • then??
  • ???echo?"a?is?not?equal?to?b"??
  • fi??
  • 運行結果:a is not equal to b

    2)?if ... else ... fi 語句

    if ... else ... fi 語句的語法:if [ expression ] thenStatement(s) to be executed if expression is true elseStatement(s) to be executed if expression is not true fi如果 expression 返回 true,那么 then 后邊的語句將會被執行;否則,執行 else 后邊的語句。

    舉個例子:
    [cpp]?view plaincopy
  • #!/bin/sh??
  • a=10??
  • b=20??
  • if?[?$a?==?$b?]??
  • then??
  • ???echo?"a?is?equal?to?b"??
  • else??
  • ???echo?"a?is?not?equal?to?b"??
  • fi??
  • 執行結果:a is not equal to b

    3)?if ... elif ... fi 語句

    if ... elif ... fi 語句可以對多個條件進行判斷,語法為:if [ expression 1 ] thenStatement(s) to be executed if expression 1 is true elif [ expression 2 ] thenStatement(s) to be executed if expression 2 is true elif [ expression 3 ] thenStatement(s) to be executed if expression 3 is true elseStatement(s) to be executed if no expression is true fi哪一個?expression 的值為 true,就執行哪個 expression 后面的語句;如果都為 false,那么不執行任何語句。

    舉個例子:
    [cpp]?view plaincopy
  • #!/bin/sh??
  • a=10??
  • b=20??
  • if?[?$a?==?$b?]??
  • then??
  • ???echo?"a?is?equal?to?b"??
  • elif?[?$a?-gt?$b?]??
  • then??
  • ???echo?"a?is?greater?than?b"??
  • elif?[?$a?-lt?$b?]??
  • then??
  • ???echo?"a?is?less?than?b"??
  • else??
  • ???echo?"None?of?the?condition?met"??
  • fi??
  • 運行結果:a is less than b
    if ... else 語句也可以寫成一行,以命令的方式來運行,像這樣: [cpp]?view plaincopy
  • if?test?$[2*3]?-eq?$[1+5];?then?echo?'The?two?numbers?are?equal!';?fi;??

  • if ... else 語句也經常與 test 命令結合使用,如下所示: [cpp]?view plaincopy
  • num1=$[2*3]??
  • num2=$[1+5]??
  • if?test?$[num1]?-eq?$[num2]??
  • then??
  • ????echo?'The?two?numbers?are?equal!'??
  • else??
  • ????echo?'The?two?numbers?are?not?equal!'??
  • fi??
  • 輸出:The two numbers are equal!test 命令用于檢查某個條件是否成立,與方括號([ ])類似。

    四、Shell case esac語句

    case ... esac 與其他語言中的 switch ... case 語句類似,是一種多分枝選擇結構。 case 語句匹配一個值或一個模式,如果匹配成功,執行相匹配的命令。case語句格式如下:case 值 in 模式1)command1command2command3;; 模式2)command1command2command3;; *)command1command2command3;; esaccase工作方式如上所示。取值后面必須為關鍵字 in,每一模式必須以右括號結束。取值可以為變量或常數。匹配發現取值符合某一模式后,其間所有命令開始執行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最后。

    取值將檢測匹配的每一個模式。一旦模式匹配,則執行完匹配模式相應命令后不再繼續其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執行后面的命令。

    下面的腳本提示輸入1到4,與每一種模式進行匹配: [cpp]?view plaincopy
  • echo?'Input?a?number?between?1?to?4'??
  • echo?'Your?number?is:\c'??
  • read?aNum??
  • case?$aNum?in??
  • ????1)??echo?'You?select?1'??
  • ????;;??
  • ????2)??echo?'You?select?2'??
  • ????;;??
  • ????3)??echo?'You?select?3'??
  • ????;;??
  • ????4)??echo?'You?select?4'??
  • ????;;??
  • ????*)??echo?'You?do?not?select?a?number?between?1?to?4'??
  • ????;;??
  • esac??
  • 輸入不同的內容,會有不同的結果,例如:Input a number between 1 to 4 Your number is:3 You select 3
    再舉一個例子: [cpp]?view plaincopy
  • #!/bin/bash??
  • option="${1}"??
  • case?${option}?in??
  • ???-f)?FILE="${2}"??
  • ??????echo?"File?name?is?$FILE"??
  • ??????;;??
  • ???-d)?DIR="${2}"??
  • ??????echo?"Dir?name?is?$DIR"??
  • ??????;;??
  • ???*)???
  • ??????echo?"`basename?${0}`:usage:?[-f?file]?|?[-d?directory]"??
  • ??????exit?1?#?Command?to?come?out?of?the?program?with?status?1??
  • ??????;;??
  • esac??
  • 運行結果如下: $./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] $ ./test.sh -f index.htm $ vi test.sh $ ./test.sh -f index.htm File name is index.htm $ ./test.sh -d unix Dir name is unix $

    總結

    以上是生活随笔為你收集整理的Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。