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环境中运行
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现