Shell脚本执行的几种方式小结

 更新时间:2023年09月18日 10:33:53   作者:stormkai  
本文介绍了Shell脚本执行的几种方式小结,主要介绍了5种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Shell脚本执行的几种方式 

前面两种是通过创建子shell进程来解释执行脚本。父shell可能获取不到子shell的一些环境变量。
source是在当前shell进程里面直接执行。

父子shell的演示

  • 创建子shell进程

bash命令创建子shell进程
ps -ef|grep bash 查看shell进程

[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3496   3449  0 00:10 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# bash
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3534   3505  0 00:10 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# bash
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3535   3505  0 00:10 pts/0    00:00:00 bash
root       3566   3535  0 00:11 pts/0    00:00:00 grep --color=auto bash

可以看到,第一次执行bash后,创建了个父进程是3499的子进程;第二次执行bash后,创建两个父进程是3505(第一次创建的进程)的子进程。

  • 退出子shell进程

exit 退出子shell进程

[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3535   3505  0 00:10 pts/0    00:00:00 bash
root       3566   3535  0 00:11 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# exit
exit
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3505   3449  0 00:10 pts/0    00:00:00 bash
root       3616   3505  0 00:17 pts/0    00:00:00 grep --color=auto bash
[root@localhost ~]# exit
exit
[root@localhost ~]# ps -ef|grep bash
root        864      1  0 6月27 ?       00:00:00 /bin/bash /usr/sbin/ksmtuned
root       1965   1949  0 6月27 tty1    00:00:02 -bash
root       3449   3433  0 00:09 pts/0    00:00:00 -bash
root       3626   3449  0 00:17 pts/0    00:00:00 grep --color=auto bash

1. 创建shell脚本hello.sh

在/home目录下创建文件夹jiaoben,在文件夹里面创建hello.sh文件

[root@localhost home]# mkdir jiaoben
[root@localhost home]# cd jiaoben/
[root@localhost jiaoben]# ll
总用量 0
[root@localhost jiaoben]# touch hello.sh
[root@localhost jiaoben]# vi hello.sh
[root@localhost jiaoben]# ll
总用量 4
-rw-r--r--. 1 root root 26 6月  27 23:29 hello.sh

hello.sh的内容为

#!/bin/bash
echo "Hello!"

脚本以 #!/bin/bash 开头,指定shell解析器

2. bash/sh 脚本的绝对路径/相对路径

不管脚本有没有x(可执行)权限,都可以执行

2.1 sh 脚本的相对路径执行脚本

[root@localhost jiaoben]# pwd
/home/jiaoben
[root@localhost jiaoben]# sh hello.sh
Hello!

2.2 sh 脚本的绝对路径执行脚本

[root@localhost jiaoben]# sh /home/jiaoben/hello.sh
Hello!

2.3 bash 脚本的相对路径执行脚本

[root@localhost jiaoben]# bash hello.sh
Hello!

2.4 bash 脚本的绝对路径执行脚本

[root@localhost jiaoben]# bash /home/jiaoben/hello.sh
Hello!

3. 脚本的绝对路径/相对路径(需要脚本的+x权限)

3.1 脚本的相对路径执行脚本

./hello.sh

[root@localhost jiaoben]# pwd
/home/jiaoben
[root@localhost jiaoben]# ll
总用量 4
-rw-r--r--. 1 root root 26 6月  27 23:29 hello.sh
[root@localhost jiaoben]# ./hello.sh
-bash: ./hello.sh: 权限不够
[root@localhost jiaoben]# chmod +x hello.sh
[root@localhost jiaoben]# ./hello.sh
Hello!
[root@localhost jiaoben]# ll
总用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# ./hello.sh
Hello!

从上面可以看到,执行./hello.sh需要x权限

3.2 脚本的绝对路径执行脚本

/home/jiaoben/hello.sh

  • 收回hello.sh的x(可执行)权限,然后执行脚本
[root@localhost jiaoben]# chmod -x hello.sh
[root@localhost jiaoben]# ll
总用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# /home/jiaoben/hello.sh
-bash: /home/jiaoben/hello.sh: 权限不够
  • 给脚本hello.sh添加x(可执行)权限,通过脚本绝对路径再执行脚本
[root@localhost jiaoben]# chmod +x hello.sh
[root@localhost jiaoben]# ll
总用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# /home/jiaoben/hello.sh
Hello!

4. source 脚本的绝对路径/相对路径执行脚本

source执行脚本时,不管脚本有没有x(可执行权限),脚本都可以执行我们平时配置完环境变量,用的就是source执行,如source /etc/profile

4.1 source相对路径执行脚本

source hello.sh

[root@localhost jiaoben]# ll
总用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# source hello.sh
Hello!

4.2 source绝对路径执行脚本

source /home/jiaoben/hello.sh

[root@localhost jiaoben]# ll
总用量 4
-rwxr-xr-x. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# source /home/jiaoben/hello.sh
Hello!

hello.sh没有x(可执行)权限时候,使用source执行情况:

[root@localhost jiaoben]# chmod -x hello.sh
[root@localhost jiaoben]# ll
总用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# source hello.sh
Hello!
[root@localhost jiaoben]# source /home/jiaoben/hello.sh
Hello!

5. .空格脚本的绝对路径/相对路径执行脚本

.空格绝对路径/相对路径执行脚本时候,不管脚本有没有x(可执行)权限,都可以执行。
下面我们李子都使用没有x(可执行)权限的x脚本来验证。

5.1 .空格脚本的相对路径执行脚本

. hello.sh

[root@localhost jiaoben]# ll
总用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# . hello.sh
Hello!

5.2 .空格脚本的绝对路径执行脚本

[root@localhost jiaoben]# ll
总用量 4
-rw-r--r--. 1 root root 27 6月  27 23:38 hello.sh
[root@localhost jiaoben]# . /home/jiaoben/hello.sh
Hello!

 到此这篇关于Shell脚本执行的几种方式小结的文章就介绍到这了,更多相关Shell 脚本执行内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • linux查看所有用户和查看用户组的方法(修改用户组)

    linux查看所有用户和查看用户组的方法(修改用户组)

    linux里并没有像windows的net user,net localgroup这些方便的命令来管理用户,下面介绍查看所有用户和用户组的方法
    2014-01-01
  • 查看linux中某个端口(port)是否被占用的方法

    查看linux中某个端口(port)是否被占用的方法

    下面小编就为大家带来一篇查看linux中某个端口(port)是否被占用的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Shell用sed命令删除特定行的方法

    Shell用sed命令删除特定行的方法

    这篇文章主要介绍了Shell用sed命令删除特定行的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 详解Linux解压缩文件

    详解Linux解压缩文件

    这篇文章给大家介绍了Linux解压缩文件的命令,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-05-05
  • Linux echo命令的使用及三种实现方式

    Linux echo命令的使用及三种实现方式

    这篇文章主要介绍了Linux echo命令的使用及三种实现方式,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • Linux更新Python版本及修改python默认版本的方法

    Linux更新Python版本及修改python默认版本的方法

    很多情况下拿到的服务器python版本很低,需要自己动手更改默认python版本,但是有好多朋友都被这个问题难倒了,接下来,通过本篇文章给大家介绍linux更新Python版本及修改默认版本的方法,感兴趣的朋友一起学习吧
    2015-12-12
  • Xshell全局去除提示音图文方法详解

    Xshell全局去除提示音图文方法详解

    这篇文章主要为大家介绍了Xshell全局去除提示音图文方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Linux系统中查看目录权限的命令详解

    Linux系统中查看目录权限的命令详解

    在 Linux 系统中,文件和目录的权限管理是保证系统安全和数据安全的重要机制,正确理解和使用权限设置,可以帮助用户有效地控制对文件和目录的访问,本文给大家介绍了Linux系统中查看目录权限的命令,需要的朋友可以参考下
    2024-12-12
  • Linux使用cal命令查看日历的实用技巧分享

    Linux使用cal命令查看日历的实用技巧分享

    在 Linux 系统中,cal 命令是一个简单却非常实用的小工具,专门用于显示日历信息,无论是查看当前月份的日历、特定年份的日历,还是进行日期计算,cal 命令都能轻松应对,本文将深入探讨 cal 命令的功能,从基础用法到高级技巧,需要的朋友可以参考下
    2026-02-02
  • Linux中shell脚本获取当前工作目录的方法

    Linux中shell脚本获取当前工作目录的方法

    今天小编就为大家分享一篇Linux中shell脚本获取当前工作目录的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06

最新评论