Shell命令解释器:介于 系统内核——>命令解释器——>外围应用程序:应用/命令/服务
Shell编程:bash编程
1、命令解释器
bash | 目前应用最广泛的一款命令解释器,红帽系列(默认),Debain,Unbantu,BASH全称:Bourne-Again Shell |
dash | 一般Debain/Unbantu系统默认的,运行脚本推荐使用bash lidao.sh |
csh,tcsh | 一些unix系统使用 |
zsh | 功能更多,支持更多的插件,可以更好看 |
2、编程语言分类
解析:直接解析型 | shell,python,书写代码后,通过对应解析器运行 |
编译:需要编译后运行 | C,C++ |
解释型
1
2
3
4
5
|
[root@localhost ~] # vim 1.py [root@localhost ~] # cat 1.py print( "hrllo world!" ) [root@localhost ~] # python 1.py hrllo world! |
3、Shell脚本执行方式
1通过sh或bash执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost shell] # vim 01.exec.way.sh [root@localhost shell] # cat 01.exec.way.sh hostname pwd whoami [root@localhost shell] # sh 01.exec.way.sh localhost.localdomain /root/shell root [root@localhost shell] # bash 01.exec.way.sh localhost.localdomain /root/shell root [root@localhost shell] # ll `which sh bash` -rwxr-xr-x. 1 root root 964544 Apr 10 2018 /usr/bin/bash lrwxrwxrwx. 1 root root 4 Mar 23 05:59 /usr/bin/sh -> bash |
2通过source或 . 执行
1
2
3
4
5
6
7
8
|
[root@localhost shell] # . 01.exec.way.sh localhost.localdomain /root/shell root [root@localhost shell] # source 01.exec.way.sh localhost.localdomain /root/shell root |
3通过绝对路径或相对路径执行
1
2
3
4
5
6
7
8
9
10
|
[root@localhost shell] # ./01.exec.way.sh - bash : . /01 . exec .way.sh: Permission denied [root@localhost shell] # chmod +x ./01.exec.way.sh [root@localhost shell] # ls -l total 4 -rwxr-xr-x. 1 root root 21 May 6 01:15 01. exec .way.sh [root@localhost shell] # ./01.exec.way.sh localhost.localdomain /root/shell root |
4通过重定向符号运行
1
2
3
4
5
6
7
8
|
[root@localhost shell] # sh < 01.exec.way.sh localhost.localdomain /root/shell root [root@localhost shell] # bash < 01.exec.way.sh localhost.localdomain /root/shell root |
4、应用场景
应用及场景
通过sh或bash执行 | 书写脚本后,最常用的方式,在其他非红帽系统中。建议使用bash运行脚本 |
通过.或source | 加载/配置生效文件(环境变量,别名)可以用来实现include功能,八其他脚本引用到当前脚本中 |
通过相对或绝对路径 | 一般不推荐使用这种,系统脚本/服务使用脚本(加上执行权限) |
输入重定向符 | 不推荐使用 |
总结
原文链接:https://blog.csdn.net/m0_73703409/article/details/130529383