Shell腳本執行的四種方法


(1).bash(或sh) [腳本的相對路徑或絕對路徑]

[xf@xuexi ~]$ cat a.sh
#!/bin/bash
echo "hello world!"
[xf@xuexi ~]$ bash a.sh
hello world!
[xf@xuexi ~]$ which bash
/usr/bin/bash
[xf@xuexi ~]$ sh a.sh
hello world!
[xf@xuexi ~]$ which sh
/usr/bin/sh
[xf@xuexi ~]$ ll /usr/bin/sh
lrwxrwxrwx. 1 root root 4 4月   5 22:07 /usr/bin/sh -> bash

  可以看到sh命令實際指向的是bash命令,sh只是bash的一個軟鏈接

(2).source(或.) [腳本的相對路徑或絕對路徑]

  .(點)是等同於source的,最常見的使用是在重新加載環境變量時。

[xf@xuexi ~]# source a.sh
hello world!
[xf@xuexi ~]# . a.sh
hello world!

 

(3).sh < [腳本名稱]或者cat [腳本名稱] | sh(或bash)

[xf@xuexi ~]# sh < a.sh
hello world!
[xf@xuexi ~]# cat a.sh | sh
hello world!

(4).腳本的相對路徑或絕對路徑,使用該方法,腳本必須擁有執行權限

[xf@xuexi ~]$ chmod +x a.sh
[xf@xuexi ~]$ ./a.sh 
hello world!
[xf@xuexi ~]$ /home/xf/a.sh 
hello world!

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM