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