错误原因
shell脚本没有可执行权限。
赋予shell脚本可执行权限即可。
例如touch创建test.sh文件,输入以下内容:
[root@localhost demo]# touch test.sh
[root@localhost demo]# ls
test.sh
[root@localhost demo]# ll
total 0
-rw-r--r--. 1 root root 0 May 18 08:20 test.sh
[root@localhost demo]#
可以看到此时test.sh没有可执行权限。
vim输入:
#!/bin/bash
echo "hello world";
echo "执行的文件名:$0";
执行./test.sh,输出
-rw-r--r--. 1 root root 64 May 18 08:24 test.sh
[root@localhost demo]# ./test.sh
-bash: ./test.sh: Permission denied
[root@localhost demo]#
执行. test.sh,输出
[root@localhost demo]# . test.sh
hello world
执行的文件名:-bash
[root@localhost demo]#
执行source test.sh,输出
[root@localhost demo]# source test.sh
hello world
执行的文件名:-bash
[root@localhost demo]#
执行sh test.sh,输出
[root@localhost demo]# sh test.sh
hello world
执行的文件名:test.sh
[root@localhost demo]#
执行bash test.sh,输出
[root@localhost demo]# bash test.sh
hello world
执行的文件名:test.sh
[root@localhost demo]#
赋予test.sh可执行权限,后在执行./test.sh,输出
[root@localhost demo]# chmod 777 test.sh
[root@localhost demo]# ./test.sh
hello world
执行的文件名:./test.sh
[root@localhost demo]#
以上可证明. 、sh 、bash 、source在当前bash环境下读取并执行FileName中的命令。该filename文件可以无"执行权限",./在当前bash环境下读取并执行FileName中的命令。该filename文件不可以没有"执行权限"。
扩展
./FileName作用:打开一个子shell来读取并执行FileName中命令。运行一个shell脚本时会启动另一个命令解释器,每个shell脚本有效地运行在父shell(parent shell)的一个子进程里, 这个父shell是指在一个控制终端或在一个xterm窗口中给你命令指示符的进程,shell脚本也可以启动他自已的子进程,这些子shell(即子进程)使脚本并行地,有效率地地同时运行脚本内的多个子任务。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报