Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
生活随笔
收集整理的這篇文章主要介紹了
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
三、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
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
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
if ... else 語句也可以寫成一行,以命令的方式來運行,像這樣: [cpp]?view plaincopy
if ... else 語句也經常與 test 命令結合使用,如下所示: [cpp]?view plaincopy
四、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
再舉一個例子: [cpp]?view plaincopy
總結
以上是生活随笔為你收集整理的Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @action 注解
- 下一篇: 使用Control Flash 更新AB