Hadoop搭建笔记(14)

本文是我学习Hadoop搭建过程中的各种笔记,内容来自于各种公开的教程,起点非常低,从Linux基础开始,直至在PC上搭建Hadoop成功,是真正的从零开始。

感谢过程中帮助我的各位认识的和不认识的老师。

25、shell中的循环、casein、continue、break

循环:for、while、until

for循环:

for var in item1 item2…itemN    item1 item2…itemN指:循环的一个列表】

do

command1

command2

    ……

    commandN      【指多个命令】

done      【指结束】

打印1到5:for i in 1 2 3 4 5 ; do echo $i ; done

[root@hadoop01 shell]# for i in 1 2 3 4 5 ; do echo $i ; done

1

2

3

4

5

for循环:

#!/bin/bash

for val in `seq 1 20`   (虚拟化增长1到20

do

echo “the value is:”$val

done

 

[root@hadoop01 shell]# vi ./for.sh

[root@hadoop01 shell]# chmod 744 ./for.sh

出来的结果是:120

[root@hadoop01 shell]# for.sh

the value is:”1

the value is:”2

the value is:”3

the value is:”4

the value is:”5

the value is:”6

the value is:”7

the value is:”8

the value is:”9

the value is:”10

the value is:”11

the value is:”12

the value is:”13

the value is:”14

the value is:”15

the value is:”16

the value is:”17

the value is:”18

the value is:”19

the value is:”20

 

循环从一个目录下面创建一堆文件:

[root@hadoop01 shell]# vi ./for.sh

#!/bin/bash

 

for i in `seq 1 20`

do

touch "/home/shell/${i}.log"

done

此时ll:

[root@hadoop01 shell]# ll

total 20

-rwxr--r--. 1 root root 287 Apr 23 21:04 array.sh

-rw-r--r--. 1 root root  32 Apr 23 17:19 firstshell

-rwxr--r--. 1 root root  73 Apr 24 15:22 for.sh

-rwxr--r--. 1 root root 267 Apr 24 15:01 if.sh

-rwxr--r--. 1 root root 137 Apr 23 20:06 varible.sh

[root@hadoop01 shell]# for.sh

for.sh不报错,再次ll:

[root@hadoop01 shell]# ll

total 20

-rw-r--r--. 1 root root   0 Apr 24 15:25 10.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 11.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 12.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 13.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 14.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 15.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 16.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 17.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 18.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 19.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 1.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 20.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 2.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 3.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 4.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 5.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 6.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 7.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 8.log

-rw-r--r--. 1 root root   0 Apr 24 15:25 9.log

-rwxr--r--. 1 root root 287 Apr 23 21:04 array.sh

-rw-r--r--. 1 root root  32 Apr 23 17:19 firstshell

-rwxr--r--. 1 root root  71 Apr 24 15:25 for.sh

-rwxr--r--. 1 root root 267 Apr 24 15:01 if.sh

-rwxr--r--. 1 root root 137 Apr 23 20:06 varible.sh

此时就循环创建了20个.log文件

while循环:

while condition   【控制体】

do

command

……

commandN    【多个命令】

done

做一个1到20的递增:

[root@hadoop01 shell]# vi ./while.sh

#!/bin/bash

i=1     i从1开始)

while(($i < 21))

do

echo the value is:”$i    (把i值取出来)

i=`expr $i + 1`    (做递增的操作,还可以这么写: let”i++”

done

[root@hadoop01 shell]# chmod 744 ./while.sh

[root@hadoop01 shell]#

[root@hadoop01 shell]# while.sh

the value is:1

the value is:2

the value is:3

………………

the value is:20

出来的结果和上面的for一样

case匹配:

必须有in,一个模式结束必须用双 ;; 号, 用esac来结束整个case case的翻转)结束标识。

case 值 in     case等于一个值,in】

模式1)

     command1

     command2

     …

commandN

;;

模式2)

     command1

     command2

     …

commandN

;;

esac

(整个shell)同时也支持continue(跳出当前)和break(跳出所以循环)跳出循环。

 

综合操作:case in、continue、break

[root@hadoop01 shell]# vi ./case.sh

#!/bin/bash

while:

do

echo “请您输入一个1-5之间的数字:”   echo:接受到一个输入)

read anum    read:需要接受控制台的输入,任意定义名字: anum)

case $anum in  case:匹配这个值,上面接到的anum变量,匹配到这里面)

1|2|3|4|5)   (管道,获得关系)

