Linux 什么是 .sh 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13805295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:01:02  来源:igfitidea点击:

What's a .sh file?

linuxshdownload-manager

提问by Tony H

So I am not experienced in dealing with a plethora of file types, and I haven't been able to find much info on exactly what .shfiles are. Here's what I'm trying to do:

所以我在处理过多的文件类型方面没有经验,而且我无法找到关于.sh文件究竟是什么的太多信息。这是我想要做的:

I'm trying to download map data sets which are arranged in tiles that can be downloaded individually: http://daymet.ornl.gov/gridded

我正在尝试下载可以单独下载的图块排列的地图数据集:http: //daymet.ornl.gov/gridded

In order to download a range of tiles at once, they say to download their script, which eventually leads to daymet-nc-retrieval.sh: https://github.com/daymet/scripts/blob/master/Bash/daymet-nc-retrieval.sh

为了一次下载一系列图块,他们说要下载他们的脚本,最终导致daymet-nc-retrieval.shhttps: //github.com/daymet/scripts/blob/master/Bash/daymet-nc-retrieval.sh

So, what exactly am I supposed to do with this code? The website doesn't provide further instructions, assuming users know what to do with it. I'm guessing you're supposed to paste the code in to some other unmentioned application for a browser (using Chrome or Firefox in this case)? It almost looks like something that could be pasted in to Firefox/Greasemonkey, but not quite. Just by a quick Google on the file type I haven't been able to get heads or tails on it.

那么,我到底应该用这段代码做什么?该网站不提供进一步的说明,假设用户知道如何处理它。我猜您应该将代码粘贴到其他一些未提及的浏览器应用程序中(在这种情况下使用 Chrome 或 Firefox)?它几乎看起来像是可以粘贴到 Firefox/Greasemonkey 中的东西,但并不完全如此。只是通过对文件类型的快速谷歌我一直无法得到正面或反面。

I'm sure there's a simple explanation on what to do with these files out there, but it seems to be buried in plenty of posts where people are already assuming you know what to do with these files. Anyone willing to just simply say what needs to be done from square one after getting to the page with the code to actually implementing it? Thanks.

我确信对于如何处理这些文件有一个简单的解释,但它似乎隐藏在许多帖子中,人们已经假设您知道如何处理这些文件。任何人都愿意在进入带有代码的页面以实际实现它之后,简单地说出从第一个方面需要做什么?谢谢。

采纳答案by álvaro González

If you open your second link in a browser you'll see the source code:

如果您在浏览器中打开第二个链接,您将看到源代码:

#!/bin/bash
# Script to download individual .nc files from the ORNL
# Daymet server at: http://daymet.ornl.gov

[...]

[...]

# For ranges use {start..end}
# for individul vaules, use: 1 2 3 4 
for year in {2002..2003}
do
   for tile in {1159..1160}
        do wget --limit-rate=3m http://daymet.ornl.gov/thredds/fileServer/allcf/${year}/${tile}_${year}/vp.nc -O ${tile}_${year}_vp.nc
        # An example using curl instead of wget
    #do curl --limit-rate 3M -o ${tile}_${year}_vp.nc http://daymet.ornl.gov/thredds/fileServer/allcf/${year}/${tile}_${year}/vp.nc
     done
done

So it's a bash script. Got Linux?

所以这是一个bash脚本。有 Linux 吗?



In any case, the script is nothing but a series of HTTP retrievals. Both wget and curl are available for most operating systems and almost all language have HTTP libraries so it's fairly trivial to rewrite in any other technology. There're also some Windows ports of bash itself (git includes one). Last but not least, Windows 10 now has native support for Linux binaries.

无论如何,该脚本只不过是一系列 HTTP 检索。wget 和 curl 都可用于大多数操作系统,并且几乎所有语言都有 HTTP 库,因此用任何其他技术重写都相当简单。还有一些 bash 本身的 Windows 端口(git 包括一个)。最后但并非最不重要的一点是,Windows 10 现在具有对 Linux 二进制文件的本机支持

回答by Nelson

shfiles are unix (linux) shell executables files, they are the equivalent (but much more powerful) of batfiles on windows.

sh文件是 unix (linux) shell 可执行文件,它们与batwindows 上的文件等效(但功能更强大)。

So you need to run it from a linux console, just typing its name the same you do with bat files on windows.

因此,您需要从 linux 控制台运行它,只需像在 Windows 上使用 bat 文件一样输入它的名称即可。

回答by David Bauer

Typically a .sh file is a shell script which you can execute in a terminal. Specifically, the script you mentioned is a bash script, which you can see if you open the file and look in the first line of the file, which is called the shebang or magic line.

通常,.sh 文件是一个可以在终端中执行的 shell 脚本。具体来说,你提到的脚本是一个bash脚本,打开文件查看文件的第一行,也就是shebang或者magic line就可以看到。

回答by Sam

I know this is an old question and I probably won't help, but many Linux distributions(e.g., ubuntu) have a "Live cd/usb" function, so if you really need to run this script, you could try booting your computer into Linux. Just burn a .iso to a flash drive (here's how http://goo.gl/U1wLYA), start your computer with the drive plugged in, and press the F key for boot menu. If you choose "...USB...", you will boot into the OS you just put on the drive.

