Shell命令解释器分类示例详解(shell是一个命令解释器)干货满满

随心笔谈12个月前发布 admin
96 0



目录1、命令解释器2、编程语言分类3、Shell脚本执行方式4、应用场景总结

Shell命令解释器:介于  系统内核——>命令解释器——>外围应用程序:应用/命令/服务

Shell编程:bash编程

bash目前应用最广泛的一款命令解释器,红帽系列(默认),Debain,Unbantu,BASH全称:Bourne-Again Shelldash一般Debain/Unbantu系统默认的,运行脚本推荐使用bash lidao.shcsh,tcsh一些unix系统使用zsh功能更多,支持更多的插件,可以更好看

解析:直接解析型shell,python,书写代码后,通过对应解析器运行编译:需要编译后运行C,C++

解释型

[root@localhost ~]# vim 1.py
[root@localhost ~]# cat 1.py
print(“hrllo world!”)
[root@localhost ~]# python 1.py
hrllo world!

1通过sh或bash执行

[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或 . 执行

[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通过绝对路径或相对路径执行

[root@localhost shell]# https://www.jb51.net/article/01.exec.way.sh
-bash: https://www.jb51.net/article/01.exec.way.sh: Permission denied
[root@localhost shell]# chmod +x https://www.jb51.net/article/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]# https://www.jb51.net/article/01.exec.way.sh
localhost.localdomain
/root/shell
root

4通过重定向符号运行

[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

应用及场景

通过sh或bash执行书写脚本后,最常用的方式,在其他非红帽系统中。建议使用bash运行脚本通过.或source加载/配置生效文件(环境变量,别名)可以用来实现include功能,八其他脚本引用到当前脚本中通过相对或绝对路径一般不推荐使用这种,系统脚本/服务使用脚本(加上执行权限)输入重定向符不推荐使用

您可能感兴趣的文章:Shell脚本bash: https://www.jb51.net/article/t.sh:/bin/bash^M:损坏的解释器: 没有那个文件或目录

© 版权声明

相关文章