持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第3天,点击查看活动详情
Shell编程——概述和入门
1、Shell概述
Shell
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。
==Shell 既是一种命令行解释器,又是一种程序设计语言。==
Shell还是一个功能强大的编程语言,易编写,易调试,灵活性强。
Shell 接收应用程序/用户命令,然后调用操作系统内核。
Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。
Shell脚本
Shell 脚本(shell script),是一种为 shell 编写的脚本程序,编写完源码后不用编译,直接运行源码即可。。
Shell 脚本的优势在于处理偏操作系统底层的业务,例如,Linux 内部的很多应用(有的是应用的一部分)都是使用 Shell 脚本开发的,因为有 1000 多个 Linux 系统命令为它作支撑,特别是 Linux 正则表达式以及三剑客 grep、awk、sed 等命令。
对于一些常见的系统脚本,使用 Shell 开发会更简单、更快速,例如,让软件一键自动化安装、优化,监控报警脚本,软件启动脚本,日志分析脚本等,虽然 Python 也能做到这些,但是考虑到掌握难度、开发效率、开发习惯等因素,它们可能就不如 Shell 脚本流行以及有优势了。对于一些常见的业务应用,使用 Shell 更符合 Linux 运维简单、易用、高效的三大原则。
Shell环境
Shell 编程跟 JavaScript、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。
Centos 7支持的Shell环境是 Bash:
[root@haoming ~]# echo $SHELL
/bin/bash
Bash,也就是 Bourne Again Shell,由于易用和免费,Bash 在日常工作中被广泛使用。同时,Bash 也是大多数Linux 系统默认的 Shell。
2、Shell入门
1、编写shell脚本
[root@haoming shell]# cat hello.sh
#!/bin/bash
echo "hello world"
-
#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。
-
echo 命令用于向窗口输出文本。
2、执行shell脚本
方式一:运行bash解释器,采用bash或sh + 脚本的相对路径或绝对路径(不用赋予脚本x可执行权限)
这种运行方式是,直接运行解释器,其参数就是 shell 脚本的文件名,本质是bash解释器帮你执行脚本,所以脚本本身不需要执行权限。
bash:
[root@haoming shell]# bash ./hello.sh 相对路径
hello world
[root@haoming shell]# bash /home/shell/hello.sh 绝对路径
hello world
sh:
[root@haoming shell]# sh ./hello.sh
hello world
[root@haoming shell]# sh /home/shell/hello.sh
hello world
方式二:shell脚本作为可执行程序,通过相对路径或绝对路径执行(常用)
本质是脚本需要自己执行,所以需要执行权限
[root@haoming shell]# chmod +x hello.sh #使脚本具有执行权限
[root@haoming shell]# ll #查看权限
total 4
-rwxr-xr-x 1 root root 31 Apr 24 17:43 hello.sh
[root@haoming shell]# /home/shell/hello.sh #执行脚本 绝对路径
hello world
[root@haoming shell]# ./hello.sh #执行脚本 相对路径
hello world
注意,相对路径一定要写成 ./hello.sh,而不是 hello.sh,运行其它二进制的程序也一样,直接写 hello.sh,linux 系统会去 PATH 里寻找有没有叫 hello.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 里,你的当前目录通常不在 PATH 里,所以写成hello.sh 是会找不到命令的,要用 ./hello.sh 告诉系统说,就在当前目录找。
方式三:在脚本的路径前加上 source 或 .
#加上 source
[root@haoming shell]# source hello.sh
hello world
[root@haoming shell]# source /home/shell/hello.sh
hello world
#加上 . 这个.是一个命令,不是指当前工作目录
[root@haoming shell]# . /home/shell/hello.sh
hello world
[root@haoming shell]# . hello.sh
hello world
父子进程介绍
执行shell脚本中的方式一和方式二本质上都是在当前shell 中打开一个子shell来执行脚本内容,当脚本内容结束,则子shell关闭,回到父shell 中。方式三直接在当前的shell环境中执行脚本内容,无需打开子shell进程。 子shell 与父 shell 的区别就在于环境变量的继承关系,如在子shell中设置的当前变量,在父 shell中是不可见的。
父子shell测试:
# 查看进程间的父子关系,只有一个父shell
[root@haoming ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 3181 32120 0 14:10 pts/2 00:00:00 ps -f
root 32120 32117 0 13:39 pts/2 00:00:00 -bash
# 在当前父shell进程下开启子shell进程
[root@haoming ~]# bash
# 查看进程间的父子关系,子shell的PPID指向父shell的 PID
[root@haoming ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 3360 32120 0 14:12 pts/2 00:00:00 bash
root 3379 3360 0 14:12 pts/2 00:00:00 ps -f
root 32120 32117 0 13:39 pts/2 00:00:00 -bash
# 退出子shell
[root@haoming ~]# exit
exit
[root@haoming ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 3399 32120 0 14:12 pts/2 00:00:00 ps -f
root 32120 32117 0 13:39 pts/2 00:00:00 -bash