linux编程脚本(二).pdf
《linux编程脚本(二).pdf》由会员分享,可在线阅读,更多相关《linux编程脚本(二).pdf(86页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第八章第八章#break_exam2.sh#!/bin/bash for(i=1;i=9;i+)do for(j=1;j=i;j+)do let temp=i*j if$temp-eq 7 then break fi echo-n$i*$j=$temp done echo done BREAK_E2.SH#break_exam3.sh#!/bin/bash for(i=1;i=9;i+)do for(j=1;j=i;j+)do let temp=i*j echo-n$i*$j=$temp done if$i-eq 6 then break fi echo done BREAK_E3.SH#br
2、eak_exam1.sh#!/bin/bash sum=0 for(i=1;i=100;i+)do let sum+=i if$sum-gt 1000 then echo 1+2+.+$i=$sum break fi done BREAK_EX.SH#continue_exam2.sh#!/bin/bash for(i=1;i=9;i+)do for(j=1;j=i;j+)do let temp=i*j if$temp-eq 7 then continue fi echo-n$i*$j=$temp done echo done CONTINU2.SH#continue_exam3.sh#!/b
3、in/bash for(i=1;i=9;i+)do if$i-eq 7 then continue fi for(j=1;j=i;j+)do let temp=i*j echo-n$i*$j=$temp done echo done CONTINU3.SH#continue_exam1.sh#!/bin/bash m=1 for(i=1;i 100;i+)do let temp1=i%7 if$temp1-ne 0 then continue fi echo-n$i let temp2=m%7 if$temp2-eq 0 then echo fi let m+done CONTINUE.SH#
4、for_exam6.sh#!/bin/bash for file in$(ls)do echo file:$file done FOR_EX10.SH#for_exam7#!/bin/bash for file in*do echo file:$file done FOR_EX11.SH#for_exam8.sh#!/bin/bash echo number of arguments is$#echo What you input is:for argument in$*do echo$argument done FOR_EX12.SH#for_exam9.sh#!/bin/bash echo
5、 number of arguments is$#echo What you input is:for argument do echo$argument done FOR_EX13.SH#for_exam10.sh#!/bin/bash for(integer=1;integer=5;integer+)do echo$integer done FOR_EXA2.SH#for_exam11.sh#!/bin/bash sum=0 for(i=1;i=100;i=i+2)do let sum+=i done echo sum=$sum FOR_EXA3.SH#for_exam12.sh#!/bi
6、n/bash LIMIT=5 for(a=1,b=5;a=1;i+)do echo Hello World!done FOR_EXA5.SH#for_exam2.sh#!/bin/bash for varible1 in 1.5 do echo Hello,Welcome$varible1 times done FOR_EXA6.SH#!/bin/bash sum=0 for i in 1.100.2 do let sum+=i done echo sum=$sum FOR_EXA7.SH#for_exam4#!/bin/bash sum=0 for i in$(seq 1 2 100)do
7、let sum+=i done echo sum=$sum FOR_EXA8.SH#for_exam5#!/bin/bash for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday do echo$day done FOR_EXA9.SH#for_exam1.sh#!/bin/bash for varible1 in 1 2 3 4 5 do echo Hello,Welcome$varible1 times done FOR_EXAM.SH#nested_loop_exam3.sh:#!/bin/bash for
8、(i=1;i=8;i+)do for(j=1;j=8;j+)do total=$($i+$j)tmp=$($total%2)if$tmp-eq 0;then echo-e-n 03347m else echo-e-n 03340m fi done echo done NESTED_2.SH#nested_loop_exam3.sh:#!/bin/bash i=1 while($i=9)do j=1 while($j=$i)do echo-n*let j+done let i+echo done NESTED_3.SH#neSted_loop_exam1.sh#!/bin/bash for(i=
9、1;i=9;i+)do for(j=1;j=i;j+)do let temp=i*j echo-n$i*$j=$temp done echo done NESTED_L.SH#no_break_exam.sh#!/bin/bash sum=0 for(i=1;i=100;i+)do let sum+=i done echo 1+2+.+100=$sum NO_BREAK.SH#select_exam2.sh#!/bin/bash echo What is your favourite color?select color do break done SELECT_2.SH#select_exa
10、m1.sh#!/bin/bash echo What is your favourite color?select color in red blue green white black do break done echo You have selected$color SELECT_E.SH#while_exam2.sh#!/bin/bash echo Please input the num(1-10)read num until$num=4 do if$num-lt 4 then echo Too small.Try again!read num elif$num-gt 4 then
11、echo To high.Try again read num else exit 0 fi done echo Congratulation,you are right!UNTIL_E2.SH#until_exam1.sh#!/bin/bash i=0 until$i-gt 5 do let square=i*i echo$i*$i=$square let i+done UNTIL_EX.SH#while_exam2.sh#!/bin/bash sum=0 i=1 while(i=100)do let sum+=i let i+=2 done echo sum=$sum WHILE_E2.S
12、H le_exam3.sh#!/bin/bash echo Please input the num(1-10)read num while$num!=4 do if$num-lt 4 then echo Too small.Try again!read num elif$num-gt 4 then echo To high.Try again read num else exit 0 fi done echo Congratulation,you are right!WHILE_E3.SH#while_exam4.sh#!/bin/bash echo Please input the num
13、 read num factorial=1 while$num-gt 0 do let factorial=factorial*num let num-done echo The factorial is$factorial WHILE_E4.SH#while_exam5.sh#!/bin/bash echo Please input the num:read num signal=0 while$signal!=1 do if$num-lt 4 then echo Too small.Try again!read num elif$num-gt 4 then echo To high.Try
14、 again read num else signal=1 echo Congratulation,you are right!fi done WHILE_E5.SH#while_exam6.sh#!/bin/bash echo Please input the num read num sum=0 i=1 signal=0 while$signal!=1 do if$i-eq$num then let signal=1 let sum+=i echo 1+2+.+$num=$sum else let sum=sum+i let i+fi done WHILE_E6.SH#while_exam
15、7.sh#!/bin/bash echo number of arguments is$#echo What you input is:while$*!=do echo$1 shift done WHILE_E7.SH#while_exam1.sh#!/bin/bash int=1 while($int=5)do echo$int let int+done WHILE_EX.SH 第九章第九章#!/bin/bash for n in 0 1 2 3 4 5 do echo BASH_VERSINFO$n=$BASH_VERSINFO$n done BASHVER.SH#!/bin/bash v
16、ar1=20 var2=3.14159 var3=echo scale=5;$var1 2|bc var4=echo scale=5;$var3*$var2|bc echo The area of this circle is:$var4 CALARE.SH#!/bin/bash(a=2009)echo The initial value of a is:$a(a+)echo After a+,the value of a is:$a(+a)echo After+a,the value of a is:$a(a-)echo After a-,the value of a is:$a(-a)ec
17、ho After-a,the value of a is:$a CSTYLE.SH#!/bin/bash variable1=12 variable2=5 result=$(variable1*variable2)echo result=$result DOUBLEPA.SH#!/bin/bash S01_name=Li Hao S01_dept=Computer S01_phone=025-83481010 S01_rank=5 S02_name=Zhang Ju S02_dept=English S02_phone=025-83466524 S02_rank=8 S03_name=Zhu
18、Lin S03_dept=Physics S03_phone=025-83680010 S03_rank=3 PS3=Pls.select the number of student:select stunum in S01 S02 S03 do name=$stunum_name dept=$stunum_dept phone=$stunum_phone rank=$stunum_rank echo BASIC INFORMATION OF NO.$stunum STUDENT:echo NAME:$!name echo DEPARTMENT:$!dept echo PHONE:$!phon
19、e echo RANK:$!rank break done INDIREC2.SH#!/bin/bash filename=$1 column=$2 tempvar=$!column echo$column echo$2 echo$filename awk BEGIN FS=,print NR,$tempvar$filename INDIREC3.SH#!/bin/bash variable1=variable2 variable2=Hadoop echo varialbe1=$variable1 eval tempvar=$variable1 echo tempvar=$tempvar ec
20、ho Indirect ref variable1 is:$!variable1 INDIRECT.SH#!/bin/bash echo-n What is your name?read echo Your name is$REPLY echo-n What is the name of your father?read fname echo Your fathers name is$fname echo But the$REPLY is$REPLY READREPL.SH#!/bin/bash count=1 MAX=5 while$SECONDS-le$MAX;do echo This i
21、s the$count time to sleep.let count=$count+1 sleep 2 done echo The running time of this script is$SECONDS RUNSEC.SH#!/bin/bash echo Pls.choose your profession?select var in Worker Doctor Teacher Student Other do echo The$REPLY is$REPLY.echo Your preofession is$var.break done SELECTRE.SH#!/bin/bash T
22、MOUT=3 echo What is your name?read fname if -z$fname;then fname=(no answer)fi echo Your name is$fname TIMEDREA.SH#!/bin/bash variable1=2009 variable2=$variable1+1 echo variable2=$variable2 let variable3=$variable1+1 echo variable3=$variable3 declare-i variable4 variable4=$variable1+1 echo variable4=
23、$variable4 VARTYPE.SH 第十章第十章#!/bin/bash echo The number of arguments passed to this script:$#eval echo$#echo-n The last argument is:eval echo$#EVALPOS.SH#!/bin/bash while read NAME VALUE do eval$NAME=$VALUE#$NAME=$VALUE done&1 9&2 exec&loggg ls z*date exec 1&8 2&9 8&-9&-echo-echo Close FD 8 and 9:ls
24、 z*date EXECERR.SH#!/bin/bash exec 8&0 exec hfile read a read b echo-echo$a echo$b echo Close FD 8:exec 0&8 8&1 exec loggg echo Output of date command date echo Output of df command df exec 1&8 8&-echo-echo Output of date command date echo Output of df command df EXECOUT.SH#refor.sh 脚本:for 循环的重定向#!/
25、bin/bash#将 ls/etc 的结果写到 loggg 文件中 ls/etc loggg#计算 loggg 文件的最大行数,并赋给 maxline 变量#这是与 while 和 until 循环最大的区别#灵活运用了输入重定向符号,类似用法可以参见图 10-25 的例子 maxline=$(wc-l loggg)#搜索 loggg 文件中与 rc.d 所匹配的行,输出匹配行的行数 for filename in seq$maxline#利用 seq 命令产生循环参数 do read filename#按行读取 loggg 中的数据#let count+=1#for 循环中需要有 if 语句
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linux 编程 脚本
限制150内