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 脚本执行内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Shell 字符串拼接的实现示例

    Shell 字符串拼接的实现示例

    这篇文章主要介绍了Shell 字符串拼接的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • SHELL脚本监控JAVA进程的代码

    SHELL脚本监控JAVA进程的代码

    这篇文章主要介绍了SHELL脚本监控JAVA进程,需要的朋友可以参考下
    2016-03-03
  • linux文本处理工具及正则表达式集锦

    linux文本处理工具及正则表达式集锦

    这篇文章主要介绍了linux文本处理工具及正则表达式集锦,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05
  • linux 下实现sleep详解及简单实例

    linux 下实现sleep详解及简单实例

    这篇文章主要介绍了linux 下实现sleep详解及简单实例的相关资料,需要的朋友可以参考下
    2017-06-06
  • Linux下使用expect命令编写自动化交互脚本

    Linux下使用expect命令编写自动化交互脚本

    今天小编就为大家分享一篇关于Linux下使用expect命令编写自动化交互脚本,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-11-11
  • Apache服务器的安装步骤(图文教程)

    Apache服务器的安装步骤(图文教程)

    下面小编就为大家带来一篇Apache服务器的安装步骤(图文教程)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Linux常用命令全集(超全面)

    Linux常用命令全集(超全面)

    本文是小编日常收集整理的有关linux常用命令的知识,非常不错具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-10-10
  • awk基础知识小结

    awk基础知识小结

    awk基础知识小结,方便学习awk的朋友
    2013-02-02
  • jenkins 实现shell脚本化定时执行任务的方法

    jenkins 实现shell脚本化定时执行任务的方法

    这篇文章主要介绍了jenkins 实现shell脚本化定时执行任务,解决访问是jenkins构建好之后将jar远程推送到生产服务器,提前退出后台执行服务器远程脚本,脚本通过ngnx提前切走nginx代理auction sleep 1800s,半小时后执行更新重启,具体操作过程跟随小编一起看看吧
    2022-01-01
  • Shell退出状态的使用

    Shell退出状态的使用

    这篇文章主要介绍了Shell退出状态的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论