[Linux Command Line and Shell Scripting Bible] basic shell script

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   使用   sp   

  1 #!/bin/bash  2 ############################################  3 # @content chapter 8,9 of Linux Command Line and Shell Scripting Bible  4 # @reader gavin  5 # @date 2014/12/14  6 ############################################  7 CHAPTER 8  8   9 + user varriable make the shell more like a computer program 10     1. 在給使用者自訂變數賦值時,不能出現空格     11     2.shell script automatically determine the data type 12  13 + The backtick(`) allows you to assign the output of a shell command to a vari- 14     able 15     1.The shell runs the command within the backticks, and assigns the output to  16     the variable testing 17     (DO) 18     today=`date +%Y%m%d` 19  20 + redirecting the input and output 21     1.If the output file already exists, the redirect operator overwrites the exi- 22     sting file with the new file data 23     (DO): 24     ls > test 25     2.instead of overwriting the file’s contents, you may need to append output fr- 26     om a command to an existing file 27     (DO): 28     ls >> test 29     3.There’s another method of input redirection, called inline input redirection. 30     This method allows you to specify the data for input redirection on the command 31     line instead of in a file.The inline input redirection symbol is the double les- 32     s-than symbol (<<). Besides this symbol,you must  specify a text marker that  33     delineates the beginning and end of the data used for input. 34     (DO) 35     wc << EOF 36     > hello 37     > inline 38     > input 39     > redirection 40     > EOF 41     此處的EOF可以換成任意的MARKER 42  43 + pipe 44     1.The Linux system actually runs both commands at the same time, linking them 45     together internally in the system. 46     2.There’s no limit to the number of pipes you can use in a command 47  48 + performing math 49     1.expr命令可以執行數學運算,但是限制比較多,如要避免wildcard matching 50     (DO) 51     a=2 52     b=5 53     var=`expr $a \* $b` 54     echo -n "a*b is : " 55     echo $var 56     2.可以使用$[],來進行數學運算 57     (DO) 58     res=$[ $a + $b - ($a * $c) / ($b + $c) + $d ] 59     3.The bash shell mathematical operators only support integer arithmetic. This 60     is a huge limitation     if you’re trying to do any sort of real-world mathemat- 61     ical calculations. BUT There have been several solutions for overcoming the  62     bash integer limitation. The most popular solution uses the built-in bash ca- 63     lculator (called bc). 64     (TODO):page 220 65     這裡可以使用bash calculater,但是暫時用不到,以後用到來再補上 66  67 + exiting the script 68     1.Every command that runs in the shell uses an exit status to indicate to the 69     shell that it’s done processing.(退出狀態exit status,是一個0-255的整形值。他 70     是由執行的命令完成後傳給shell的) 71     2.$?成功執行後的退出狀態是0 72     Linux provides the $? special variable that holds the exit status value from  73     the last command that executed. You must view or use the $? variable immedi- 74     ately after the command you want to check. It changes values to the exit st- 75     atus of the last command executed by the shell Code 0 Successful completion  76     of the command 77     (DO) 78         1 General unknown error 79         2 Misuse of shell command 80         126 The command can’t execute 81         127 Command not found 82         128 Invalid exit argument 83         128+x Fatal error with Linux signal x 84         130 Command terminated with Ctl-C 85         255 Exit status out of range 86  87 + exit command 88     1.you can change that to return your own exit status code. The exit command  89     allows you to specify an exit status when your script ends: 90     (DO) 91         res=$[ $a + $b] 92         exit 5 93         當檢測上面程式的exit code時$?=5,這裡也可以使用變數作為exit的值 94  95  96 ########################################### 97 CHAPTER 9 98  99 + if100     if []; then101         prog1102     elif []; then103         prog2104     elif []; then105         prog3106     fi107 108 + the test command 109     1.直接判斷一個命令的推測狀態    if command; then110     2.可以使用 if test command; then。也可以使用 if [ command ]; then;注111     意${中括弧裡的命令必須前後都有空格}112     3.test command 可以測試3類條件113     (DO)114         ■  Numeric comparisons115         ■  String comparisons116         ■  File comparisons117 118 + numeric comparisons119     n1 -eq n2 Check if n1 is equal to n2.120     n1 -ge n2 Check if n1 is greater than or equal to n2.121     n1 -gt n2 Check if n1 is greater than n2.122     n1 -le n2 Check if n1 is less than or equal to n2.123     n1 -lt n2 Check if n1 is less than n2.124     n1 -ne n2 Check if n1 is not equal to n2.125     (DO)126     if [ $a -ge $b ]; then127         do something128     elif [ $a -lt $b ]; then129         do something else130     fi131     ${數值比較不能處理浮點類型}132 133     

 

[Linux Command Line and Shell Scripting Bible] basic shell script

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.