cd ./
当前目录
cd ../
返回上一级目录
cd -
返回上一次操作的目录
cd ~
回到root用户的家目录
clear
清屏
ls(list)查看命令(相当于windows的dir)
ls
列出当前目录下的所有目录
ls /home/bigdata
ls -l(等于ll,等于-l)
列出当前目录下的所有目录的详细信息
pwd
//显示当前目录
修改VMware的操作界面模式:
vi /etc/inittab
//修改id后面的数字3或5,保存退出,重启生效,无需使用source /etc/inittab
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:5:initdefault:
2、Hadoop三个版本
(1)Apache Hadoop(所有其他版本都是基于它开发的)
(2)CDH(Cloudera's Distribute Including Apache Hadoop )收费,商业
(3)HDP(Hortonworks Data Paltform)
在中国的互联网公司多用CDH,政府是使用国内大数据平台公司的Hadoop
3、Hadoop环境支持
(1)JDK
(2)Hadoop
(公用)
查看防火墙状态:
service iptables status
//查看防火墙状态
# service iptables stop //关闭防火墙
# chkconfig iptables off //保存防火墙开机不启动
主机名
# vi /etc/sysconfig/network
//永久性修改主机名
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
6c:0f:42:8d:0a:a8:b5:5f:c3:61:8a:6a:51:55:f7:c0 root@q1
The key's randomart image is:
+--[ RSA 2048]----+
| ....o |
| . . o.Eo |
|. o. = . . |
|...+ * o |
|..o o = S |
| ... . + o |
|.. . . |
|. |
| |
+-----------------+
免密操作:
# ssh-copy-id q1 需要免密的机子 //先确定要对这台虚拟机免密,然后输入密码
Now try logging into the machine, with "ssh 'q2'" //免密成功
# ssh-copy-id q2
# ssh-copy-id q3
检验免密是否成功:
# ssh q1
# ssh q2
# ssh q3
[root@q1 ~]# ssh q3
Last login: Fri Jul 21 14:36:49 2017 from 192.168.10.254
[root@q3 ~]# exit
logout
Connection to q3 closed.
3.VMware和dos命令都能ping通
给q1虚拟机上传hadoop,jdk安装包
解压安装包
修改环境变量
# vi /etc/profile
# vi /etc/bashrc
# source /etc/profile
错误执行方式(传递的参数为 1):
/bin/sh t2.sh "a b c d"
/bin/sh t2.sh "a,b,c,d"
结果:
[root@h data]# /bin/sh t2.sh "a" "b" "c" "d"
25544
t2.sh
a
c
a b c d
a b c d
显示所有参数:a b c d
0
4
3)$*和$@的区别?
eg1:
echo $*
echo $@
执行结果:
[root@h data]# /bin/sh ./t1.sh "a" "b" "c" "d"
a b c d
a b c d
eg2:
echo "--------\例一:$*--------"
for i in "$*";do
echo $i
done
echo "--------\例二:$@--------"
for i in "$@";do
echo $i
done
执行结果:
[root@h data]# /bin/sh ./t2.sh "a" "b" "c" "d"
--------\例一:a b c d--------
a b c d
--------\例二:a b c d--------
a
b
c
d
echo "----------------< = >----------------------"
if [ $a = $b ]
then
echo "$a = $b :true a 等于 b"
echo "----------------< != >----------------------"
if [ $a != $b ]
then
echo "$a != $b :true a 不等于 b"
else
echo "$a != $b :false a 等于 b"
fi
echo "------------------<-z>----------------------"
if [ -z $a ]
then
echo "-z $a : string length is zero 字符串长度为0"
else
echo "-z $a : string length is not zero 字符串长度不为0"
fi
echo "----------------< -n >-----------------------"
if [ -n $a ]
then
echo "-n $a : string length is not zero 字符串长度不为0"
else
echo "-n $a : string length is zero 字符串长度为0"
fi
echo "-----------------< str >----------------------------"
if [ $a ]
then
echo "string is not empty 字符串不为空"
else
echo "string is empty 字符串长度为空"
fi
if [ $c ]
then
echo "string is not empty 字符串不为空"
else
echo "string is empty 字符串长度为空"
fi
执行结果:
[root@hw data]# chmod +x ./b4.sh
[root@hw data]# ./b4.sh
----------------< = >----------------------
hcs = hsch :false a 不等于 b
----------------< != >----------------------
hcs != hsch :true a 不等于 b
------------------<-z>----------------------
-z hcs : string length is not zero 字符串长度不为0
----------------< -n >-----------------------
-n hcs : string length is not zero 字符串长度不为0
-----------------< str >----------------------------
string is not empty 字符串不为空
string is empty 字符串长度为空
echo -e "OK!\n"
echo "It is a test"
输出:
OK!
It is a test
(4)显示不换行
echo -e "OK!\c"
echo "It is a test"
输出:
OK!It si a test
(5)显示结果重定向至文件
eg:
[root@h data]# touch a.log
[root@h data]# ls
a.log
>表示原样输出字符串覆盖原有文件内容
[root@h data]# echo "my name is Lucy" > a.log
[root@h data]# cat a.log
my name is Lucy
[root@h data]# echo "how are you" > a.log
[root@h data]# cat a.log
how are you
>>表示追加不覆盖
[root@h data]# echo "my name is Lucy" >> a.log
[root@h data]# cat a.log
how are you
my name is Lucy
eg:
运算符:
num1=100
num2=100
if [ ${num1} -eq ${num2} ]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
数值测试:
num1=100
num2=100
if test ${num1} -eq ${num2}
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
结果:
The two numbers are equal!
(2)字符串测试
参数
说明
=
等于则为真
!=
不相等则为真
-z 字符串
字符串长度伪则为真
-n 字符串
字符串长度不伪则为真
eg:
str1="jiaomeiqi"
str2="cdsjkv"·
if test ${str1} = ${str2}
then
echo 'The two strings are equal!'
else
echo 'The two strings are not equal!'
fi
eg:
cd /home/data
if test -e ./a.sh
then
echo 'The file already exists!'
else
echo 'The file does not exists!'
fi
结果:
The file already exists!
此外,Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:“-a”最高,“!”次之,“-o”最低。
eg:
cd /home
if test -e ./data -o ./tmp
then
echo 'One file exists at least!'
else
echo 'Both dose not exists!'
fi
结果:
One file exists at least!
12、流程控制语句:
(1)if判断(结束用fi)
if判断:
语法:
if 条件
then 输出
fi
eg:
nu=2
nuu=2
if [ ${nu} -eq ${nuu} ]
then
echo `expr ${nu} \* ${nuu}`
fi
if-else判断:
语法:
if 条件
then 输出
else 输出
fi
eg:
cd /home/data
if test -e ./a.sh
then
echo 'The file already exists!'
else
echo 'The file does not exists!'
fi
if then- else -if else判断:
if 条件
then 输出
elif 条件
then 输出
else 输出
fi
eg 1:
#!/bin/bash
a=1
b=2
if [ $a == $b ]
then
echo "a等于b"
elif [ $a -lt $b ]
then
echo "a小于b"
elif [ $a -gt $b ]
then
echo "a大于b"
else
echo "不符合以上所有判断"
fi
eg 2:(if与test命令结合使用) ----- test测试:数值测试可用[];字符串测试必须用{}
n1=$[2*4]
n2=$[5+3]
if test $[n1] -eq $[n2]
then
echo "n1等于n2"
else
echo "两数字不一样"
fi
(2)for循环
语法:
for variable in item1 item2 item3....
do
输出
输出
....
输出
done
一行:
for variable in item1 item2 item3....; do 输出;输出;....;输出;done
in后面可以跟任何替换、字符串、文件
eg:
echo "--------\例一:$*--------"
for i in "$*";do
echo $i
done
echo "--------\例二:$@--------"
for i in "$@";do echo $i;done
执行结果:
[root@h data]# /bin/sh ./t2.sh "a" "b" "c" "d"
--------\例一:a b c d--------
a b c d
--------\例二:a b c d--------
a
b
c
d
无限循环
for((;;))
(3)while
通常用于处理大量的命令,或是从输入文件中读取数据信息;(命令通常指测试条件)
格式:
while 判断条件
do
输出
done
eg 1:
#!/bin/bash
i=1
while(($i<=5))
do
echo "$i"
let "i++"
done
无限循环:
while true
do
输出
done
eg 2:
echo "What's your name?"
while read name
do
echo "My name is ${name}!"
done
(4)until
until 循环执行一系列命令直至条件为true时停止,until循环与while循环 循环在处理方式上刚好相反,一般while循环由于until循环,但在某些时候,也只是极少数情况下,until循环更加有用
语法:(循环至少执行一次)
until 条件
do
输出
done
条件:一般为条件表达式,如果返回值为false,则继续执行循环体内的语句,否则跳出循环
eg:(使用until命令输出数字)
echo "-----------until循环-------------------"
a=3
until [ ! $a -lt 10 ]
do
echo "${a}"
a=`expr $a + 1`
done
#!/bin/bash
while true
do
echo "输出1~5之间的数字:"
echo "请输入输出的数字是"
read num
case $num in
1|2|3|4|5)
echo "你选择了$num";;
*)
echo "只能输入1~5之间的数"
continue
echo "over!"
;;
esac
done
6、shell文件包含、引用
eg1:/home/data/a.sh
vi b.sh
#!/bin/bash
. /home/data/a.sh
source /home/data/a.sh
(2)定义shell函数:
Shell 函数的定义格式如下:
function_name () {
list of commands
[ return value ]
}
如果愿意,也可在函数名前加上关键字 function:
function function_name () {
list of commands
[ return value ]
}
function:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!