echo “恭喜您,您输入的数字为:${anum}  1-5之间”   (打印:echo)

break   (结束整个循环)

;;

*)   (否则的话,输入的是其他东西,下面echo)

echo “您输入的数字为:${anum} ,数字不在1-5之间,请再次输入:

continue   continue只能结束[echo “您输入的数字为:${anum} ,数字不在1-5之间,请再次输入:]这次的循环,不能结束全部的循环)

;;

esac  

done   (整个结束掉)

[root@hadoop01 shell]# chmod 744 ./case.sh

[root@hadoop01 shell]# case.sh

请您输入一个1-5之间的数字:

6

您输入的数字为:6 ,数字不在1-5之间,请再次输入:

请您输入一个1-5之间的数字:

9

您输入的数字为:9 ,数字不在1-5之间,请再次输入:

请您输入一个1-5之间的数字:

10

您输入的数字为:10 ,数字不在1-5之间,请再次输入:

请您输入一个1-5之间的数字:

3

恭喜您,您输入的数字为:3  在1-5之间

 

26、shell中的方法

shell中函数的定义格式:

[ function ] funname()     function可以省略,funname()函数名】

{

   action;

   [return int]

}

注意:

1、 可以带function fun() 定义,也可以直接fun()定义,不带任何参数

2、 参数返回值,可以显示加:return 返回,如果不加,将以最后一条命令运输结果,作反返回值。return后跟数值n(0-255)

调用无参数的,不带返回值的:

[root@hadoop01 shell]# vi ./firstfunc.sh  (注意:单词拼写错了,应该是:firstfunction

#!/bin/bash

function fun1(){

echo “this is my first function”

}    (定义上了,怎么调用?下面是调用方法)  

echo 调用方法之前

fun1  (调用方法)

echo 调用方法之后

[root@hadoop01 shell]# chmod 744 ./firstfunc.sh

[root@hadoop01 shell]# firstfunc.sh

调用方法之前

this is my first function

调用方法之后

 

带返回值的:

[root@hadoop01 shell]# vi ./wr.sh

#!/bin/bash

fun1(){

echo “请输入两个数字,我们将为您计算和

echo “请输入一个数字:

read anum

echo “请输入第二个数字:

read bnum

return $(($anum + $bnum))

}

fun1    (调用方法)

echo 您输入的两个数之和为:”$?    (一定是问号,取上一行方法的返回值)

[root@hadoop01 shell]# chmod 744 ./wr.sh

[root@hadoop01 shell]# wr.sh

请输入两个数字,我们将为您计算和

请输入一个数字:

12

请输入第二个数字:

16

您输入的两个数之和为:28

带参数的:

[root@hadoop01 shell]# vi ./wp.sh

#!/bin/bash

fun1(){

echo “第一个参数:”$1

echo “第二个参数:”$2

echo “第九个参数:”$9

echo “第十个参数:”$10   (取出来的是10这个值,而不是参数的值)

echo “第十个参数:”${10}   (参数个数大于两位数,要加花括号,否则会出现错误)

echo “所有参数:”$*

echo “参数个数:”$#

}

fun1 1 2 3 4 5 6 7 8 100 900

[root@hadoop01 shell]# chmod 744 ./wp.sh

[root@hadoop01 shell]# wp.sh

第一个参数:1

第二个参数:2

第九个参数:100

第十个参数:10

第十个参数:900

所有参数:1 2 3 4 5 6 7 8 100 900

参数个数:10

从命令端传递脚本(从外部创建参数):

[root@hadoop01 shell]# vi ./wpar.sh

#!/bin/bash

echo '命令端传递过来的所有参数:'$*

echo '命令端传递过来的第一个参数:'$1

echo '命令端传递过来的第二个参数:'$2

echo '命令端传递过来的参数长度:'$#

 

[root@hadoop01 shell]# chmod 744 ./wpar.sh

[root@hadoop01 shell]# wpar.sh 100 20 50 99

命令端传递过来的所有参数:100 20 50 99

命令端传递过来的第一个参数:100

命令端传递过来的第二个参数:20

命令端传递过来的参数长度:4

注意:

参数用空格分开

几个特殊符号用来出来参数

参数处理

说明

$#

传递到脚本的参数个数

$*

以一个单字符串显示所有向脚本传递的参数

$@

$@与$*相同,但使用时加引号,并在引号中返回每个参数

$?


猜你喜欢

转载自blog.csdn.net/zxqjinhu/article/details/80407939