本文是我学习Hadoop搭建过程中的各种笔记,内容来自于各种公开的教程,起点非常低,从Linux基础开始,直至在PC上搭建Hadoop成功,是真正的从零开始。
感谢过程中帮助我的各位认识的和不认识的老师。
20、shell介绍和变量:
1、shell介绍
shell是一个用C语言编写的程序,它被称为用户使用Linux的桥梁。
shell即是一种命令语言,又是一种程序设计语言
shell脚本(shell script),是一种为shell编写的脚本语言
2、shell运行环境和运行方式
2.1 shell编程和Java、Python等一样【都是一种编程语言,语言简单不需太多语法,不需动态编译】。只需要一个文本编辑器和解释工具即可。Linux的shell太多,常见的有:
Bourne Shell(/urs/bin/sh/或/bin/sh)在Linux下: /bin/sh
Bourne Again Shell (/bin/bash)在Linux下: /bin/bash
[root@hadoop01 ~]# which sh
/bin/sh
[root@hadoop01 ~]# which bash
/bin/bash
C Shell (/usr/bin/csh) 在Linux下: /bin/tcsh
[root@hadoop01 ~]# which csh
/bin/csh
[root@hadoop01 ~]# which tcsh
/bin/tcsh
K Shell (/usr/bin/ksh) 是cshell的超集
[root@hadoop01 ~]# which ksh
/usr/bin/which: no ksh in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.8.0_144//bin::/root/bin)
Shell for Root (/sbin/sh)
其中Linux自带有:/bin/bash、/bin/sh、/bin/tcsh、/bin/csh等。以上集中都是交互式。
bash易用和免费。bash在日常工作中广泛被使用。
bash也是大多数的Linux系统默认的Shell
/bin/sh 等价于 /bin/bash
一个hello world实例:
编辑第一个文件脚本shell:vi/home/shell/first.sh
[root@hadoop01 shell]# vi ./firstshell firstshell:Linux命令的集合,也称之为shell脚本
#!/bin/bash
echo 'hello world'
现在shell脚本就编好了:
[root@hadoop01 shell]# ll
total 4
-rw-r--r--. 1 root root 32 Apr 23 17:19 firstshell
运行shell脚本(加可执行的权限或加解释器):
[root@hadoop01 shell]# ll
total 4
-rw-r--r--. 1 root root 32 Apr 23 17:19 firstshell
-rw-r--r--:是没有执行权限的,此时./firstshell是Tab不出来的
加可执行权限:
[root@hadoop01 shell]# chmod 744 ./firstshell
[root@hadoop01 shell]# ll
total 4
-rwxr--r--. 1 root root 32 Apr 23 17:19 firstshell
此时就能打印出来了:
[root@hadoop01 shell]# ./firstshell
hello world
脚本是shell命令的集合,此时在外面,echo也能打印出来:
[root@hadoop01 shell]# echo 'hello world'
hello world
此时:chmod 644