shell脚本的执行方式

方式一:直接执行,需要添加x权限

1
2
3
4
5
[root@m01 /server/scripts]# ll tar.sh
-rw-r--r-- 1 root root 76 Aug 5 18:22 tar.sh
[root@m01 /server/scripts]# chmod +x tar.sh
[root@m01 /server/scripts]# ./tar.sh
[root@m01 /server/scripts]# /server/scripts/tar.sh

方式二:sh执行

1
2
3
4
[root@m01 /server/scripts]# bash tar.sh
[root@m01 /server/scripts]# ll `which sh bash`
-rwxr-xr-x. 1 root root 964544 Apr 11 2018 /usr/bin/bash
lrwxrwxrwx. 1 root root 4 Apr 29 19:16 /usr/bin/sh -> bash

方式三:source替你在当前环境执行一次脚本

 

 方式四:输入重定向方法

1
2
3
4
[root@m01 /server/scripts]# sh <tar.sh
[root@m01 /server/scripts]# #cat <<
[root@m01 /server/scripts]# #mysql <zabbix.sql
[root@m01 /server/scripts]# #tr

 

注意:sh和source(.)的区别

1
2
3
4
5
6
[root@m01 /server/scripts]# oldboy=666
[root@m01 /server/scripts]# cat show.sh
#!/bin/bash
echo $oldboy
[root@m01 /server/scripts]# #sh show.sh
[root@m01 /server/scripts]# #source show.sh
sh执行脚本 生成子shell 而这个变量在子shell中没有定义
source (include) 把这个脚本在当前shell环境中运行

 

posted @   映辉  阅读(88)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示