Linux shell script programming (3) and linuxshell Script Programming
Linux shell Script Programming
1 Process Control: 2 loop statements: for, while, until 3 4 while loop: 5 6 while CONDITION; do 7 loop body 8 done 9 10 entry CONDITION: when CONDITION is "true"; 11 exit CONDITION: When CONDITION is "false"; 12 13 while CONDITION; do 14 loop body 15 control variable correction expression 16 done 17 18 Example: returns the sum of all positive integers within 100. 19 #! /Bin/bash 20 #21 declare-I sum = 0 22 declare-I = 1 23 24 while [$ I-le 100]; do 25 let sum + = $ I 26 let I ++ 27 done 28 29 echo "Sum: $ sum. "30 31 exercise: Calculate the sum of all odd numbers within 100 and the sum of all even numbers respectively; 32 33 example: print the 9-9 multiplication table; 34 #! /Bin/bash 35 #36 declare-I = 1 37 declare-I j = 1 38 39 while [$ j-le 9]; do 40 while [$ I-le $ j]; do 41 echo-e-n "$ {I} X $ {j} = $ [$ I * $ j] \ t" 42 let I ++ 43 done 44 echo 45 let I = 1 46 let j ++ 47 done 48 49 until loop: 50 until CONDITION; do 51 loop body 52 correction expression of loop control variable 53 done 54 55 entry CONDITION: 56 exit CONDITION when CONDITION is false: 57 58 example: Calculate the sum of positive integers less than 100; 59 #! /Bin/bash 60 #61 declare-I sum = 0 62 declare-I = 1 63 64 until [$ I-gt 100]; do 65 let sum + = $ I 66 let I ++ 67 done 68 69 echo "Sum: $ sum. "70 Exercise 1: Calculate the sum of all even numbers within 100 and the sum of all odd numbers respectively; 71 Exercise 2: implement the 9-9 multiplication table; 72 Exercise 3: use the while and until cycles respectively to add 10 users: user-user10; 73 74 cycle control: 75 continue: early completion of the cycle, and directly enter the next round; 76 break [n]: end the loop ahead of schedule; 77 78 while CONDTION; do 79 ....... 80 if CONDITION; then 81 break [n] 82 fi 83 done 84 85 while CONDT ION; do 86 ....... 87 if CONDTION2; then 88 continue [n] 89 fi 90 ....... 91 92 example: Calculate the sum of all even numbers within 100; 93 #! /Bin/bash 94 #95 declare-I sum = 0 96 declare-I = 0 97 98 while [$ I-le 100]; do 99 let I ++ 100 if [$ [$ I % 2]-eq 1]; then101 echo "$ I is a odd. "102 continue103 fi104 let sum + = $ i105 done106 107 echo" Sum: $ sum. "108 109 endless loop: 110 while true; do111 cyclic body 112 if CONDITION; then113 break114 fi115 done116 117 until false; do118 cyclic body 119 if CONDITION; then120 break121 fi122 done123 124 example; check whether the current system is named 'gent every 3 seconds. Oo 'User Logon; 125 If gentoo is logged on, gentoo is logged on; 126 If gentoo is not logged on, gentoo is still not started, and shows the number of times this has been viewed; 127 128 #! /Bin/bash129 #130 username = $1131 declare-I count = 0132 133 while true; do134 if who | grep "^ $ username" &>/dev/null; then135 echo "$ username is logged. "136 breakstmelse138 let count + + 139 echo" $ count $ username is not login. "140 fi141 sleep 3142 done143 144 example: 2145 146 #! /Bin/bash147 #148 declare-I count = 0149 username = $1150 151 if [$ #-lt 1]; then152 echo "At lease one argument. "153 exit 1154 fi155 156 if! Id $ username &>/dev/null; then157 echo "No such user. "158 exit 2159 fi160 161 until who | grep" ^ $ username "&>/dev/null; do162 let count ++ 163 echo" $ count $ username is not login. "164 sleep 3165 done166 echo" $ username is logged on. "167 168 169 special usage of while loop: 170 traverse each row of the file: 171 while read VARIABLE; do172 loop body 173 done </PATH/FROM/SOME_FILE174 175 example: find the username and ID of all users whose UID is an even number. 176 #! /Bin/bash177 #178 while read line; do179 userid =$ (echo $ line | cut-d:-f3) 180 if [$ [$ userid % 2]-eq 0]; then181 echo $ line | cut-d:-f1, 3182 fi183 done </etc/passwd184 185 special usage of 186 for loops: 187 for (expr1; expr2; expr3); do188 loop body 189 done190 191 expr1: Define control variables, and the initial value is assigned; 192 expr2: cyclic control condition; 193 entry condition: the control condition is "true" 194 exit condition: the control condition is "false" 195 expr3: correction control variable 197 198 example: Calculate the sum of all positive integers within 100; 199 200 Exercise 1: print the 9-9 multiplication table: 201 Exercise 2: pass a text file as a parameter Script to show all the even rows of the file, and display the row number before the row; 202 203 function: 204 function: function 205 encapsulates a piece of code with independent functions, and give the name; when it is used later, you can directly call the overall code by specifying the function name; 206 207 function: 208 code reuse: 209 Modular programming: 210 211 function usage: 212 Definition first: Compile the function code 213 and call it: Give the function name, and PASS Parameters as needed; 214 215 definition method: 216 (1) function f_name {217 function body 218} 219 (2) f_name () {220 function body 221} 222 call function: 223 f_name [argu1, argu2,...] 225 226 returned value of the custom function status: 227 return [#] 228 0: Success 229 1-255: Failure 230 Note: When the function code is executed, the function code stops running once return is encountered, function Return; 231 example: Previous Service script 232 233 #! /Bin/bash234 #235 prog = $ (basename $0) 236 lockfile = "/var/lock/subsys/$ prog" 237 # echo $ lockfile238 239 if [$ #-lt 1]; then240 echo "Usage: $ prog start | stop | restart | status "241 exit 1242 fi243 244 start () {245 if [-f $ lockfile]; then246 echo" $ prog is started yes. "247 else248 touch $ lockfile & echo" Starting $ prog OK... "| echo" Starting $ prog failed .. 249 fi250} 251 stop () {252 if [-f $ lockfile ]; Then253 rm-f $ lockfile & echo "Stop $ prog OK... "| echo" Stop $ prog failed .. "254 else255 echo" $ prog is stopped yet. "256 fi257} 258 restart () {259 if [-f $ lockfile]; then260 rm-f $ lockfile & touch $ lockfile & echo" Restart $ prog OK... "261 else262 touch $ lockfile & echo" $ prog is stopped, Starting $ prog OK... "263 fi264} 265 status () {266 if [-f $ lockfile]; then267 echo" Running... "26 8 else269 echo "Stopped... "270 fi271} 272 case $1 in274 start) 273 start276; 275 stop) 277 stop279; 278 restart) 280 restart282; 281 status) 283 status285 ;; 286 287 *) 288 echo "Usage: $ prog start | stop | restart | status" 289 exit 1290 esac291 example: Identify the parity of the user ID 292 #! /Bin/bash294 #295 evenid () {296 if [$ #-lt 1]; then297 return 1298 fi299 300 if! Id $1 &>/dev/null; then301 return 2302 fi303 304 userid = $ (id-u $1) 305 if [$ [$ userid % 2]-eq 0]; then306 echo "$1, Even user ID. "307 else308 echo" $1, Odd user ID. "309 fi310} 311 312 evenid root313 314 evenid315 echo $? 316 317 evenid rooooooot318 echo $? 319 320 Modular programming 321 example: 322 erection/root/bin/srv directory has two files: 323 (1) function file 324 (2) script File 325 326 for the script use configuration file 327 A file only defines the variable 328 script file source this variable defines the file 329 330 scope of the variable: 331 local variables: 332 local VARIABLE = value333 334 survival time: 335 function execution starts until function return ends;