添加sh有什么作用?


10

我对使用终端真的很陌生,我一直盲目地遵循指南,却不知道为什么要这样做以及它做什么。.尽管本指南可以帮助我安装vmware player。

这对我有用,因为使文件可执行文件,单击文件并运行。

sudo sh vmwareplayer.txt (将其更短)

我不知何故sudosh做什么,但会做什么?没有它,它将变为“找不到vmwareplayer.txt”。对于捆绑文件,为什么以.txt结尾?


它是语言翻译
Tachyons

@Tachyons如果您知道问题的答案,请更详细地解释它,并将其发布为答案!:)
Alvar

也看到了相关的问题的答案非常深入解释有关口译(如sh,bash)的和shebangs等

1
@Mik这不是该问题的重复项。这是sh在调用脚本的命令中做什么。这个问题是关于脚本文件内部如何#!工作的。完全不同。
Eliah Kagan 2013年

@EliahKagan是的,我可能太急于引用它作为重复项:在提及其他答案时,我应该把“相关,但不能重复”。你像往常一样是对的;如果您想让主持人重新打开就可以了。我已经投票决定要重新开放。

Answers:


16

sh代表“ shell”,shell是老式的,类似于Unix的命令行解释器。解释器是执行以编程或脚本语言编写的特定指令的程序。因此,基本上,您说“为我执行该文件”。

您必须了解Linux并不会真正查看文件扩展名才能确定文件(或程序)是什么。因此,只要以sh解释器理解的方式编写该文件的内容,它就可以工作。但是,仅出于可读性考虑,此类文件通常使用.sh扩展名,而我不知道开发人员在给该文件添加.txt扩展名时的想法。


5

在Ubuntu中,sh/bin/sh仅指向dashsh应该运行dash用于Ubuntu的默认命令解释器。1 dash是指Debian Almquist外壳。

Shell是系统的命令行解释器。还有其他一些炮弹一样bashcshzsh等这里是从man页面简要摘录dash

 The shell is a command that reads lines from either a file or the termi‐
 nal, interprets them, and generally executes other commands.  It is the
 program that is running when a user logs into the system (although a user
 can select a different shell with the chsh(1) command).  The shell imple‐
 ments a language that has flow control constructs, a macro facility that
 provides a variety of features in addition to data storage, along with
 built in history and line editing capabilities.  It incorporates many
 features to aid interactive use and has the advantage that the interpre‐
 tative language is common to both interactive and non-interactive use
 (shell scripts).  That is, commands can be typed directly to the running
 shell or can be put into a file and the file can be executed directly by
 the shell.

关于Linux Shell的教程很多,您可以从Wikipedia Article开始。

遇到您的问题,如果您编写sh filedash则会file为您执行。


这是一个很好的解释,但sh在上面的特定问题中的作用却不是。
NilsB

添加了更多信息,现在就足够了
点燃

1

只需评论一下:

sudoroot特权执行命令。调用时sudo vmwareplayer.txt显然意味着“具有root用户访问权限运行vmwareplayer.txt命令”并失败,因为没有此类程序。

sudo sh command表示“ sh command以root权限执行”

sh command在Bourne shell下执行命令。

出现了新问题。什么是贝壳?这是一个更长的问题,我想您会在Wiki上找到一些信息。

同时,请记住man是您最好的朋友。


1

我相信没有一个较早的答复者回答您实际问过的问题G,所以我会对此进行介绍。sh如前所述,在执行程序时插入,将在非交互式子shell中执行程序。非交互式外壳的行为(例如,初始化)与运行程序时所使用的标准交互式外壳不同。

老实说,我不知道这有什么实际效果。如您所见sh vmwareplayer.txt,(通常)能够在同一文件夹中执行某些操作,而仅仅键入vmwareplayer.txt(通常)则无法。那必须要做外壳环境;它不是shell或subshel​​l本身固有的任何质量。您当然可以直接以root用户身份在当前目录中执行程序sudo ./vmwareplayer.txt。以我的经验,这总是和sudo sh vmwareplayer.txt。我希望有人会来解释为什么有时需要子外壳。


0

sh实用程序是命令语言解释器/命令,它执行从命令行字符串,标准输入或指定文件读取的命令。该应用程序确保要执行的命令以Shell命令语言中描述的语言表示。

例如:

下面的命令从字符串执行shell命令: sh -c "cat myfile.txt" 即执行命令-cat myfile.txt

请参阅以下链接以获取详细说明:http : //pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.