大数据程序员为什么要学习Shell呢?
1)需要看懂运维人员编写的Shell程序。
2)偶尔会编写一些简单Shell程序来管理集群、提高开发效率。
(1)Linux提供的Shell解析器有:
[root@master ~]$ cat /etc/shells
/bin/sh #是bash的一个快捷方式
/bin/bash #bash是大多数Linux默认的shell,包含的功能几乎可以涵盖shell所有的功能
/sbin/nologin #表示非交互,不能登录操作系统
/bin/dash #小巧,高效,功能相比少一些
/bin/csh #具有C语言风格的一种shell,具有许多特性,但也有一些缺陷
/bin/tcsh #是csh的增强版,完全兼容csh
(2)bash和sh的关系
[root@master bin]$ ll | grep bash
-rwxr-xr-x. 1 root root 941880 5月 11 2016 bash
lrwxrwxrwx. 1 root root 4 5月 27 2017 sh -> bash
(3)Centos默认的解析器是bash
[root@master bin]$ echo $SHELL
/bin/bash
1)脚本格式
脚本以#!/bin/bash
开头(指定解析器)
2)第一个Shell脚本:helloworld
(1)需求:创建一个Shell脚本,输出helloworld
(2)案例实操:
[root@master datas]$ touch helloworld.sh
[root@master datas]$ vi helloworld.sh
在helloworld.sh中输入如下内容
#!/bin/bash
echo "helloworld"
(3)脚本的常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
sh+脚本的相对路径
[root@master datas]$ sh helloworld.sh
Helloworld
sh+脚本的绝对路径
[root@master datas]$ sh /home/atguigu/datas/helloworld.sh
helloworld
bash+脚本的相对路径
[root@master datas]$ bash helloworld.sh
Helloworld
bash+脚本的绝对路径
[root@master datas]$ bash /home/atguigu/datas/helloworld.sh
Helloworld
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
(a)首先要赋予helloworld.sh 脚本的+x权限
[root@master datas]$ chmod 777 helloworld.sh
(b)执行脚本
相对路径(推荐使用)
[root@master datas]$ ./helloworld.sh
Helloworld
绝对路径
[root@master datas]$ /home/atguigu/datas/helloworld.sh
Helloworld
注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限
3)第二个Shell脚本:多命令处理
(1)需求: 在/home/atguigu/目录下创建一个cls.txt,在cls.txt文件中增加“I love cls”。
(2)案例实操:
[root@master datas]$ touch batch.sh
[root@master datas]$ vi batch.sh
在batch.sh中输入如下内容
#!/bin/bashcd /home/atguigu
touch cls.txt
echo "I love cls" >>cls.txt
上一篇:STM32实战项目-基本定时器
下一篇:你的游戏帐号是如何被盗的