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下find 命令的 7 种用法

    Linux下find 命令的 7 种用法

    Linux 下 find 命令在目录结构中搜索文件,并执行指定的操作,Linux 下 find 命令提供了相当多的查找条件,功能很强大,这篇文章主要介绍了find 命令的 7 种用法,需要的朋友可以参考下
    2022-12-12
  • Jenkins中sh函数的用法示例小结

    Jenkins中sh函数的用法示例小结

    这篇文章主要介绍了Jenkins中sh函数的用法示例小结,通过实例代码讲解了三种用法,每种方法给大家介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • bash shell 自定义函数命令持久化生效问题

    bash shell 自定义函数命令持久化生效问题

    这篇文章主要介绍了bash shell 自定义函数命令持久化生效,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • linux系统下dd命令的使用方法

    linux系统下dd命令的使用方法

    这篇文章主要介绍了linux系统下dd命令的使用方法,dd命令的功能是把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换
    2014-01-01
  • Linux shell常用的73条命令总结

    Linux shell常用的73条命令总结

    对于经常使用linux系统的朋友们来说,基本常用的shell命令是少不了的,下面这篇文中就为大家总结了Linux shell的常用命令,有需要的朋友们可以参考借鉴,下面来一起学习学习吧。
    2017-01-01
  • 三分钟学会Linux基本指令

    三分钟学会Linux基本指令

    本文小马将把Linux一般比较常见的指令给大家一一列举出来,为了大家忘记某些指令后,可以方便查询记忆,再次小马建议,Linux指令并不需要去特别花时间专门记忆,只需要多进行操作实现就行,这篇文章主要介绍了Linux基本指令,需要的朋友可以参考下
    2022-12-12
  • linux shell 管道命令(pipe)使用及与shell重定向区别

    linux shell 管道命令(pipe)使用及与shell重定向区别

    这篇文章主要介绍了linux shell 管道命令(pipe)使用及与shell重定向区别,需要的朋友可以参考下
    2015-10-10
  • 使用shell检查并修复mysql数据库表的脚本

    使用shell检查并修复mysql数据库表的脚本

    这篇文章主要介绍了使用shell检查并修复mysql数据库表的脚本,需要的朋友可以参考下
    2014-03-03
  • Shell脚本实现memcache缓存命中率监控

    Shell脚本实现memcache缓存命中率监控

    这篇文章主要介绍了Shell脚本实现memcache缓存命中率监控,这个脚本比较简单,通过check_tcp脚本获取memcached的stats信息,然后将该信息格式化成一个字符串,然后将get_hits比上cmd_gets就能得到缓存命中率,需要的朋友可以参考下
    2014-12-12
  • shell脚本实现mysql定时备份、删除、恢复功能

    shell脚本实现mysql定时备份、删除、恢复功能

    这篇文章主要介绍了shell脚本实现mysql定时备份、删除、恢复功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11

最新评论