大数据技术之Shell 第1章 Shell概述

这篇博客介绍了Shell的基础知识,包括Shell的作用、Linux下的常见Shell类型,以及Centos默认使用的bash。第二章深入讲解了Shell脚本的格式、编写helloworld脚本的步骤,以及脚本的三种执行方式,探讨了执行权限和子shell的影响。同时推荐了IT学习网站和Shell视频教程,邀请读者加入QQ交流群进一步学习。
摘要由CSDN通过智能技术生成

第1章 Shell概述

Shell是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核。

image-20220608043501862

Shell还是一个功能相当强大的编程语言,易编写、易调试、灵活性强。

1)Linux提供的Shell解析器有:

[root@hadoop101 ~]$ cat /etc/shells 

/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

2)bash和sh的关系

[root@hadoop101 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@hadoop101 bin]$ echo $SHELL
/bin/bash

第2章 Shell脚本入门

1)脚本格式

脚本以#!/bin/bash开头(指定解析器)

2)第一个Shell脚本:helloworld

(1)需求:创建一个Shell脚本,输出helloworld

(2)案例实操:

[root@hadoop101 datas]$ touch helloworld.sh
[root@hadoop101 datas]$ vi helloworld.sh

在helloworld.sh中输入如下内容

#!/bin/bash
echo "helloworld"

(3)脚本的常用执行方式

第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)

sh+脚本的相对路径

[root@hadoop101 datas]$ sh helloworld.sh 
Helloworld

​ sh+脚本的绝对路径

[root@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh 
helloworld

​ bash+脚本的相对路径

[root@hadoop101 datas]$ bash helloworld.sh 
Helloworld

​ bash+脚本的绝对路径

[root@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh 
Helloworld

第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)

(a)首先要赋予helloworld.sh 脚本的+x权限

[root@hadoop101 datas]$ chmod +x helloworld.sh

(b)执行脚本

相对路径

[root@hadoop101 datas]$ ./helloworld.sh 
Helloworld

绝对路径

[root@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh 

Helloworld

注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。

第三种:在脚本的路径前加上“.”

(a)有以下脚本

[root@hadoop101 datas]$ cat test1.sh 
#! /bin/bash
A="hello"

(b) 分别使用sh,bash,./ 和 . 的方式来执行,结果如下:

[root@hadoop101 datas]$ bash test1.sh 
[root@hadoop101 datas]$ echo $A
[root@hadoop101 datas]$ sh test1.sh 
[root@hadoop101 datas]$ echo $A
[root@hadoop101 datas]$ ./test1.sh 
[root@hadoop101 datas]$ echo $A
[root@hadoop101 datas]$ . test1.sh 
[root@hadoop101 datas]$ echo $A
hello

原因:

前两种方式都是在当前shell中打开一个子shell来执行脚本内容,当脚本内容结束,则子shell关闭,回到父shell中。

第三种,也就是使用在脚本路径前加.的方式,可以使脚本内容在当前shell里执行,而无需打开子shell!

开子shell与不开子shell的区别就在于,环境变量的继承关系,如在子shell中设置的当前变量,父shell是不可见的。

IT学习网站

牛牛IT网站

Shell 视频教程推荐

跟着360架构师 学习Shell脚本编程

链接:https://pan.baidu.com/s/1BcYMz5Ov9OpbaoFVrCS2lw
提取码:150c
–来自百度网盘超级会员V4的分享

Shell 高阶开发实战
链接:https://pan.baidu.com/s/1J1Y_htCmru6ujUZVzyIKJA
提取码:i3rn

失效➕V:x923713

QQ交流群 欢迎加入

WechatIMG135
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值