本文是我学习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 +