我知道这是一个老问题,我可能帮不上忙,但是许多 Linux 发行版(例如 ubuntu)都有“Live cd/usb”功能,所以如果你真的需要运行这个脚本,你可以尝试启动你的计算机进入Linux。只需将 .iso 刻录到闪存驱动器(这是http://goo.gl/U1wLYA 的方法),在插入驱动器的情况下启动计算机,然后按 F 键进入启动菜单。如果您选择“...USB...”,您将启动到刚刚安装在驱动器上的操作系统。

回答by v010dya

What is a file with extension .sh?

什么是扩展名为 .sh 的文件?

It is a Bourne shell script. They are used in many variations of UNIX-like operating systems. They have no "language" and are interpreted by your shell (interpreter of terminal commands) or if the first line is in the form

它是一个Bourne shell 脚本。它们用于许多类 UNIX 操作系统的变体中。它们没有“语言”,由您的外壳程序(终端命令的解释器)解释,或者如果第一行采用以下形式

#!/path/to/interpreter

they will use that particular interpreter. Your file has the first line:

他们将使用那个特定的解释器。您的文件有第一行:

#!/bin/bash

and that means that it uses Bourne Again Shell, so called bash. It is for all practical purposes a replacement for good old sh.

这意味着它使用 Bourne Again Shell,即所谓的 bash。出于所有实际目的,它是旧 sh 的替代品。

Depending upon the interpreter you will have different language in which the file is written.

根据解释器的不同,您将使用不同的语言来编写文件。

Keep in mind, that in UNIX world, it is not the extension of the file that determines what the file is (see How to execute a shell script).

请记住,在 UNIX 世界中,决定文件内容的不是文件的扩展名(请参阅如何执行 shell 脚本)。

If you come from the world of DOS/Windows, you will be familiar with files that have .bat or .cmd extensions (batch files). They are not similar in content, but are akin in design.

如果您来自 DOS/Windows 世界,您将熟悉具有 .bat 或 .cmd 扩展名的文件(批处理文件)。它们在内容上不相似,但在设计上相似。

How to execute a shell script

如何执行shell脚本

Unlike some silly operating systems, *nix does not rely exclusively on extensions to determine what to do with a file. Permissions are also used. This means that if you attempt to run the shell script after downloading it, it will be the same as trying to "run" any text file. The ".sh" extension is there only for your convenience to recognize that file.

与一些愚蠢的操作系统不同,*nix 并不完全依赖扩展名来确定如何处理文件。还使用了权限。这意味着如果您在下载后尝试运行 shell 脚本,它与尝试“运行”任何文本文件的效果相同。“.sh”扩展名只是为了方便您识别该文件。

You will need to make the file executable. Let's assume that you have downloaded your file as file.sh, you can then run in your terminal:

您需要使文件可执行。假设您已将文件下载为file.sh,然后可以在终端中运行:

chmod +x file.sh

chmodis a command for changing file's permissions, +xsets execute permissions (in this case for everybody) and finally you have your file name.

chmod是用于更改文件权限的命令,+x设置执行权限(在这种情况下为每个人),最后你有你的文件名。

You can also do it in GUI. Most of the time you can right click on the file and select properties, in XUbuntu the permissions options look like this:

您也可以在 GUI 中执行此操作。大多数情况下,您可以右键单击文件并选择属性,在 XUbuntu 中,权限选项如下所示:

File permissions in XUbuntu

XUbuntu 中的文件权限

If you do not wish to change the permissions. You can also force the shell to run the command. In the terminal you can run:

如果您不想更改权限。您还可以强制 shell 运行该命令。在终端中,您可以运行:

bash file.sh

The shell should be the same as in the first line of your script.

shell 应该与脚本的第一行中的相同。

How safe is it?

它有多安全?

You may find it weird that you must perform another task manually in order to execute a file. But this is partially because of strong need for security.

您可能会发现必须手动执行另一个任务才能执行文件很奇怪。但这部分是因为对安全的强烈需求。

Basically when you download and run a bash script, it is the same thing as somebody telling you "run all these commands in sequence on your computer, I promise that the results will be good and safe". Ask yourself if you trust the party that has supplied this file, ask yourself if you are sure that have downloaded the file from the same place as you thought, maybe even have a glance inside to see if something looks out of place (although that requires that you know something about *nix commands and bash programming).

基本上,当您下载并运行 bash 脚本时,这与有人告诉您“在您的计算机上按顺序运行所有这些命令,我​​保证结果将是好的和安全的”是一样的。问问自己是否信任提供此文件的一方,问问自己是否确定从同一个位置下载了该文件,甚至可以看一眼里面是否有什么地方看起来不合适(尽管这需要您对 *nix 命令和 bash 编程有所了解)。

Unfortunately apart from the warning above I cannot give a step-by-step description of what you should do to prevent evil things from happening with your computer; so just keep in mind that any time you get and run an executable file from someone you're actually saying, "Sure, you can use my computer to do something".

不幸的是,除了上面的警告之外,我无法一步一步地描述你应该做什么来防止你的计算机发生邪恶的事情;所以只要记住,任何时候你从某人那里得到并运行一个可执行文件时,你实际上是在说,“当然,你可以用我的电脑做一些事情”。

回答by Buckell

A .sh file is a shell file. It can be used to open programs and what not.

.sh 文件是一个 shell 文件。它可以用来打开程序,什么不能。

回答by PRAKHAR GUPTA

open the location in terminal then type these commands 1. chmod +x filename.sh 2. ./filename.sh that's it

在终端中打开位置然后输入这些命令 1. chmod +x filename.sh 2. ./filename.sh 就是这样