linux shell bash 精彩脚本示例,Linux Shell Bash 精彩脚本示例(5)

273 then # If alive, then

274 array[$i]=. #+ represent the cell as a period.

275 else

276 array[$i]="_" # Otherwise underscore

277 fi #+ (which will later be converted to space).

278 let "i += 1"

279 done

280

281

282 # let "generation += 1" # Increment generation count.

283 # Why was the above line commented out?

284

285

286 # Set variable to pass as parameter to "display" function.

287 avar=`echo ${array[@]}` # Convert array back to string variable.

288 display "$avar" # Display it.

289 echo; echo

290 echo "Generation $generation - $alive alive"

291

292 if [ "$alive" -eq 0 ]

293 then

294 echo

295 echo "Premature exit: no more cells alive!"

296 exit $NONE_ALIVE # No point in continuing

297 fi #+ if no live cells.

298

299 }

300

301

302 # =========================================================

303

304 # main ()

305

306 # Load initial array with contents of startup file.

307 initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\

308 sed -e 's/\./\. /g' -e 's/_/_ /g'` )

309 # Delete lines containing '#' comment character.

310 # Remove linefeeds and insert space between elements.

311

312 clear # Clear screen.

313

314 echo # Title

315 echo "======================="

316 echo " $GENERATIONS generations"

317 echo " of"

318 echo ""Life in the Slow Lane""

319 echo "======================="

320

321

322 # -------- Display first generation. --------

323 Gen0=`echo ${initial[@]}`

324 display "$Gen0" # Display only.

325 echo; echo

326 echo "Generation $generation - $alive alive"

327 # -------------------------------------------

328

329

330 let "generation += 1" # Increment generation count.

331 echo

332

333 # ------- Display second generation. -------

334 Cur=`echo ${initial[@]}`

335 next_gen "$Cur" # Update & display.

336 # ------------------------------------------

337

338 let "generation += 1" # Increment generation count.

339

340 # ------ Main loop for displaying subsequent generations ------

341 while [ "$generation" -le "$GENERATIONS" ]

342 do

343 Cur="$avar"

344 next_gen "$Cur"

345 let "generation += 1"

346 done

347 # ==============================================================

348

349 echo

350

351 exit 0 # END

352

353

354

355 # The grid in this script has a "boundary problem."

356 # The the top, bottom, and sides border on a void of dead cells.

357 # Exercise: Change the script to have the grid wrap around,

358 # + so that the left and right sides will "touch,"

359 # + as will the top and bottom.

360 #

361 # Exercise: Create a new "gen0" file to seed this script.

362 # Use a 12 x 16 grid, instead of the original 10 x 10 one.

363 # Make the necessary changes to the script,

364 #+ so it will run with the altered file.

365 #

366 # Exercise: Modify this script so that it can determine the grid size

367 #+ from the "gen0" file, and set any variables necessary

368 #+ for the script to run.

369 # This would make unnecessary any changes to variables

370 #+ in the script for an altered grid size.

例子 A-11. "Game of Life"的数据文件

1 # This is an example "generation 0" start-up file for "life.sh".

2 # --------------------------------------------------------------

3 # The "gen0" file is a 10 x 10 grid using a period (.) for live cells,

4 #+ and an underscore (_) for dead ones. We cannot simply use spaces

5 #+ for dead cells in this file because of a peculiarity in Bash arrays.

6 # [Exercise for the reader: explain this.]

7 #

8 # Lines beginning with a '#' are comments, and the script ignores them.

9 __.__..___

10 ___._.____

11 ____.___..

12 _._______.

13 ____._____

14 ..__...___

15 ____._____

16 ___...____

17 __.._..___

18 _..___..__

+++

下面的两个脚本是由多伦多大学的Mark Moraes编写的. 请参考附件文件"Moraes-COPYRIGHT", 详细的指明了授权与约定.

--------------------------------------------------------------------------------

例子 A-12. behead: 去掉信件与新消息的头

1 #! /bin/sh

2 # Strips off the header from a mail/News message i.e. till the first

3 # empty line

4 # Mark Moraes, University of Toronto

5

6 # ==> These comments added by author of this document.

7

8 if [ $# -eq 0 ]; then

9 # ==> If no command line args present, then works on file redirected to stdin.

10 sed -e '1,/^$/d' -e '/^[ ]*$/d'

11 # --> Delete empty lines and all lines until

12 # --> first one beginning with white space.

13 else

14 # ==> If command line args present, then work on files named.

15 for i do

16 sed -e '1,/^$/d' -e '/^[ ]*$/d' $i

17 # --> Ditto, as above.

18 done

19 fi

20

21 # ==> Exercise: Add error checking and other options.

22 # ==>

23 # ==> Note that the small sed script repeats, except for the arg passed.

24 # ==> Does it make sense to embed it in a function? Why or why not?

例子 A-13. ftpget: 通过ftp下载文件

1 #! /bin/sh

2 # $Id: ftpget,v 1.2 91/05/07 21:15:43 moraes Exp $

3 # Script to perform batch anonymous ftp. Essentially converts a list of

4 # of command line arguments into input to ftp.

5 # ==> This script is nothing but a shell wrapper around "ftp" . . .

6 # Simple, and quick - written as a companion to ftplist

7 # -h specifies the remote host (default prep.ai.mit.edu)

8 # -d specifies the remote directory to cd to - you can provide a sequence

9 # of -d options - they will be cd'ed to in turn. If the paths are relative,

10 # make sure you get the sequence right. Be careful with relative paths -

11 # there are far too many symlinks nowadays.

12 # (default is the ftp login directory)

13 # -v turns on the verbose option of ftp, and shows all responses from the

14 # ftp server.

15 # -f remotefile[:localfile] gets the remote file into localfile

16 # -m pattern does an mget with the specified pattern. Remember to quote

17 # shell characters.

18 # -c does a local cd to the specified directory

19 # For example,

20 # ftpget -h expo.lcs.mit.edu -d contrib -f xplaces.shar:xplaces.sh \

21 # -d ../pub/R3/fixes -c ~/fixes -m 'fix*'

22 # will get xplaces.shar from ~ftp/contrib on expo.lcs.mit.edu, and put it in

23 # xplaces.sh in the current working directory, and get all fixes from

24 # ~ftp/pub/R3/fixes and put them in the ~/fixes directory.

25 # Obviously, the sequence of the options is important, since the equivalent

26 # commands are executed by ftp in corresponding order

27 #

28 # Mark Moraes , Feb 1, 1989

29 #

30

31

32 # ==> These comments added by author of this document.

33

34 # PATH=/local/bin:/usr/ucb:/usr/bin:/bin

35 # export PATH

36 # ==> Above 2 lines from original script probably superfluous.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值