shell脚本没有家当线工作的罚款?为什么? [英] Shell script working fine without shebang line? Why?

查看:138
本文介绍了shell脚本没有家当线工作的罚款?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的shell脚本,并发现我的shell脚本不需要家当行

I was writing a simple shell script and found out that my shell script doesn't require shebang line

#!/bin/sh

如果我给执行权限给我的脚本,并使用执行 ./ myscript.sh 。它运行正常。

If I give execute permissions to my script and execute using ./myscript.sh. It runs fine.

我使用庆典 / bin / sh的实际上指向庆典

lrwxrwxrwx 1 root root /bin/sh -> bash

我知道shebang行是用来告诉壳其中除preTER用于您的脚本的其余部分。

I know that shebang line is used to tell shell which interpreter to use for your rest of the script.

如果我在Perl错过家当线,给执行权限和运行 ./ myscript.pl ,这是行不通的。

If I miss shebang line in perl, give execute permissions and run ./myscript.pl, it doesn't work.

什么是实际发生的事情吗?如果我使用 ./ ,当实际需要家当行?

What's actually happening here? If I use ./, When is shebang line actually needed?

推荐答案

需要家当行的的文件,只有当它的意思为可执行文件来运行(而不是 SH file.sh 调用。它实际上并不脚本需要,它是系统知道如何找到除preTER

shebang line is needed in the file and only if it's meant to be run as executable (as opposed to sh file.sh invocation. It is not actually needed by script, it is for the system to know how to find interpreter.

修改的:对不起,误读的问题。如果家当线缺失或无法识别,则使用 / bin / sh的。但我preFER是明确的关于跨preTER。

EDIT: Sorry for misreading the question. If the shebang line is missing or not recognized, /bin/sh is used. But I prefer being explicit about the interpreter.

请注意,这种行为并不普遍,IIRC,只有一些 EXEC * 家庭功能做的(更不用提不同的平台),所以这是另一个原因是明确在这里。

Note, that this behavior is not universal, IIRC, only some exec* family function do that (not to mention different platforms), so that's another reason to be explicit here.

这篇关于shell脚本没有家当线工作的罚款?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