錯誤原因
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(即子進程)使腳本並行地,有效率地地同時運行腳本內的多個子任務。