Linux中shell脚本的执行通常有4种方式,分别为工作目录执行,绝对路径执行,sh执行,shell环境执行。
首先,看下我们的脚本内容
[tan@tan scripts]$ ll total 4 -rw-rw-r--. 1 tan tan 68 May 8 23:18 test.sh [tan@tan scripts]$ cat test.sh #!/usr/bin/bash /usr/bin/python <<-EOF print "Hello Shell" EOF
1、工作目录执行
工作目录执行,指的是执行脚本时,先进入到脚本所在的目录(此时,称为工作目录),然后使用 ./脚本方式执行
[tan@tan scripts]$ ./test.sh -bash: ./test.sh: Permission denied [tan@tan scripts]$ chmod 764 test.sh [tan@tan scripts]$ ./test.sh Hello Shell
如图,报了权限错误,上一篇博文有提到,这里需要赋权,使用chmod 764 test.sh 赋权后就可以正常执行了
./的意思是说在当前的工作目录下执行hello.sh。如果不加上./,bash可能会响应找到不到hello.sh的错误信息。因为目前的工作目录 (/data/shell)可能不在执行程序默认的搜索路径之列,也就是说,不在环境变量PASH的内容之中。查看PATH的内容可用 echo $PASH 命令。现在的/data/shell就不在环境变量PASH中的,所以必须加上./才可执行。
2、绝对路径执行
绝对路径中执行,指的是直接从根目录/到脚本目录的绝对路径
[tan@tan scripts]$ pwd /home/tan/scripts [tan@tan scripts]$ `pwd`/test.sh Hello Shell [tan@tan scripts]$ /home/tan/scripts/test.sh Hello Shell
这里 `pwd` 指的是该命令执行结果,等同于 /home/tan/scripts
3、sh执行
sh执行,指的是用脚本对应的sh或bash来接着脚本执行
[tan@tan scripts]$ sh test.sh Hello Shell [tan@tan scripts]$ bash test.sh Hello Shell
注意,若是以方法三的方式来执行,那么,可以不必事先设定shell的执行权限,甚至都不用写shell文件中的第一行(指定bash路径)。因为方法三 是将hello.sh作为参数传给sh(bash)命令来执行的。这时不是hello.sh自己来执行,而是被人家调用执行,所以不要执行权限。那么不用 指定bash路径自然也好理解了啊,呵呵……。
4、shell环境执行
shell环境执行,指的是在当前的shell环境中执行,可以使用 . 接脚本 或 source 接脚本
[tan@tan scripts]$ . test.sh Hello Shell [tan@tan scripts]$ source test.sh Hello Shell
总结
以上所述是小编给大家介绍的shell脚本4种执行方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
shell,脚本执行方式
更新日志
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 戴梅君.2011-问签诗【美华】【WAV+CUE】
- 李国祥.1995-九五变奏【嘉音】【WAV+CUE】
- 许景淳.1992-你来自何方【全美唱片】【WAV+CUE】
- 石欣卉.2007-剧欣卉集·完整电视剧主题精丫华纳】【WAV+CUE】
- 群星.2005-LOVE情歌集VOL.5.2CD【正东】【WAV+CUE】
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】