[拾 得] 一枚迷人的贝壳 SHELL / Linux | shell 脚本初步入门,shelllinux
[拾 得] 一枚迷人的贝壳 SHELL / Linux | shell 脚本初步入门,shelllinux
坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/ 索引:- 什么是shell
- shell的分类
- shell脚本的执行方式
- 了解shell的本质是什么
- 了解除bash这个shell之外,还有哪些shell
- shell 脚本以及shell脚本的执行方式/区别
什么是shell 一般来说,操作系统由用户接口(User Interface)和系统内核(kernel)两部分构成 从名字可以看出,内核是操作系统的核心, 它是一个操作系统的灵魂 内核的主要作用有以下几点
- 简单概括, 内核负责管理计算机硬件资源(CPU, 内存, 硬盘, 周边设备I/O)的使用
- 简单地概括, shell 本质应该是CLI, 它是一个让用户通过命令行来实现和系统交互的接口.
shell的分类 操作系统: CentOS release 6.9 (Final) 命令提示符 PS1 : \[\e[31m\]>\[\e[m\] (下图中红色的> 为命令提示符) 来介绍shell的分类之前, 先来介绍一下关于shell查看的几个常用操作 查看系统默认的shell 查看系统中拥有的shell 查看bash的版本
2014年9月, bash曾曝出一个严重的漏洞CVE-2014-6271
ShellShock Bug (特制环境变量注入攻击) 我们使用以下命令对系统进行测试 env x='() { :;}; echo vulnerable' bash -c "echo this is a test" 如果测试的结果只显示了 "this is a test" 表示你的系统正常, 否则你需要对bash打补丁 (以下仅针对CentOS/redhat)Linux shell的大类有以下两种 Bourne shell 以及 C shell 首先介绍 Bourne shell Bourne shell 家族中有三大成员, [sh, ksh, bash] [SH] sh 其的开发者是 Stephen Bourne. 它在 V7 Unix 中作为默认shell 替代了之前使用的 Thompson shell 有意思的是,sh设置sh的许多特性依然沿用至今
Features of the Bourne shell versions since 1979 include:
- Built-in
test
command – System III shell (1981) - # as comment character – System III shell (1981)
- Colon in parameter substitutions "${parameter:=word}" – System III shell (1981)
continue
with argument – System III shell (1981)cat <<-EOF
for indented here documents – System III shell (1981)- Functions and the
return
builtin – SVR2 shell (1984) - Built-ins
unset
,echo
,type
– SVR2 shell (1984) - Source code de-ALGOL68-ized – SVR2 shell (1984)
- Modern "
$@
" – SVR3 shell (1986) - Built-in
getopts
– SVR3 shell (1986) - Cleaned up parameter handling allows recursively callable functions – SVR3 shell (1986)
- 8-bit clean – SVR3 shell (1986)
- Job control – SVR4 shell (1989)
- Multi-byte support – SVR4 shell (1989)
- Job control, command aliasing, and command history designed after the corresponding C shell features. Job control was added to the Bourne Shell in 1989.
- A choice of three command line editing styles based on vi, Emacs, and XEmacs.
- Associative arrays and built-in floating point arithmeticoperations (only available in the ksh93 version of KornShell).
- Dynamic extensibility of built-in commands (as of ksh93)
C Shell 家族有两个成员 [csh, tcsh]
[csh] csh 其设计者是 Bill Joy 由于csh拥有交互的特性, 在1980年, csh以其易于上手的特性, 赢得了青睐 csh的设计理念是使得它开起来更像是 C语言, 并且拥有更好的用户交互性
Bourne shell #!/bin/shif [ $days -gt 365 ]then echo This is over a year. fi |
C shell #!/bin/cshif ( $days > 365 ) then echo This is over a year. endif |
shell 脚本的执行方式
# 执行的脚本 [第一种方式 sh script.sh] 这是一种十分常用的 shell 脚本执行方式. 因为它不需要用户给予脚本执行权限 sh 命令接收 script.sh 这个文件作为传入参数, 开启一个子shell进行命令的解析与执行 [第二种方式 source(.) script.sh] 这种方式, 也不需要用户给予脚本执行权限, 脚本将直接在当前shell中执行 这意味着, 在script.sh 中定义的变量或函数, 在脚本执行结束后, 变量的值或函数返回值等将被保留 (需要注意的是, "." script.sh 这种方式只能在 bash 类型的shell中执行 而 source script.sh 既能在 bash类型 也能在 csh 类型的shell中执行) [第三种方式 ./script.sh] 这种方式, 用户必须赋予脚本执行权限(chmod a+x script.sh) 才能运行脚本, 该方式也是通过子shell对命令进行解析与执行
[关于 子shell 和 父shell] 简单来说,子shell和父shell的关系有以两点 1.子shell 会复制 父shell的变量和函数, 就如同子女继承父母的基因一样, 2.子shell中设置的变量和函数只有子shell自己知道, 子shell无法改变父shell设定的变量和函数 脚本执行中, 如果使用 source / . 方式执行脚本, 命令会于当前shell中解析执行 下面用一个简单的脚本来体会一下, 当前shell中执行脚本, 和子shell执行脚本的取本 脚本内容 使用当前shell执行完cd命令后, 当前shell的目录发生了变化 使用子shell执行完cd命令后, 当前shell的目录并没有发生改变 ------------------------------ 参考资料: https://www.amazon.com/Computer-Science-Overview-Glenn-Brookshear/dp/0133760065 // Computer Science: An Overview (12th Edition) https://www.wikiwand.com/en/Kernel_(operating_system) // Kernel (operating system) http://www.multicians.org/shell.html // The Origin of the Shell https://www.wikiwand.com/en/Command-line_interface // Command-line interface https://unix.stackexchange.com/questions/14934/why-was-the-word-shell-used-to-descibe-a-command-line-interface // Why was the word “shell” used to descibe a command-line interface? https://access.redhat.com/blogs/766093/posts/1976383 // Bash specially-crafted environment variables code injection attack https://www.youtube.com/watch?v=hlDxOvO4Jxk // What is the ShellShock Bug? (September 2014) https://www.youtube.com/watch?v=UllSNdgGLbo // ShellShock Attack Demonstration https://www.wikiwand.com/en/Bourne_shell // Bourne shell https://www.wikiwand.com/en/KornShell // KornShell https://www.wikiwand.com/en/Bash_(Unix_shell) // Bash (Unix shell) https://www.wikiwand.com/en/C_shell // C shell https://www.wikiwand.com/en/Tcsh // tcsh https://unix.stackexchange.com/questions/2976/different-ways-to-execute-a-shell-script // Different ways to execute a shell script
评论暂时关闭