Hadoop搭建笔记(15)

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

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

27、shell中的文件引入

(在一个文件里引入另一个文件的代码)和其他语言一样,shell也可以包含外部脚本,这样可以很方便封装一些公用的代码作为一个独立的文件。

shell文件包含的语法格式如下:

. filename    #注意点符号(.)和文件名中间有一空格

source filename

定义两个文件,把一个文件作为公用文件;把变量或者方法,作为常用的封装起来,另一个文件来调用这个封装起来的

####vi /home/shell/1.sh

#!/bin/bash

name=’123’

####vi /home/shell/2.sh

. /home/shell/1.sh

#source /home/shell/1.sh

echo $name

 

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

#!/bin/bash

class=1701A

name=zhangsan

 

function fun1(){

echo “i am in 1.sh”

}

(vi ./1.sh这个文件不用做可执行权限,要把这个文件引向另一个文件,不真正执行)

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

#!/bin/bash

source /home/shell/1.sh

echo $class

echo $name

fun1

引入文件方法:

1、source /home/shell/1.sh  (bshell里面的)

2、. /home/shell/1.sh   (cshell里面的)

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

[root@hadoop01 shell]# 2.sh

1701A

zhangsan

i am in 1.sh

 

28、shell中脚本的调试

任何语编程都有对应的调试工具,如Java有debug、MySQL有调试工具、is有调试工具等。

shell的语法检测:相当于Java的编译

1、 shell语法检测: sh n ./test.sh    (sh是/bin/sh   是系统提供的可执行脚本)

2、 shell的普通调试: sh –x ./test.sh

#!/bin/bash

echo “welcome to shell debug”

for i in 1 2 3 4 5 6

do

echo $i

done

vi ./test.sh

which sh

sh –n ./test.sh   (检测脚本是否有语法错误)

sh –x ./test.sh    (普通的调试)

执行sh –x ./test.sh该语句后如下:

+echo‘welcome to shell debug’

welcome to shell debug

进入调试模式后,shell依次执行读入的语句,产生的输出中有的带加号,有的不带,带加号的表示该条语句是shell执行的;不带加号表示该语句是shell产生的输出。(带加号的是依据代码,不带加号的是代码的输出)

3、 shell 中断调试:

在shell中添加一个睡眠,保证可以有时间中断调试sleep 3 睡眠3秒执行下一个语句。

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

#!/bin/bash

echo "welcome to shell debug"

for i in 1 2 3 4 5 6

done

[root@hadoop01 shell]# which sh

/bin/sh

检测有无语法上的错误:

[root@hadoop01 shell]# sh -n ./test.sh

./test.sh: line 5: syntax error near unexpected token `done'

./test.sh: line 5: `done'

(在echo附近有语法上的错误)改正:

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

#!/bin/bash

echo "welcome to shell debug"

for i in 1 2 3 4 5 6

do

echo $i

done

[root@hadoop01 shell]# sh -n ./test.sh  (不报错)

进行普通调试:

[root@hadoop01 shell]# sh -x ./test.sh

+ echo 'welcome to shell debug'

welcome to shell debug

+ for i in 1 2 3 4 5 6

+ echo 1

1

+ for i in 1 2 3 4 5 6

+ echo 2

2

+ for i in 1 2 3 4 5 6

+ echo 3

3

+ for i in 1 2 3 4 5 6

+ echo 4

4

+ for i in 1 2 3 4 5 6

+ echo 5

5

+ for i in 1 2 3 4 5 6

+ echo 6

6

打印出来相应的信息,带加号的是依据程序、依据代码,不带加号的是上一句的输出,如果样本量大,不好找,不能进行交互

终断调试:

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

#!/bin/bash

echo "welcome to shell debug"

for i in 1 2 3 4 5 6

do

echo $i

sleep 2

done

按Ctrl +Z :停止

没有调试完,按f g:继续

在调试过程中可以按Ctrl+Z中断调试,观察结果,然后再按fg键(先按f再按g键)继续调试即可

以上两种交互性都不是很好

4、 使用调试工具-bashdb

功能:类似于GDB的调试工具,可以完成shell脚本的断点设置,单步执行,变量观察等许多功能。

场合:脚本较大时,通过-x参数调试时已不方便时

用法:

bashdb-c script.sh

bashdb script.sh

bashdb --debugger script.sh

借助bashdb进行调试:

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

#!/bin/bash

echo "welcome to shell bashdb debug -------start"

max=3

for ((i=0;i<max;i++))

do

nowdate=`date -d "-$i day" +%Y-%m-%d`

echo $nowdate

done

echo "welcome to shell bashdb debug -------end"

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

[root@hadoop01 shell]# ./bs.sh  (先执行一次,看bs.sh语句是否有问题)

welcome to shell bashdb debug -------start

2018-04-24

2018-04-23

2018-04-22

welcome to shell bashdb debug -------end

无问题,开始调试:

由于没有配置环境变量,源码安装在哪里,就在哪里cd /usr/local/bashdb-4.4-0.92/

[root@hadoop01 shell]# cd /usr/local/bashdb-4.4-0.93/

[root@hadoop01 bashdb-4.4-0.93]# ./bashdb  (可以Tab出来,没问题)

[root@hadoop01 bashdb-4.4-0.93]# ./bashdb --debugger /home/shell/bs.sh

bash debugger, bashdb, release 4.4-0.93

 

Copyright 2002-2004, 2006-2012, 2014, 2016-2017 Rocky Bernstein

This is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

 

/usr/share/share/bashdb/lib/fns.sh: line 43: conditional binary operator expected

/usr/share/share/bashdb/lib/fns.sh: line 43: syntax error near `$1'

/usr/share/share/bashdb/lib/fns.sh: line 43: `[[ -v $1 ]]'

(/home/shell/bs.sh:3):

3:echo "welcome to shell bashdb debug -------start"

bashdb<0>  (可以输入下面的常用命令

常用命令:

l

列出当前行上下各5行,总共10行

q|quit

退出  结束

h

帮助

/for/

向后搜索字符串for

?for?

向前搜索字符串for

x 1+2

计算算数表达式的值

!! ls -laRt

执行shell命令

n

执行下一条语句

s 4

单步支持4次,如遇到函数则进入函数里面

b 4

在行号4处设置断点

del 4

删除行号为4的断点

c 10

一直执行到行号10处

R|run

重新执行当前调试脚本

finish

执行到程序最后

c

继续运行

print $i

查看当前的环境变量

xshell操作:

复制:Ctrl + insert

粘贴:Shift + insert

要退出到本地shell:Ctrl + Alt +

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Demoatnes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值