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

  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 + if
100     if []; then
101         prog1
102     elif []; then
103         prog2
104     elif []; then
105         prog3
106     fi
107 
108 + the test command 
109     1.直接判断一个命令的推测状态    if command; then
110     2.可以使用 if test command; then。也可以使用 if [ command ]; then;注
111     意${中括号里的命令必须前后都有空格}
112     3.test command 可以测试3类条件
113     (DO)
114         ■  Numeric comparisons
115         ■  String comparisons
116         ■  File comparisons
117 
118 + numeric comparisons
119     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 ]; then
127         do something
128     elif [ $a -lt $b ]; then
129         do something else
130     fi
131     ${数值比较不能处理浮点类型}
132 
133     

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。