这两个用于运行Shell脚本的命令有什么区别? [英] What is the difference between these two commands which are used to run shell script?

查看:79
本文介绍了这两个用于运行Shell脚本的命令有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一个脚本,可以在Linux中导出一些必要的路径.运行此脚本后,我必须运行其他一些脚本.

我有两个脚本

1 import.sh =  importing paths 
2 main.sh   = this script do something with HCI (use for Bluetooth purpose).

当我运行./import.sh而不是./main.sh时,它给出了错误. 当我先运行. ./import.sh然后运行./main.sh时,它就可以正常工作.

那么./import.sh. ./import.sh之间的区别是什么?

如果我以超级用户身份运行脚本会怎样?可能是. ./以超级用户身份用于运行脚本.

解决方案

这两个调用之间的区别是./import.sh作为程序执行import.sh,而. ./import.sh在您的Shell中对其进行评估.

如果"import.sh"是ELF程序(已编译的二进制文件,而不是Shell脚本),则. ./import.sh将不起作用.

如果import.sh的顶部有一个Shebang(例如#!/bin/perl),那么如果您尝试执行. ./import.sh,则可能会带来令人讨厌的惊喜和大量错误消息-除非shebang发生了匹配您当前的外壳,在这种情况下它将偶然起作用.或者,如果Perl代码以某种方式成为有效的Bash脚本,这似乎不太可能.

. ./import.sh等效于source import.sh,并且不需要文件设置执行位(因为它是由已经在运行的shell解释的,而不是通过exec生成的).我认为这是您错误的根源.另一个区别是./import.sh在当前外壳程序中运行,而不是在子外壳程序中运行,因此任何未导出的环境变量都会影响您用于启动的外壳程序!

因此,它们实际上是完全不同的.除非您知道自己在做什么并且了解其中的区别,否则通常要./import.sh.

Here I have one script which exporting some necessary path in Linux. After running this script I have to run some other scripts.

I have two scripts

1 import.sh =  importing paths 
2 main.sh   = this script do something with HCI (use for Bluetooth purpose).

when I run ./import.sh and than ./main.sh then it's giving error. And when I run . ./import.sh and then ./main.sh then it's working fine.

So what is the diff between ./import.sh and . ./import.sh?

What happens if I run script as a super user? May be . ./ using for run script as a super user.

解决方案

The difference between the two invocations is that ./import.sh is executing import.sh as a program, and . ./import.sh is evaluating it in your shell.

If "import.sh" were an ELF program (a compiled binary, not a shell script), . ./import.sh would not work.

If import.sh had a shebang at the top (like #!/bin/perl), you'd be in for a nasty surprise and a huge number of error messages if you tried to do . ./import.sh - unless the shebang happened to match your current shell, in which case it would accidentally work. Or if the Perl code were to somehow be a valid Bash script, which seems unlikely.

. ./import.sh is equivalent to source import.sh, and doesn't require that the file have the execute bit set (since it's interpreted by your already-running shell instead of spawned via exec). I assume this is the source of your error. Another difference is that ./import.sh runs in the current shell instead of a subshell, so any non-exported environment variables will affect the shell you used for the launch!

So, they're actually rather different. You usually want to ./import.sh unless you know what you're doing and understand the difference.

这篇关于这两个用于运行Shell脚本的命令有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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